Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest - Suppress DeprecationWarning from specific 3rd party modules

Tags:

When I run pytest I'm getting some deprecation warnings from a 3rd party library. I'd like to be informed about any deprecation warnings in my own code, but not in a vendored copy of a library bundled with another 3rd-party library.

This answer was helpful in getting me partway there. If I run pytest like this: $ pytest ./tests/ I get:

$ pytest ./tests/ ============================= test session starts ============================== platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini collected 5 items                                                                tests/test_file1.py .                                                   [ 20%] tests/test_file2.py ....                                                [100%]  =============================== warnings summary =============================== /home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1 /home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1   /home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working     from collections import Mapping, MutableMapping  -- Docs: https://docs.pytest.org/en/latest/warnings.html ======================== 5 passed, 2 warnings in 2.54s ========================= 

but if I run pytest like this: $ pytest ./tests/ -W ignore::DeprecationWarning I get:

============================= test session starts ============================== platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini collected 5 items                                                                tests/test_file1.py .                                                   [ 20%] tests/test_file2.py ....                                                [100%]  ============================== 5 passed in 2.61s =============================== 

This second output shows me that the filter works, but that will also hide any deprecation warnings I'd like to seeing resulting from my own code.

Part of this issue is that I'm not sure which module to try referencing in the ignore filter. I've tried $ pytest ./tests/ -W ignore::DeprecationWarning:urllib3.*: and I've tried $ pytest ./tests/ -W ignore::DeprecationWarning:botocore.*:. Both of these result in the same output as the first example with no filtering.

How can I filter out DeprecationWarnings from the version of urllib3 packaged with the vendored version of requests included with botocore (which gets called when I run commands with the boto3 library)?

like image 324
WhiteHotLoveTiger Avatar asked Oct 15 '19 17:10

WhiteHotLoveTiger


People also ask

How do I stop warnings in PyTest?

Disabling warnings summary Although not recommended, you can use the --disable-warnings command-line option to suppress the warning summary entirely from the test run output.

How do you suppress a warning in Python?

Use the filterwarnings() Function to Suppress Warnings in Python. The warnings module handles warnings in Python. We can show warnings raised by the user with the warn() function. We can use the filterwarnings() function to perform actions on specific warnings.

What is deprecation warning in Python?

To warn about deprecation, you need to set Python's builtin DeprecationWarning as category. To let the warning refer to the caller, so you know exactly where you use deprecated code, you have to set stacklevel=2 .

What is DeprecationWarning?

DeprecationWarning errors are logged by the Node. js runtime when your code (or one of the dependencies in your code) calls a deprecated API. These warnings usually include a DEP deprecation code. They are logged using console.


1 Answers

You should use the warning filters options (ini or marks):

[pytest] filterwarnings =     ignore::DeprecationWarning:botocore.*: 

Source: https://docs.python.org/3/library/warnings.html#default-warning-filter

"Individual warnings filters are specified as a sequence of fields separated by colons:"

action:message:category:module:line 
like image 54
zentia Avatar answered Sep 21 '22 08:09

zentia