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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
# -*- coding: utf-8 -*-
import inspect
import sys
import warnings
import pytest
import deprecated.classic
class MyDeprecationWarning(DeprecationWarning):
pass
class WrongStackLevelWarning(DeprecationWarning):
pass
_PARAMS = [
None,
((), {}),
(('Good reason',), {}),
((), {'reason': 'Good reason'}),
((), {'version': '1.2.3'}),
((), {'action': 'once'}),
((), {'category': MyDeprecationWarning}),
((), {'extra_stacklevel': 1, 'category': WrongStackLevelWarning}),
]
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_function(request):
if request.param is None:
@deprecated.classic.deprecated
def foo1():
pass
return foo1
else:
args, kwargs = request.param
@deprecated.classic.deprecated(*args, **kwargs)
def foo1():
pass
return foo1
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_class(request):
if request.param is None:
@deprecated.classic.deprecated
class Foo2(object):
pass
return Foo2
else:
args, kwargs = request.param
@deprecated.classic.deprecated(*args, **kwargs)
class Foo2(object):
pass
return Foo2
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_method(request):
if request.param is None:
class Foo3(object):
@deprecated.classic.deprecated
def foo3(self):
pass
return Foo3
else:
args, kwargs = request.param
class Foo3(object):
@deprecated.classic.deprecated(*args, **kwargs)
def foo3(self):
pass
return Foo3
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_static_method(request):
if request.param is None:
class Foo4(object):
@staticmethod
@deprecated.classic.deprecated
def foo4():
pass
return Foo4.foo4
else:
args, kwargs = request.param
class Foo4(object):
@staticmethod
@deprecated.classic.deprecated(*args, **kwargs)
def foo4():
pass
return Foo4.foo4
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_class_method(request):
if request.param is None:
class Foo5(object):
@classmethod
@deprecated.classic.deprecated
def foo5(cls):
pass
return Foo5
else:
args, kwargs = request.param
class Foo5(object):
@classmethod
@deprecated.classic.deprecated(*args, **kwargs)
def foo5(cls):
pass
return Foo5
# noinspection PyShadowingNames
def test_classic_deprecated_function__warns(classic_deprecated_function):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
classic_deprecated_function()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated function (or staticmethod)" in str(warn.message)
assert warn.filename == __file__ or warn.category is WrongStackLevelWarning, 'Incorrect warning stackLevel'
# noinspection PyShadowingNames
def test_classic_deprecated_class__warns(classic_deprecated_class):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
classic_deprecated_class()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated class" in str(warn.message)
assert warn.filename == __file__ or warn.category is WrongStackLevelWarning, 'Incorrect warning stackLevel'
# noinspection PyShadowingNames
def test_classic_deprecated_method__warns(classic_deprecated_method):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
obj = classic_deprecated_method()
obj.foo3()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated method" in str(warn.message)
assert warn.filename == __file__ or warn.category is WrongStackLevelWarning, 'Incorrect warning stackLevel'
# noinspection PyShadowingNames
def test_classic_deprecated_static_method__warns(classic_deprecated_static_method):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
classic_deprecated_static_method()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated function (or staticmethod)" in str(warn.message)
assert warn.filename == __file__ or warn.category is WrongStackLevelWarning, 'Incorrect warning stackLevel'
# noinspection PyShadowingNames
def test_classic_deprecated_class_method__warns(classic_deprecated_class_method):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
cls = classic_deprecated_class_method()
cls.foo5()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
if (3, 9) <= sys.version_info < (3, 13):
assert "deprecated class method" in str(warn.message)
else:
assert "deprecated function (or staticmethod)" in str(warn.message)
assert warn.filename == __file__ or warn.category is WrongStackLevelWarning, 'Incorrect warning stackLevel'
def test_should_raise_type_error():
try:
deprecated.classic.deprecated(5)
assert False, "TypeError not raised"
except TypeError:
pass
def test_warning_msg_has_reason():
reason = "Good reason"
@deprecated.classic.deprecated(reason=reason)
def foo():
pass
with warnings.catch_warnings(record=True) as warns:
foo()
warn = warns[0]
assert reason in str(warn.message)
def test_warning_msg_has_version():
version = "1.2.3"
@deprecated.classic.deprecated(version=version)
def foo():
pass
with warnings.catch_warnings(record=True) as warns:
foo()
warn = warns[0]
assert version in str(warn.message)
def test_warning_is_ignored():
@deprecated.classic.deprecated(action='ignore')
def foo():
pass
with warnings.catch_warnings(record=True) as warns:
foo()
assert len(warns) == 0
def test_specific_warning_cls_is_used():
@deprecated.classic.deprecated(category=MyDeprecationWarning)
def foo():
pass
with warnings.catch_warnings(record=True) as warns:
foo()
warn = warns[0]
assert issubclass(warn.category, MyDeprecationWarning)
def test_respect_global_filter():
@deprecated.classic.deprecated(version='1.2.1', reason="deprecated function")
def fun():
print("fun")
warnings.simplefilter("once", category=DeprecationWarning)
with warnings.catch_warnings(record=True) as warns:
fun()
fun()
assert len(warns) == 1
def test_default_stacklevel():
"""
The objective of this unit test is to ensure that the triggered warning message,
when invoking the 'use_foo' function, correctly indicates the line where the
deprecated 'foo' function is called.
"""
@deprecated.classic.deprecated
def foo():
pass
def use_foo():
foo()
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
use_foo()
# Check that the warning path matches the module path
warn = warns[0]
assert warn.filename == __file__
# Check that the line number points to the first line inside 'use_foo'
use_foo_lineno = inspect.getsourcelines(use_foo)[1]
assert warn.lineno == use_foo_lineno + 1
def test_extra_stacklevel():
"""
The unit test utilizes an 'extra_stacklevel' of 1 to ensure that the warning message
accurately identifies the caller of the deprecated function. It verifies that when
the 'use_foo' function is called, the warning message correctly indicates the line
where the call to 'use_foo' is made.
"""
@deprecated.classic.deprecated(extra_stacklevel=1)
def foo():
pass
def use_foo():
foo()
def demo():
use_foo()
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
demo()
# Check that the warning path matches the module path
warn = warns[0]
assert warn.filename == __file__
# Check that the line number points to the first line inside 'demo'
demo_lineno = inspect.getsourcelines(demo)[1]
assert warn.lineno == demo_lineno + 1
|