pytest: DeprecationWarning for imp module triggered when run with -Werror
Testing with -Werror
is convenient for CI testing so that warnings can be quickly identified and corrected rather than staying dormant.
With Python 3.5.1 if you execute with -Werror
you receive a PendingDeprecationWarning
because imp
has been deprecated since Python 3.4.
Edit: As of Python 3.6 this is now a full
DeprecationWarning
.
The code causing the issue is:
To reproduce:
$ python -Werror -m pytest test.py
Traceback (most recent call last):
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\site-packages\pytest.py", line 16, in <module>
import pytest
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\site-packages\pytest.py", line 27, in <module>
_preloadplugins() # to populate pytest.* namespace so help(pytest) works
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\site-packages\_pytest\config.py", line 75, in _preloadplugins
_preinit.append(get_config())
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\site-packages\_pytest\config.py", line 84, in get_config
pluginmanager.import_plugin(spec)
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\site-packages\_pytest\config.py", line 384, in import_plugin
__import__(importspec)
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\site-packages\_pytest\main.py", line 6, in <module>
import os, sys, imp
File "C:\Users\John Hagen\AppData\Local\Programs\Python\Python35\lib\imp.py", line 33, in <module>
PendingDeprecationWarning, stacklevel=2)
PendingDeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
$ pip -V
pip 8.0.2
$ pip list
...
pytest (2.8.7)
This was tested on Windows 10 x64.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 6
- Comments: 18 (10 by maintainers)
Commits related to this issue
- Merge #86 86: Update pytest to 5.0.1 r=rehandalal a=pyup-bot This PR updates [pytest](https://pypi.org/project/pytest) from **4.6.3** to **5.0.1**. <details> <summary>Changelog</summary> ... — committed to rehandalal/therapist by bors[bot] 5 years ago
- Merge #32 32: Pin pytest to latest version 5.1.3 r=duckinator a=pyup-bot This PR pins [pytest](https://pypi.org/project/pytest) to the latest release **5.1.3**. <details> <summary>Changelog</s... — committed to duckinator/bork by bors[bot] 5 years ago
- Ovverriding DeprecationWarning for the imp module https://github.com/pytest-dev/pytest/issues/1403 — committed to artynet/sphinx by artynet 4 years ago
- Ovverriding DeprecationWarning for the imp module https://github.com/pytest-dev/pytest/issues/1403 — committed to artynet/sphinx by artynet 4 years ago
- Merge #1 1: Initial Update r=aragilar a=pyup-bot This PR sets up pyup.io on this repo and updates all dependencies at once, in a single branch. Subsequent pull requests will update one dependency... — committed to aragilar/pytest-info-collector by bors[bot] 4 years ago
- Merge #1 1: Initial Update r=aragilar a=pyup-bot This PR sets up pyup.io on this repo and updates all dependencies at once, in a single branch. Subsequent pull requests will update one dependency... — committed to aragilar/spaceplot by bors[bot] 4 years ago
- Merge #1 1: Initial Update r=aragilar a=pyup-bot This PR sets up pyup.io on this repo and updates all dependencies at once, in a single branch. Subsequent pull requests will update one dependency... — committed to aragilar/venv_tools by bors[bot] 4 years ago
- Merge #5 5: Initial Update r=aragilar a=pyup-bot This PR sets up pyup.io on this repo and updates all dependencies at once, in a single branch. Subsequent pull requests will update one dependency... — committed to aragilar/root-solver by bors[bot] 4 years ago
Shouldn’t this stay open until the compatibility layer mentioned by @nicoddemus is introduced?
With
_pytest/assertion/rewrite.py
uses the imp module, triggering the following warning on every run of one of my programs:which I think is annoying enough to at least leave the report open 😉?
With Python 3.6, we will get the following warning instead of
PendingDeprecationWarning
:(tested with Python 3.6.5 (python:3.6-alpine3.7 Docker image))
The most narrow way of suppressing this I’ve been able to find is:
This will suppress all warnings in
_pytest.assertion.rewrite
, which is arguably a little brittle ifpytest
refactors internally but at least it avoids having to globally disable allDeprecationWarning
s and miss the opportunity to catch the use of deprecated items in user code.Having this fixed in
pytest
would still be very ideal and would allow downstream users to use-Werror
in a much more straightforward way.@marchfra you can choose to suppress them in pytest:
or additionally update the dependency if it has been updated since inline with pending deprecations
For anyone else who wants to run with warnings as errors: in Hypothesis we found that there are too many warnings in tools to make environment-wide
-Werror
(let alonePYTHONWARNINGS=error
) practical.Instead, we configure warnings in
conftest.py
: