Как я могу подавить невстроенные предупреждения с помощью параметра python -W{action:message:category:module:line?
Среда
Я использую официальный образ Docker на основе Python python:3.7.9 со следующей настройкой:
Код: Выделить всё
$ 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
Когда я запускаю тесты в старом проекте Python (Python==3.7.9), я получаю следующее предупреждение:
Код: Выделить всё
/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.
Код: Выделить всё
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,
)
Я знаю это обновление Python — лучшее радикальное решение, но сейчас я хочу подавить только это конкретное предупреждение cryptography.utils.CryptographyDeprecationWarning, чтобы сосредоточиться на тестировании других частей проекта. Однако я не знаю, как указать невстроенные предупреждения по категориям.
Подводя итог моему вопросу, есть ли способ правильно указать и подавить эти невстроенные предупреждения. в предупреждении с помощью параметра командной строки - не путем сопоставления сообщений, а путем указания категории предупреждения?
Что я пробовал
Указание имени категории предупреждения не работает.
Например:
Код: Выделить всё
$ 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'
Использование полного пути к модулю не может подавить предупреждение:
Код: Выделить всё
$ 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 =========================================
Код: Выделить всё
$ 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 ============================================================================================
Код: Выделить всё
[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
Подробнее здесь: https://stackoverflow.com/questions/792 ... ionwarning