My use-case was not in CPython’s core tests, but in my custom project, where I wanted to be sure that I do not show warnings in some cases (since one of my users reported a bug in this part).
I also found that we have a special test-only helper called check_no_warnings
: cpython/warnings_helper.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
» ag check_no_warnings
Lib/test/test_grammar.py
320: from test.support.warnings_helper import check_no_warnings
1288: with self.check_no_warnings(category=SyntaxWarning):
Lib/test/support/warnings_helper.py
110:def check_no_warnings(testcase, message='', category=Warning, force_gc=False):
148: with check_no_warnings(testcase, category=ResourceWarning, force_gc=True):
Lib/test/test_file.py
209: with warnings_helper.check_no_warnings(self,
Lib/test/test_http_cookiejar.py
658: with warnings_helper.check_no_warnings(self):
Right now there are multiple simplefilter("error", ...)
usages:
» ag 'simplefilter\(.error'
Lib/test/test_coroutines.py
1682: warnings.simplefilter("error")
1759: warnings.simplefilter("error")
Lib/test/test_descr.py
1849: warnings.simplefilter('error', DeprecationWarning)
1857: warnings.simplefilter('error', DeprecationWarning)
Lib/test/test_xml_etree.py
3394: warnings.simplefilter('error', DeprecationWarning)
3414: warnings.simplefilter('error', DeprecationWarning)
3415: warnings.simplefilter('error', RuntimeWarning)
3432: warnings.simplefilter('error', DeprecationWarning)
3433: warnings.simplefilter('error', RuntimeWarning)
Lib/test/test_itertools.py
33: warnings.simplefilter("error", category=DeprecationWarning)
Lib/test/test_importlib/test_util.py
372: warnings.simplefilter('error')
Lib/test/test_importlib/import_/test_fromlist.py
153: warnings.simplefilter('error', BytesWarning)
163: warnings.simplefilter('error', BytesWarning)
Lib/test/test_grammar.py
242: warnings.simplefilter('error', SyntaxWarning)
1297: warnings.simplefilter('error', SyntaxWarning)
1485: warnings.simplefilter('error', SyntaxWarning)
1569: warnings.simplefilter('error', SyntaxWarning)
Lib/test/test_fnmatch.py
210: warnings.simplefilter('error', Warning)
Lib/test/test_runpy.py
494: warnings.simplefilter("error", RuntimeWarning)
Lib/test/test_warnings/__init__.py
347: self.module.simplefilter("error", category=UserWarning)
350: self.module.simplefilter("error", category=UserWarning)
365: self.module.simplefilter("error", append=True)
378: self.module.simplefilter("error")
Lib/test/test_unittest/test_case.py
1501: warnings.simplefilter("error", RuntimeWarning)
1548: warnings.simplefilter("error", RuntimeWarning)
1595: warnings.simplefilter("error", RuntimeWarning)
1639: warnings.simplefilter("error", RuntimeWarning)
Lib/test/support/warnings_helper.py
39: warnings.simplefilter('error', SyntaxWarning)
Lib/test/test_re.py
1581: warnings.simplefilter('error', BytesWarning)
2131: warnings.simplefilter('error', BytesWarning)
Lib/test/test_codeop.py
288: warnings.simplefilter('error', SyntaxWarning)
293: warnings.simplefilter('error', SyntaxWarning)
Lib/test/test_string_literals.py
124: warnings.simplefilter('error', category=SyntaxWarning)
149: warnings.simplefilter('error', category=SyntaxWarning)
201: warnings.simplefilter('error', category=SyntaxWarning)
225: warnings.simplefilter('error', category=SyntaxWarning)
Lib/test/test_hmac.py
355: warnings.simplefilter('error', RuntimeWarning)
Lib/test/test_random.py
189: warnings.simplefilter("error", DeprecationWarning)
Doc/conf.py
44:warnings.simplefilter('error')
And catch_warnings(record=True)
hack is also present:
- cpython/test_codeop.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
- cpython/test_grammar.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
- cpython/test_string_literals.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
- cpython/test_string_literals.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
- cpython/test_string_literals.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
- cpython/test_string_literals.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
- cpython/test_base_events.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
- cpython/test_subprocess.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub
So, I can say that this is a quite popular thing to solve in CPython tests as well.
And the existence of check_no_warnings
proves that this is a desired feature.
@iritkatriel I agree that for CPython this is a working alternative, but unittest
has many users outside of CPython and it is not easy to just fail on all warnings for many projects. Because of how many warnings there are in average projects with multiple runtime / tests deps.
For example, even my current project I am working on has these warnings in tests:
warning : /Users/sobolev/Desktop/coverage-conditional-plugin/.venv/lib/python3.11/site-packages/pytest_cov/plugin.py:256: PytestDeprecationWarning: The hookimpl CovPlugin.pytest_configure_node uses old-style configuration options (marks or attributes).
Please use the pytest.hookimpl(optionalhook=True) decorator instead
to configure the hooks.
See https://docs.pytest.org/en/latest/deprecations.html#configuring-hook-specs-impls-using-markers
def pytest_configure_node(self, node):
warning : /Users/sobolev/Desktop/coverage-conditional-plugin/.venv/lib/python3.11/site-packages/pytest_cov/plugin.py:265: PytestDeprecationWarning: The hookimpl CovPlugin.pytest_testnodedown uses old-style configuration options (marks or attributes).
Please use the pytest.hookimpl(optionalhook=True) decorator instead
to configure the hooks.
See https://docs.pytest.org/en/latest/deprecations.html#configuring-hook-specs-impls-using-markers
def pytest_testnodedown(self, node, error):
So, I would need to set up filterwarnings
in pytest correctly to do a single assert. This is quite complex. We can do better.
But, I don’t push too hard for this feature, because there are some alternatives, but I still think that there are use-cases for it and the implementation is very simple (again, we can take a look at cpython/warnings_helper.py at 41de54378d54f7ffc38f07db4219e80f48c4249e · python/cpython · GitHub)