]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix collections ABC access before Python 3.8
authorNathaniel Knight <nknight@cfenet.ubc.ca>
Wed, 1 Aug 2018 01:59:47 +0000 (21:59 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 1 Aug 2018 16:30:09 +0000 (12:30 -0400)
Started importing "collections" from "collections.abc" under Python 3.3 and
greater for Python 3.8 compatibility.  Pull request courtesy Nathaniel
Knight.

In Python 3.3, the abstract base classes (Iterable, Mapping, etc.)
were moved from the `collections` module and put in the
`collections.abc` module. They remain in the `collections` module for
backwards compatibility, and will until Python 3.8.

This commit adds a variable (`collections_abc`) to the `util/compat`
module, which will be the `collections` module for Python < 3.3 and
before, or the `collections.abc` module for Python >= 3.3. It also
uses the new variable, getting rid of some deprecation warnings that
were seen when running under Python 3.7.

Change-Id: I2d1c0ef97c8ecac7af152cc56263422a40faa6bb
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/464

doc/build/changelog/unreleased_12/pr464.rst [new file with mode: 0644]
lib/sqlalchemy/util/__init__.py
lib/sqlalchemy/util/_collections.py
lib/sqlalchemy/util/compat.py
test/sql/test_operators.py

diff --git a/doc/build/changelog/unreleased_12/pr464.rst b/doc/build/changelog/unreleased_12/pr464.rst
new file mode 100644 (file)
index 0000000..7f84c19
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: bug, py3k
+
+    Started importing "collections" from "collections.abc" under Python 3.3 and
+    greater for Python 3.8 compatibility.  Pull request courtesy Nathaniel
+    Knight.
index 2a2d5cfc6ae035117736fe8594697afe0a569ffa..031376d7809241dc68e4c0f5015bf831384ac0fa 100644 (file)
@@ -21,7 +21,7 @@ from ._collections import KeyedTuple, ImmutableContainer, immutabledict, \
     UniqueAppender, PopulateDict, EMPTY_SET, to_list, to_set, \
     to_column_set, update_copy, flatten_iterator, has_intersection, \
     LRUCache, ScopedRegistry, ThreadLocalRegistry, WeakSequence, \
-    coerce_generator_arg, lightweight_named_tuple
+    coerce_generator_arg, lightweight_named_tuple, collections_abc
 
 from .langhelpers import iterate_attributes, class_hierarchy, \
     portable_instancemethod, unbound_method_to_callable, \
index cc084a7c7b4baa48012cf897158ca200bf782106..d8e389c61e33fafa614c6f471d4c91008a94b69c 100644 (file)
@@ -11,10 +11,10 @@ from __future__ import absolute_import
 import weakref
 import operator
 from .compat import threading, itertools_filterfalse, string_types, \
-    binary_types
+    binary_types, collections_abc
 from . import py2k
 import types
-import collections
+
 
 EMPTY_SET = frozenset()
 
@@ -795,7 +795,7 @@ def coerce_generator_arg(arg):
 def to_list(x, default=None):
     if x is None:
         return default
-    if not isinstance(x, collections.Iterable) or \
+    if not isinstance(x, collections_abc.Iterable) or \
             isinstance(x, string_types + binary_types):
         return [x]
     elif isinstance(x, list):
index bc0c3e6e0639861a2788e97592dc8e32774ba4e2..02be2b3b9214df2bc6ea0897bbdc80f69f6e0869 100644 (file)
@@ -327,3 +327,12 @@ def nested(*managers):
                 exc = sys.exc_info()
         if exc != (None, None, None):
             reraise(exc[0], exc[1], exc[2])
+
+
+# Fix deprecation of accessing ABCs straight from collections module
+# (which will stop working in 3.8).
+if py33:
+    import collections.abc as collections_abc
+else:
+    import collections as collections_abc
+
index f5446a856d4a2aa9134fe245c51d1474c68ea8ff..f4b3cf58409f1b1f4f558943bf4c8d4b6b631cf7 100644 (file)
@@ -636,7 +636,7 @@ class ExtensionOperatorTest(fixtures.TestBase, testing.AssertsCompiledSQL):
                     return self.op("->")(index)
 
         col = Column('x', MyType())
-        assert not isinstance(col, collections.Iterable)
+        assert not isinstance(col, util.collections_abc.Iterable)
 
     def test_lshift(self):
         class MyType(UserDefinedType):