]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fix imp warning
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Jul 2019 15:59:59 +0000 (11:59 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Jul 2019 17:15:40 +0000 (13:15 -0400)
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
alembic/testing/assertions.py
alembic/util/compat.py
alembic/util/pyfiles.py
docs/build/unreleased/imp_warning.rst [new file with mode: 0644]
tests/test_command.py
tox.ini

index 765dd7b24846a771d3cf466d77dc758dd9957c9e..4c704fe51f6ee8f931f6fc34af58428b8f6d128e 100644 (file)
@@ -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
index 196e791c29ef0db66efc869e8b4b8c19b0138e22..865408300e63aa81e8fb43e3b8a842c820336a5f 100644 (file)
@@ -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.
 
index d57df9e30e9561b55a5d72f3965af420962e2777..e0269b75bb58e3d74efe986516a3da01aae893cb 100644 (file)
@@ -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:
index 6e9b542a588df7d6116c7aa9e62e718fbc6dfc7d..013b147d061489b409abb19450d4f6373771f6ad 100644 (file)
@@ -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 (file)
index 0000000..6fc87ef
--- /dev/null
@@ -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.
+
index ff80bbf1b6be98fb12902ea2762e524a6b61eb12..653028ff7f034697f2bf63f09a853cbe7a2392fd 100644 (file)
@@ -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 2b238f49730bf2220c9bb96eb0ee8feed9a5dd08..bfe1d6a762ac7962b94b56b8147e291e32b9d69c 100644 (file)
--- 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