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 <>
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()

    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()
       self.addCleanup(w.__exit__, None, None, None)

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

   def setUp(self):

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

    def test_foo(self):

class BarTests(unittest.TestCase):
Date User Action Args
2021-08-06 18:25:28serhiy.storchakasetrecipients: + serhiy.storchaka, lukasz.langa
2021-08-06 18:25:28serhiy.storchakasetmessageid: <>
2021-08-06 18:25:28serhiy.storchakalinkissue44852 messages
2021-08-06 18:25:28serhiy.storchakacreate