This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients lukasz.langa, serhiy.storchaka
Date 2021-08-06.18:25:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1628274328.4.0.627207641933.issue44852@roundup.psfhosted.org>
In-reply-to
Content
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):
    ...
History
Date User Action Args
2021-08-06 18:25:28serhiy.storchakasetrecipients: + serhiy.storchaka, lukasz.langa
2021-08-06 18:25:28serhiy.storchakasetmessageid: <1628274328.4.0.627207641933.issue44852@roundup.psfhosted.org>
2021-08-06 18:25:28serhiy.storchakalinkissue44852 messages
2021-08-06 18:25:28serhiy.storchakacreate