40

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)?

WhiteHotLoveTiger
  • 2,088
  • 3
  • 30
  • 41

3 Answers3

39

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
WhiteHotLoveTiger
  • 2,088
  • 3
  • 30
  • 41
steamdragon
  • 1,052
  • 13
  • 15
  • 1
    Thanks, this will allow me to filter out all deprecation warnings where the message matches a given regex. I'm really interested in filtering them out based on module though. – WhiteHotLoveTiger Oct 15 '19 at 18:03
  • 2
    @WhiteHotLoveTiger Edited. Also, saw you commented on the referenced question. Did it work? – steamdragon Oct 15 '19 at 20:44
  • 1
    Yeah, I've finally got it working via the ini file with `filterwarnings = ignore::DeprecationWarning:botocore.*:`. This is the same filter I had tried on the command line, so I'm still confused about why it works in the ini file but not with the `-W` flag. – WhiteHotLoveTiger Oct 16 '19 at 13:53
  • Have you tried `pytest ./tests/ -W ignore::DeprecationWarning:botocore.*` ? – steamdragon Oct 16 '19 at 16:51
  • Tried just now. It gives the same result, showing the unwanted warnings :( – WhiteHotLoveTiger Oct 16 '19 at 17:22
  • 2
    Where do I put the contents of that first code snippet? I put it in `pytest.ini`, next to my `*.py` files, invoked with `python3 -m pytest *.py`, and the warning is still there. – falsePockets Jun 19 '20 at 04:37
  • The answer I needed for an hour. Thank you. – Tanner Aug 17 '22 at 23:50
5

if you are using pyproject.toml file for pytest configuration you can use:

 [tool.pytest.ini_options]
 testpaths = ["./tests/unit"]
 filterwarnings = ["ignore:::.*third_party_package.module:123", "ignore:::.*another_module*"]

ignore:::.*third_party_package.module:123 ignores in specific warning at specific line ignore:::.*another_module* ignores all warnings in the module.

Notice that you can have multiple ignores. You need to list them in []. Also, you can't have *third_party_package/module:123 you must replace all / with .

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • Note that the syntax here is from the upstream warnings filter: https://docs.python.org/3/library/warnings.html#the-warnings-filter – Paul McMillan Jan 10 '22 at 00:28
4

The answer from @Santiago Magariños does not work for me (but it put me on the correct path to find solution - so many thanks).

I use Python 3.9, pytest 6.2.1 and trying to suppress warnings from selenium 3.141.0.

I have realized that I need to prepend .* before the module name:

[pytest]
filterwarnings = ignore:::.*.selenium

or use the full "path". So to suppress the warning

../../../../../../.local/share/virtualenvs/common-bjARi2zp/lib/python3.9/site-packages/selenium/webdriver/support/wait.py:28
  /home/vaclav/.local/share/virtualenvs/common-bjARi2zp/lib/python3.9/site-packages/selenium/webdriver/support/wait.py:28: DeprecationWarning: invalid escape sequence \ 
    """Constructor, takes a WebDriver instance and timeout in seconds.

I need to use this filter in pytest.ini file:

[pytest]
filterwarnings = ignore:::.home.vaclav..local.share.virtualenvs.common-bjARi2zp.lib.python3.9.site-packages.selenium
eNca
  • 1,043
  • 11
  • 21
  • Is it possible to use cut the path before the module itself? Let's sat I have the modules, for which I want to ignore warning, in a folder (not a package) named ```mods```. Can I use ```.*.mods``` or does it have to be package. – User 10482 Aug 02 '21 at 17:52
  • I gues it is possible. But I think it should be easy for you to test it and let us know the result ;-) @User10482 – eNca Aug 03 '21 at 17:53
  • Tried it and didn't work. Was curious if there's some workaround. – User 10482 Aug 03 '21 at 22:22