Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

There are problems with clearing all ignored deprecation filters.

We can want to ignore warnings in narrower or wider scope: per-method, 
per-class, per-module. We should use the warnings.catch_warnings() context 
manager for restoring filters in the correct scope. For example, use 
setUp/tearDown, setUpClass/tearDownClass, setUpModule/tearDownModule and save 
the context manager as an instance/class attribute or module global.

   def setUp(self):
       self.w = warnings.catch_warnings()
       self.w.__enter__()
       warnings.filterwarnings(...)

    def tearDown():
        self.w.__exit__(None, None, None)

It is hard to use any helper here because the code should be added in multiple 
places.

Or use setUp/addCleanup, setUpClass/addClassCleanup, 
setUpModule/addModuleCleanup if you want to keep all code in one place:

   def setUp(self):
       w = warnings.catch_warnings()
       w.__enter__()
       self.addCleanup(w.__exit__, None, None, None)
       warnings.filterwarnings(...)

The helper can call catch_warnings(), __enter__(), set filters and return a 
caller which calls __exit__(None, None, None):

   def setUp(self):
       self.addCleanup(some_helper(...))

For class and method scopes it would be convenient to use class and method 
decorators correspondingly.

    @ignore_some_warnings(...)
    def test_foo(self):
        ...

@ignore_some_warnings(...)
class BarTests(unittest.TestCase):
    ...

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44852>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to