Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_deprecate.py 6.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  1. import warnings
  2. import unittest
  3. from unittest.mock import patch
  4. from super_gradients.common.deprecate import deprecated
  5. class TestDeprecationDecorator(unittest.TestCase):
  6. def setUp(self):
  7. """Prepare required functions before each test."""
  8. self.new_function_message = "This is the new function!"
  9. def new_func():
  10. return self.new_function_message
  11. @deprecated(deprecated_since="3.2.0", removed_from="10.0.0", target=new_func, reason="Replaced for optimization")
  12. def fully_configured_deprecated_func():
  13. return new_func()
  14. @deprecated(deprecated_since="3.2.0", removed_from="10.0.0")
  15. def basic_deprecated_func():
  16. return new_func()
  17. self.new_func = new_func
  18. self.fully_configured_deprecated_func = fully_configured_deprecated_func
  19. self.basic_deprecated_func = basic_deprecated_func
  20. class NewClass:
  21. def __init__(self):
  22. pass
  23. @deprecated(deprecated_since="3.2.0", removed_from="10.0.0", target=NewClass, reason="Replaced for optimization")
  24. class DeprecatedClass:
  25. def __init__(self):
  26. pass
  27. self.NewClass = NewClass
  28. self.DeprecatedClass = DeprecatedClass
  29. def test_emits_warning(self):
  30. """Ensure that the deprecated function emits a warning when called."""
  31. with warnings.catch_warnings(record=True) as w:
  32. warnings.simplefilter("always")
  33. self.fully_configured_deprecated_func()
  34. self.assertEqual(len(w), 1)
  35. def test_displays_deprecated_version(self):
  36. """Ensure that the warning contains the version in which the function was deprecated."""
  37. with warnings.catch_warnings(record=True) as w:
  38. warnings.simplefilter("always")
  39. self.fully_configured_deprecated_func()
  40. self.assertTrue(any("3.2.0" in str(warning.message) for warning in w))
  41. def test_displays_removed_version(self):
  42. """Ensure that the warning contains the version in which the function will be removed."""
  43. with warnings.catch_warnings(record=True) as w:
  44. warnings.simplefilter("always")
  45. self.fully_configured_deprecated_func()
  46. self.assertTrue(any("10.0.0" in str(warning.message) for warning in w))
  47. def test_guidance_on_replacement(self):
  48. """Ensure that if a replacement target is provided, guidance on using the new function is included in the warning."""
  49. with warnings.catch_warnings(record=True) as w:
  50. warnings.simplefilter("always")
  51. self.fully_configured_deprecated_func()
  52. self.assertTrue(any("new_func" in str(warning.message) for warning in w))
  53. def test_displays_reason(self):
  54. """Ensure that if provided, the reason for deprecation is included in the warning."""
  55. reason_str = "Replaced for optimization"
  56. with warnings.catch_warnings(record=True) as w:
  57. warnings.simplefilter("always")
  58. self.fully_configured_deprecated_func()
  59. self.assertTrue(any(reason_str in str(warning.message) for warning in w))
  60. def test_triggered_only_once(self):
  61. """Ensure that the deprecation warning is triggered only once even if the deprecated function is called multiple times."""
  62. with warnings.catch_warnings(record=True) as w:
  63. warnings.simplefilter("always")
  64. for _ in range(10):
  65. self.fully_configured_deprecated_func()
  66. self.assertEqual(len(w), 1, "Only one warning should be emitted")
  67. def test_basic_deprecation_emits_warning(self):
  68. """Ensure that a function with minimal deprecation configuration emits a warning."""
  69. with warnings.catch_warnings(record=True) as w:
  70. warnings.simplefilter("always")
  71. self.basic_deprecated_func()
  72. self.assertEqual(len(w), 1)
  73. def test_class_deprecation_warning(self):
  74. """Ensure that creating an instance of a deprecated class emits a warning."""
  75. with warnings.catch_warnings(record=True) as w:
  76. warnings.simplefilter("always")
  77. _ = self.DeprecatedClass() # Instantiate the deprecated class
  78. self.assertEqual(len(w), 1)
  79. def test_class_deprecation_message_content(self):
  80. """Ensure that the emitted warning for a deprecated class contains relevant information including target class."""
  81. with warnings.catch_warnings(record=True) as w:
  82. warnings.simplefilter("always")
  83. _ = self.DeprecatedClass()
  84. self.assertTrue(any("3.2.0" in str(warning.message) for warning in w))
  85. self.assertTrue(any("10.0.0" in str(warning.message) for warning in w))
  86. self.assertTrue(any("DeprecatedClass" in str(warning.message) for warning in w))
  87. self.assertTrue(any("Replaced for optimization" in str(warning.message) for warning in w))
  88. self.assertTrue(any("NewClass" in str(warning.message) for warning in w))
  89. def test_raise_error_when_library_version_equals_removal_version(self):
  90. """Ensure that an error is raised when the library's version equals the function's removal version."""
  91. with patch("super_gradients.__version__", "10.1.0"): # Mocking the version to be equal to removal version
  92. with self.assertRaises(ImportError):
  93. @deprecated(deprecated_since="3.2.0", removed_from="10.1.0", target=self.new_func)
  94. def deprecated_func_version_equal():
  95. return
  96. deprecated_func_version_equal()
  97. def test_no_error_when_library_version_below_removal_version(self):
  98. """Ensure that no error is raised when the library's version is below the function's removal version."""
  99. with patch("super_gradients.__version__", "10.1.0"): # Mocking the version to be below removal version
  100. @deprecated(deprecated_since="3.2.0", removed_from="10.2.0", target=self.new_func)
  101. def deprecated_func_version_below():
  102. return
  103. deprecated_func_version_below()
  104. if __name__ == "__main__":
  105. unittest.main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...