Unable to suppress non-built-in warnings e.g. “CryptographyDeprecationWarning”

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.

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật