Question
How can I suppress non-built-in warnings using the python -W{action:message:category:module:line}
option?
Environment
I am using an official Python-based Docker image python:3.7.9
with the following setup:
$ python -V
Python 3.7.9
$ pip list | grep -e redis -e cryptography
cryptography 44.0.0
redis 4.5.1
types-redis 4.6.0.11
Background
When I run tests on an older Python project (Python==3.7.9), I get the following warning:
/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py:1728: CryptographyDeprecationWarning: Python 3.7 is no longer supported by the Python core team and support for it is deprecated in cryptography. A future release of cryptography will remove support for Python 3.7.
This warning seems to originate from the cryptography package, which is imported indirectly via the redis
package:
if sys.version_info[:2] == (3, 7):
warnings.warn(
"Python 3.7 is no longer supported by the Python core team "
"and support for it is deprecated in cryptography. A future "
"release of cryptography will remove support for Python 3.7.",
utils.CryptographyDeprecationWarning,
stacklevel=2,
)
Source: https://github.com/pyca/cryptography/blob/main/src/cryptography/__init__.py
I know that upgrading Python is the best radical solution, but right now I want to suppress only this particular warning cryptography.utils.CryptographyDeprecationWarning
to focus on testing other parts of the project. However, I don’t know how to specify warnings that are not built-in by category.
To summarize my question, is there a way to correctly specify and suppress this non-built-in warning with a command line option – not by matching messages – but by specifying a warning category?
What I tried
Specifying a warning category name does not work.
For example:
$ pytest -Wignore::CryptographyDeprecationWarning
ERROR: while parsing the following warning configuration:
ignore::CryptographyDeprecationWarning
This error occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 1690, in parse_warning_filter
category: Type[Warning] = _resolve_warning_category(category_)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 1729, in _resolve_warning_category
cat = getattr(m, klass)
AttributeError: module 'builtins' has no attribute 'CryptographyDeprecationWarning'
Note: The -W
option passed to pytest has the same behavior as if you had given it directly to Python.
Using the full module path fails to suppress the warning:
$ pytest -Wignore::cryptography.utils.CryptographyDeprecationWarning
============================================== test session starts ===============================================
platform linux -- Python 3.7.9, pytest-7.2.1, pluggy-1.2.0
rootdir: /app, configfile: pytest.ini
plugins: mock-3.10.0
collected 192 items
... # test contents
================================================ warnings summary ================================================
../usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py:1728
/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py:1728: CryptographyDeprecationWarning: Python 3.7 is no longer supported by the Python core team and support for it is deprecated in cryptography. A future release of cryptography will remove support for Python 3.7.
m = __import__(module, None, None, [klass])
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========================================= 192 passed, 1 warning in 5.20s =========================================
On the other hand, filtering by the message text works as expected:
$ pytest -W ignore:'Python 3.7 is no longer supported by the Python core team and support for it is deprecated in cryptography. A future release of cryptography will remove support for Python 3.7.'
=========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.7.9, pytest-7.2.1, pluggy-1.2.0
rootdir: /app/proj, configfile: pytest.ini
plugins: mock-3.10.0, anyio-3.7.1
collected 6 items
... # test contents
============================================================================================ 6 passed in 0.04s ============================================================================================
This issue is the same when configuring filterwarnings in pytest.ini:
[pytest]
# filterwarnings = ignore::CryptographyDeprecationWarning # Error
# filterwarnings = ignore::cryptography.utils.CryptographyDeprecationWarning # Does not suppress
filterwarnings = ignore:Python 3.7 is no longer supported by the Python core team and support for it is deprecated in cryptography. A future release of cryptography will remove support for Python 3.7. # Works
Furthermore, the same is true if the option is given directly to Python.
$ python -Wignore::CryptographyDeprecationWarning -m pytest
# => Invalid -W option ignored: unknown warning category: 'CryptographyDeprecationWarning'
# ... Tests run with options ignored.
$ python -Wignore::cryptography.utils.CryptographyDeprecationWarning -m pytest # ignored this option
# => Invalid -W option ignored: invalid module name: 'cryptography.utils'
# ... Tests run with options ignored.
$ python -W'ignore:Python 3.7 is no longer supported by the Python core team and support for it is deprecated in cryptography. A future release of cryptography will remove support for Python 3.7.' -m pytest
# ... tests run with correctly suppressed CyptographyDeprecationWarning
I found that CryptographyDeprecationWarning
extends from UserWarning
, so I tried as follows, but it still did not work.
$ pytest -Wignore::UserWarning:cryptography.utils
# or
$ python -Wignore::UserWarning:cryptography.utils -m pytest
Notice: I want to do a temporary check, so I want to achieve this with a command line option without changing the code as much as possible.
Y. Sakishita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
8