Skip to content

Commit

Permalink
Fix tantale#15: @deprecated respects global warning filters with …
Browse files Browse the repository at this point in the history
…actions other than "ignore" and "always" on Python 3
  • Loading branch information
heidecjj committed Apr 21, 2020
1 parent afd1c60 commit 2c6ef04
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
12 changes: 8 additions & 4 deletions deprecated/classic.py
Expand Up @@ -161,9 +161,11 @@ def __call__(self, wrapped):

def wrapped_cls(cls, *args, **kwargs):
msg = self.get_deprecated_msg(wrapped, None)
with warnings.catch_warnings():
if self.action:
if self.action:
with warnings.catch_warnings():
warnings.simplefilter(self.action, self.category)
warnings.warn(msg, category=self.category, stacklevel=_class_stacklevel)
else:
warnings.warn(msg, category=self.category, stacklevel=_class_stacklevel)
if old_new1 is object.__new__:
return old_new1(cls)
Expand Down Expand Up @@ -274,9 +276,11 @@ def some_old_function(x, y):
@wrapt.decorator(adapter=adapter)
def wrapper_function(wrapped_, instance_, args_, kwargs_):
msg = adapter.get_deprecated_msg(wrapped_, instance_)
with warnings.catch_warnings():
if action:
if action:
with warnings.catch_warnings():
warnings.simplefilter(action, category)
warnings.warn(msg, category=category, stacklevel=_routine_stacklevel)
else:
warnings.warn(msg, category=category, stacklevel=_routine_stacklevel)
return wrapped_(*args_, **kwargs_)

Expand Down
5 changes: 3 additions & 2 deletions tests/test_deprecated.py
Expand Up @@ -248,8 +248,9 @@ def test_respect_global_filter():
def fun():
print("fun")

warnings.simplefilter("ignore", category=DeprecationWarning)
warnings.simplefilter("once", category=DeprecationWarning)

with warnings.catch_warnings(record=True) as warns:
fun()
assert len(warns) == 0
fun()
assert len(warns) == 1
13 changes: 13 additions & 0 deletions tests/test_deprecated_class.py
Expand Up @@ -106,6 +106,19 @@ class MySubClass(MyBaseClass):
assert issubclass(MySubClass, MyBaseClass)


def test_class_respect_global_filter():
@deprecated.classic.deprecated
class MyBaseClass(object):
pass

with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("once")
obj = MyBaseClass()
obj = MyBaseClass()

assert len(warns) == 1


def test_subclass_deprecation_using_deprecated_decorator():
@deprecated.classic.deprecated
class MyBaseClass(object):
Expand Down

0 comments on commit 2c6ef04

Please sign in to comment.