Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid getting DeprecationWarning from inside dependencies with pytest?

Tags:

python

pytest

I often get lots of deprecations from libraries that are outside my control and I do not want to pollute test executions with them.

How can I avoid this without risking to disable deprecations from my own code?

Example:

================================================================================ warnings summary ==================================================================================
.tox/py27-ansible25-unit/lib/python3.6/site-packages/toml/decoder.py:47
  /Users/ssbarnea/os/molecule/.tox/py27-ansible25-unit/lib/python3.6/site-packages/toml/decoder.py:47: DeprecationWarning: invalid escape sequence \.
    TIME_RE = re.compile("([0-9]{2}):([0-9]{2}):([0-9]{2})(\.([0-9]{3,6}))?")

.tox/py27-ansible25-unit/lib/python3.6/site-packages/sh.py:424
  /Users/ssbarnea/os/molecule/.tox/py27-ansible25-unit/lib/python3.6/site-packages/sh.py:424: DeprecationWarning: invalid escape sequence \d
    rc_exc_regex = re.compile("(ErrorReturnCode|SignalException)_((\d+)|SIG[a-zA-Z]+)")

.tox/py27-ansible25-unit/lib/python3.6/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:152
  /Users/ssbarnea/os/molecule/.tox/py27-ansible25-unit/lib/python3.6/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py:152: DeprecationWarning: invalid escape sequence \*
like image 975
sorin Avatar asked Oct 15 '25 14:10

sorin


1 Answers

I won't repeat pytest docs on the general topic of warnings capturing, for the sake of reference: Warnings Capture. From here, you can narrow the warnings captured by stricter filters. The filter format is

{action}:{message}:{category}:{module}:{lineno}

with elements skippable. Examples to paste in your pytest.ini, from general to specific:

ignore everything

[pytest]
filterwarnings =
    ignore:

ignore all DeprecationWarnings

[pytest]
filterwarnings =
    ignore::DeprecationWarning

ignore all DeprecationWarnings with invalid escape sequence in message

[pytest]
filterwarnings =
    ignore:.*invalid escape sequence.*:DeprecationWarning

ignore DeprecationWarnings only in toml.decoder module

[pytest]
filterwarnings =
    ignore::DeprecationWarning:toml.decoder

ignore DeprecationWarnings only in toml.decoder module on line 47

[pytest]
filterwarnings =
    ignore::DeprecationWarning:toml.decoder:47

ignore DeprecationWarnings only in toml.decoder module, only on line 47 and only with invalid escape sequence in message:

[pytest]
filterwarnings =
    ignore:.*invalid escape sequence.*:DeprecationWarning:toml.decoder:47
like image 118
hoefling Avatar answered Oct 18 '25 08:10

hoefling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!