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 7.1 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  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. @classmethod
  30. def new_class_func(cls):
  31. return None
  32. @classmethod
  33. @deprecated(deprecated_since="3.2.0", removed_from="10.0.0", target=new_class_func)
  34. def deprecated_class_func(cls):
  35. return cls.new_class_func()
  36. @staticmethod
  37. def new_static_func():
  38. return None
  39. @staticmethod
  40. @deprecated(deprecated_since="3.2.0", removed_from="10.0.0", target=new_static_func)
  41. def deprecated_static_func():
  42. return TestDeprecationDecorator.new_static_func
  43. def test_emits_warning(self):
  44. """Ensure that the deprecated function emits a warning when called."""
  45. with warnings.catch_warnings(record=True) as w:
  46. warnings.simplefilter("always")
  47. self.fully_configured_deprecated_func()
  48. self.assertEqual(len(w), 1)
  49. def test_displays_deprecated_version(self):
  50. """Ensure that the warning contains the version in which the function was deprecated."""
  51. with warnings.catch_warnings(record=True) as w:
  52. warnings.simplefilter("always")
  53. self.fully_configured_deprecated_func()
  54. self.assertTrue(any("3.2.0" in str(warning.message) for warning in w))
  55. def test_displays_removed_version(self):
  56. """Ensure that the warning contains the version in which the function will be removed."""
  57. with warnings.catch_warnings(record=True) as w:
  58. warnings.simplefilter("always")
  59. self.fully_configured_deprecated_func()
  60. self.assertTrue(any("10.0.0" in str(warning.message) for warning in w))
  61. def test_guidance_on_replacement(self):
  62. """Ensure that if a replacement target is provided, guidance on using the new function is included in the warning."""
  63. with warnings.catch_warnings(record=True) as w:
  64. warnings.simplefilter("always")
  65. self.fully_configured_deprecated_func()
  66. self.assertTrue(any("new_func" in str(warning.message) for warning in w))
  67. def test_displays_reason(self):
  68. """Ensure that if provided, the reason for deprecation is included in the warning."""
  69. reason_str = "Replaced for optimization"
  70. with warnings.catch_warnings(record=True) as w:
  71. warnings.simplefilter("always")
  72. self.fully_configured_deprecated_func()
  73. self.assertTrue(any(reason_str in str(warning.message) for warning in w))
  74. def test_triggered_only_once(self):
  75. """Ensure that the deprecation warning is triggered only once even if the deprecated function is called multiple times."""
  76. with warnings.catch_warnings(record=True) as w:
  77. warnings.simplefilter("always")
  78. for _ in range(10):
  79. self.fully_configured_deprecated_func()
  80. self.assertEqual(len(w), 1, "Only one warning should be emitted")
  81. def test_basic_deprecation_emits_warning(self):
  82. """Ensure that a function with minimal deprecation configuration emits a warning."""
  83. with warnings.catch_warnings(record=True) as w:
  84. warnings.simplefilter("always")
  85. self.basic_deprecated_func()
  86. self.assertEqual(len(w), 1)
  87. def test_class_method_deprecation_emits_warning(self):
  88. with warnings.catch_warnings(record=True) as w:
  89. warnings.simplefilter("always")
  90. _ = TestDeprecationDecorator.deprecated_class_func()
  91. self.assertEqual(len(w), 1)
  92. def test_static_method_deprecation_emits_warning(self):
  93. with warnings.catch_warnings(record=True) as w:
  94. warnings.simplefilter("always")
  95. _ = TestDeprecationDecorator.deprecated_static_func()
  96. self.assertEqual(len(w), 1)
  97. def test_class_deprecation_warning(self):
  98. """Ensure that creating an instance of a deprecated class emits a warning."""
  99. with warnings.catch_warnings(record=True) as w:
  100. warnings.simplefilter("always")
  101. _ = self.DeprecatedClass() # Instantiate the deprecated class
  102. self.assertEqual(len(w), 1)
  103. def test_class_deprecation_message_content(self):
  104. """Ensure that the emitted warning for a deprecated class contains relevant information including target class."""
  105. with warnings.catch_warnings(record=True) as w:
  106. warnings.simplefilter("always")
  107. _ = self.DeprecatedClass()
  108. self.assertTrue(any("3.2.0" in str(warning.message) for warning in w))
  109. self.assertTrue(any("10.0.0" in str(warning.message) for warning in w))
  110. self.assertTrue(any("DeprecatedClass" in str(warning.message) for warning in w))
  111. self.assertTrue(any("Replaced for optimization" in str(warning.message) for warning in w))
  112. self.assertTrue(any("NewClass" in str(warning.message) for warning in w))
  113. def test_raise_error_when_library_version_equals_removal_version(self):
  114. """Ensure that an error is raised when the library's version equals the function's removal version."""
  115. with patch("super_gradients.__version__", "10.1.0"): # Mocking the version to be equal to removal version
  116. with self.assertRaises(ImportError):
  117. @deprecated(deprecated_since="3.2.0", removed_from="10.1.0", target=self.new_func)
  118. def deprecated_func_version_equal():
  119. return
  120. deprecated_func_version_equal()
  121. def test_no_error_when_library_version_below_removal_version(self):
  122. """Ensure that no error is raised when the library's version is below the function's removal version."""
  123. with patch("super_gradients.__version__", "10.1.0"): # Mocking the version to be below removal version
  124. @deprecated(deprecated_since="3.2.0", removed_from="10.2.0", target=self.new_func)
  125. def deprecated_func_version_below():
  126. return
  127. deprecated_func_version_below()
  128. if __name__ == "__main__":
  129. unittest.main()
Tip!

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

Comments

Loading...