Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeprecationWarning for imp module triggered when run with -Werror #1403

Closed
johnthagen opened this issue Feb 19, 2016 · 12 comments

Comments

Projects
None yet
7 participants
@johnthagen
Copy link

commented Feb 19, 2016

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.

@RonnyPfannschmidt

This comment has been minimized.

Copy link
Member

commented Jul 19, 2016

closing this one,
the remaining import of the imp module is in a place that handles old python versions

@rpkilby

This comment has been minimized.

Copy link

commented Sep 17, 2016

@RonnyPfannschmidt - is there an alternative way to view deprecations with pytest? The -Wall and Wonce flags aren't sufficient, since they only report the last line of the stack trace. Usually, you would use -Werror, however this causes the test runner to break before the test suite can even run.

As a side note, This now affects the assertion.rewrite module.

@rpkilby

This comment has been minimized.

Copy link

commented Sep 17, 2016

the remaining import of the imp module is in a place that handles old python versions

If the code is only handling old python versions, why is it executing in python 3.5?

@RonnyPfannschmidt

This comment has been minimized.

Copy link
Member

commented Sep 17, 2016

@rpkilby mainly because the code is in place and the import didnt hurt anyone before

@nicoddemus

This comment has been minimized.

Copy link
Member

commented Sep 17, 2016

If the code is only handling old python versions, why is it executing in python 3.5?

It is used by all Python versions actually. In order to get rid of it we have to map the functionality used by imp to importlib (its replacement) and create a compatibility layer because importlib was introduced in 3.1 only.

@rpkilby

This comment has been minimized.

Copy link

commented Sep 18, 2016

Thanks @RonnyPfannschmidt & @nicoddemus.
For anyone else who runs into this, there is pytest-warnings which seems to work well enough.

@nicoddemus

This comment has been minimized.

Copy link
Member

commented Aug 1, 2017

Closing this because I don't think there's any explicit action left to be done here

@nicoddemus nicoddemus closed this Aug 1, 2017

@leezu

This comment has been minimized.

Copy link
Contributor

commented Oct 3, 2017

Shouldn't this stay open until the compatibility layer mentioned by @nicoddemus is introduced?

With

This is pytest version 3.2.2, imported from /usr/lib64/python3.6/site-packages/pytest.py
setuptools registered plugins:
  pytest-timeout-1.2.0 at /usr/lib64/python3.6/site-packages/pytest_timeout.py
  pytest-pep8-1.0.6 at /usr/lib64/python3.6/site-packages/pytest_pep8.py
  pytest-cov-2.5.1 at /usr/lib64/python3.6/site-packages/pytest_cov/plugin.py

_pytest/assertion/rewrite.py uses the imp module, triggering the following warning on every run of one of my programs:

/usr/lib64/python3.6/site-packages/_pytest/assertion/rewrite.py:7: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp            

which I think is annoying enough to at least leave the report open ;)?

@nicoddemus

This comment has been minimized.

Copy link
Member

commented Oct 3, 2017

@leezu thanks that's a good point.

@nicoddemus nicoddemus reopened this Oct 3, 2017

@nicoddemus nicoddemus changed the title PendingDeprecationWarning triggered when run with -Werror PendingDeprecationWarning for imp module triggered when run with -Werror Oct 3, 2017

@Zac-HD

This comment has been minimized.

Copy link
Member

commented Apr 20, 2018

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 alone PYTHONWARNINGS=error) practical.

Instead, we configure warnings in conftest.py:

import warnings
warnings.simplefilter('error')
@tnir

This comment has been minimized.

Copy link

commented May 29, 2018

With Python 3.6, we will get the following warning instead of PendingDeprecationWarning:

DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/site-packages/pytest.py", line 9, in 
    from _pytest.config import (
  File "/usr/local/lib/python3.6/site-packages/_pytest/config.py", line 18, in 
    import _pytest.assertion
  File "/usr/local/lib/python3.6/site-packages/_pytest/assertion/__init__.py", line 9, in 
    from _pytest.assertion import rewrite
  File "/usr/local/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 6, in 
    import imp
  File "/usr/local/lib/python3.6/imp.py", line 33, in 
    DeprecationWarning, stacklevel=2)
DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses

(tested with Python 3.6.5 (python:3.6-alpine3.7 Docker image))

@johnthagen johnthagen changed the title PendingDeprecationWarning for imp module triggered when run with -Werror DeprecationWarning for imp module triggered when run with -Werror Dec 1, 2018

@johnthagen

This comment has been minimized.

Copy link
Author

commented Dec 2, 2018

The most narrow way of suppressing this I've been able to find is:

$ python -Werror -Wignore:::_pytest.assertion.rewrite -m pytest test.py

This will suppress all warnings in _pytest.assertion.rewrite, which is arguably a little brittle if pytest refactors internally but at least it avoids having to globally disable all DeprecationWarnings 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.