[Python-checkins] bpo-39674: Revert "bpo-25988: Do not expose abstract collection classes in the collections module. (GH-10596)" (GH-18545)

Victor Stinner webhook-mailer at python.org
Tue Feb 18 10:29:01 EST 2020


https://github.com/python/cpython/commit/af5ee3ff610377ef446c2d88bbfcbb3dffaaf0c9
commit: af5ee3ff610377ef446c2d88bbfcbb3dffaaf0c9
branch: master
author: Victor Stinner <vstinner at python.org>
committer: GitHub <noreply at github.com>
date: 2020-02-18T16:28:53+01:00
summary:

bpo-39674: Revert "bpo-25988: Do not expose abstract collection classes in the collections module. (GH-10596)" (GH-18545)

This reverts commit ef092fe9905f61ca27889092ca1248a11aa74498.

Update collections __getattr__() and documentation to defer aliases
removal to Python 3.10.

files:
A Misc/NEWS.d/next/Library/2020-02-18-12-31-24.bpo-39674.S_zqVM.rst
M Doc/library/collections.rst
M Doc/whatsnew/3.9.rst
M Lib/collections/__init__.py

diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index a5e8d04099b22..65cdf34aa4e4f 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -33,7 +33,7 @@ Python's general purpose built-in containers, :class:`dict`, :class:`list`,
 :class:`UserString`     wrapper around string objects for easier string subclassing
 =====================   ====================================================================
 
-.. deprecated-removed:: 3.3 3.9
+.. deprecated-removed:: 3.3 3.10
     Moved :ref:`collections-abstract-base-classes` to the :mod:`collections.abc` module.
     For backwards compatibility, they continue to be visible in this module through
     Python 3.8.
diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index 23f0e4306ee63..f7e279b379f12 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -471,11 +471,6 @@ Removed
   since Python 3.2.
   (Contributed by Victor Stinner in :issue:`38916`.)
 
-* The abstract base classes in :mod:`collections.abc` no longer are
-  exposed in the regular :mod:`collections` module.  This will help
-  create a clearer distinction between the concrete classes and the abstract
-  base classes.
-
 * The undocumented ``sys.callstats()`` function has been removed. Since Python
   3.7, it was deprecated and always returned :const:`None`. It required a special
   build option ``CALL_PROFILE`` which was already removed in Python 3.7.
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index cec6c9781a15e..178cdb1fa5ba0 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -39,6 +39,21 @@
     pass
 
 
+def __getattr__(name):
+    # For backwards compatibility, continue to make the collections ABCs
+    # through Python 3.6 available through the collections module.
+    # Note, no new collections ABCs were added in Python 3.7
+    if name in _collections_abc.__all__:
+        obj = getattr(_collections_abc, name)
+        import warnings
+        warnings.warn("Using or importing the ABCs from 'collections' instead "
+                      "of from 'collections.abc' is deprecated since Python 3.3, "
+                      "and in 3.10 it will stop working",
+                      DeprecationWarning, stacklevel=2)
+        globals()[name] = obj
+        return obj
+    raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
+
 ################################################################################
 ### OrderedDict
 ################################################################################
diff --git a/Misc/NEWS.d/next/Library/2020-02-18-12-31-24.bpo-39674.S_zqVM.rst b/Misc/NEWS.d/next/Library/2020-02-18-12-31-24.bpo-39674.S_zqVM.rst
new file mode 100644
index 0000000000000..1d0e906242ae1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-02-18-12-31-24.bpo-39674.S_zqVM.rst
@@ -0,0 +1,4 @@
+Revert "Do not expose abstract collection classes in the collections module"
+change (bpo-25988). Aliases to ABC like collections.Mapping are kept in
+Python 3.9 to ease transition from Python 2.7, but will be removed in Python
+3.10.



More information about the Python-checkins mailing list