From 7cc28f542f4b1addd6b5a55d3b774ac74de6bed4 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 18 Jul 2019 11:59:59 -0400 Subject: [PATCH] Fix imp warning Fixed use of the deprecated "imp" module, which is used to detect pep3147 availability as well as to locate .pyc files, which started emitting deprecation warnings during the test suite. The warnings were not being emitted earlier during the test suite, the change is possibly due to changes in py.test itself but this is not clear. The check for pep3147 is set to True for any Python version 3.5 or greater now and importlib is used when available. Note that some dependencies such as distutils may still be emitting this warning. Tests are adjusted to accommodate for dependencies that emit the warning as well. Pin pycodestyle - PyCQA/pydocstyle#375 Change-Id: Id7795d581f3ac2172960d31e33ee46b95117e5c4 --- alembic/testing/__init__.py | 1 + alembic/testing/assertions.py | 16 ++++++++++++++++ alembic/util/compat.py | 12 +++++++++--- alembic/util/pyfiles.py | 12 ++++++++++-- docs/build/unreleased/imp_warning.rst | 13 +++++++++++++ tests/test_command.py | 2 ++ tox.ini | 2 ++ 7 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 docs/build/unreleased/imp_warning.rst diff --git a/alembic/testing/__init__.py b/alembic/testing/__init__.py index 765dd7b2..4c704fe5 100644 --- a/alembic/testing/__init__.py +++ b/alembic/testing/__init__.py @@ -1,6 +1,7 @@ from alembic import util # noqa from .assertions import assert_raises # noqa from .assertions import assert_raises_message # noqa +from .assertions import emits_python_deprecation_warning # noqa from .assertions import eq_ # noqa from .assertions import eq_ignore_whitespace # noqa from .assertions import is_ # noqa diff --git a/alembic/testing/assertions.py b/alembic/testing/assertions.py index 196e791c..86540830 100644 --- a/alembic/testing/assertions.py +++ b/alembic/testing/assertions.py @@ -162,6 +162,22 @@ def emits_warning(*messages): return decorate +def emits_python_deprecation_warning(*messages): + """Decorator form of expect_warnings(). + + Note that emits_warning does **not** assert that the warnings + were in fact seen. + + """ + + @decorator + def decorate(fn, *args, **kw): + with _expect_warnings(DeprecationWarning, assert_=False, *messages): + return fn(*args, **kw) + + return decorate + + def emits_warning_on(db, *messages): """Mark a test as emitting a warning on a specific dialect. diff --git a/alembic/util/compat.py b/alembic/util/compat.py index d57df9e3..e0269b75 100644 --- a/alembic/util/compat.py +++ b/alembic/util/compat.py @@ -222,11 +222,17 @@ if py3k: return suffixes def has_pep3147(): - # http://www.python.org/dev/peps/pep-3147/#detecting-pep-3147-availability - import imp + if py35: + return True + else: + # TODO: not sure if we are supporting old versions of Python + # the import here emits a deprecation warning which the test + # suite only catches if imp wasn't imported alreadt + # http://www.python.org/dev/peps/pep-3147/#detecting-pep-3147-availability + import imp - return hasattr(imp, "get_tag") + return hasattr(imp, "get_tag") else: diff --git a/alembic/util/pyfiles.py b/alembic/util/pyfiles.py index 6e9b542a..013b147d 100644 --- a/alembic/util/pyfiles.py +++ b/alembic/util/pyfiles.py @@ -9,6 +9,7 @@ from .compat import get_current_bytecode_suffixes from .compat import has_pep3147 from .compat import load_module_py from .compat import load_module_pyc +from .compat import py35 from .exc import CommandError @@ -54,12 +55,19 @@ def pyc_file_from_path(path): """ if has_pep3147(): - import imp + if py35: + import importlib - candidate = imp.cache_from_source(path) + candidate = importlib.util.cache_from_source(path) + else: + import imp + + candidate = imp.cache_from_source(path) if os.path.exists(candidate): return candidate + # even for pep3147, fall back to the old way of finding .pyc files, + # to support sourceless operation filepath, ext = os.path.splitext(path) for ext in get_current_bytecode_suffixes(): if os.path.exists(filepath + ext): diff --git a/docs/build/unreleased/imp_warning.rst b/docs/build/unreleased/imp_warning.rst new file mode 100644 index 00000000..6fc87ef9 --- /dev/null +++ b/docs/build/unreleased/imp_warning.rst @@ -0,0 +1,13 @@ +.. change:: + :tag: bug, py3k + + Fixed use of the deprecated "imp" module, which is used to detect pep3147 + availability as well as to locate .pyc files, which started emitting + deprecation warnings during the test suite. The warnings were not being + emitted earlier during the test suite, the change is possibly due to + changes in py.test itself but this is not clear. The check for pep3147 is + set to True for any Python version 3.5 or greater now and importlib is used + when available. Note that some dependencies such as distutils may still be + emitting this warning. Tests are adjusted to accommodate for dependencies + that emit the warning as well. + diff --git a/tests/test_command.py b/tests/test_command.py index ff80bbf1..653028ff 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -8,6 +8,7 @@ from sqlalchemy import exc as sqla_exc from alembic import command from alembic import config +from alembic import testing from alembic import util from alembic.script import ScriptDirectory from alembic.testing import assert_raises @@ -677,6 +678,7 @@ class EditTest(TestBase): command.edit(self.cfg, self.b[0:3]) edit.assert_called_with(expected_call_arg) + @testing.emits_python_deprecation_warning("the imp module is deprecated") def test_edit_with_missing_editor(self): with mock.patch("editor.edit") as edit_mock: edit_mock.side_effect = OSError("file not found") diff --git a/tox.ini b/tox.ini index 2b238f49..bfe1d6a7 100644 --- a/tox.ini +++ b/tox.ini @@ -28,6 +28,7 @@ deps=pytest!=3.9.1,!=3.9.2 cov: pytest-cov + usedevelop= cov: True @@ -65,6 +66,7 @@ deps= flake8-builtins flake8-docstrings flake8-rst-docstrings + pydocstyle<4.0.0 # used by flake8-rst-docstrings pygments commands = flake8 ./alembic/ ./tests/ setup.py docs/build/conf.py -- 2.47.2