Skip to content

Commit 2e576f5

Browse files
bpo-30144: Import collections ABC from collections.abc rather than collections. (#1263)
1 parent 9eb5ca0 commit 2e576f5

22 files changed

+92
-87
lines changed

Doc/library/http.client.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ also send your request step by step, by using the four functions below.
372372
Section 3.3.1. How the data is encoded is dependent on the type of
373373
*message_body*. If *message_body* implements the :ref:`buffer interface
374374
<bufferobjects>` the encoding will result in a single chunk.
375-
If *message_body* is a :class:`collections.Iterable`, each iteration
375+
If *message_body* is a :class:`collections.abc.Iterable`, each iteration
376376
of *message_body* will result in a chunk. If *message_body* is a
377377
:term:`file object`, each call to ``.read()`` will result in a chunk.
378378
The method automatically signals the end of the chunk-encoded data

Doc/reference/datamodel.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ Basic customization
13751375
:meth:`__hash__` method of a class is ``None``, instances of the class will
13761376
raise an appropriate :exc:`TypeError` when a program attempts to retrieve
13771377
their hash value, and will also be correctly identified as unhashable when
1378-
checking ``isinstance(obj, collections.Hashable)``.
1378+
checking ``isinstance(obj, collections.abc.Hashable)``.
13791379

13801380
If a class that overrides :meth:`__eq__` needs to retain the implementation
13811381
of :meth:`__hash__` from a parent class, the interpreter must be told this
@@ -1385,7 +1385,7 @@ Basic customization
13851385
support, it should include ``__hash__ = None`` in the class definition.
13861386
A class which defines its own :meth:`__hash__` that explicitly raises
13871387
a :exc:`TypeError` would be incorrectly identified as hashable by
1388-
an ``isinstance(obj, collections.Hashable)`` call.
1388+
an ``isinstance(obj, collections.abc.Hashable)`` call.
13891389

13901390

13911391
.. note::
@@ -1981,7 +1981,7 @@ range of items. It is also recommended that mappings provide the methods
19811981
:meth:`keys`, :meth:`values`, :meth:`items`, :meth:`get`, :meth:`clear`,
19821982
:meth:`setdefault`, :meth:`pop`, :meth:`popitem`, :meth:`!copy`, and
19831983
:meth:`update` behaving similar to those for Python's standard dictionary
1984-
objects. The :mod:`collections` module provides a
1984+
objects. The :mod:`collections.abc` module provides a
19851985
:class:`~collections.abc.MutableMapping`
19861986
abstract base class to help create those methods from a base set of
19871987
:meth:`__getitem__`, :meth:`__setitem__`, :meth:`__delitem__`, and :meth:`keys`.

Lib/asyncio/base_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515

1616
import collections
17+
import collections.abc
1718
import concurrent.futures
1819
import heapq
1920
import itertools
@@ -1001,7 +1002,7 @@ def create_server(self, protocol_factory, host=None, port=None,
10011002
if host == '':
10021003
hosts = [None]
10031004
elif (isinstance(host, str) or
1004-
not isinstance(host, collections.Iterable)):
1005+
not isinstance(host, collections.abc.Iterable)):
10051006
hosts = [host]
10061007
else:
10071008
hosts = host

Lib/cgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# =======
3333

3434
from io import StringIO, BytesIO, TextIOWrapper
35-
from collections import Mapping
35+
from collections.abc import Mapping
3636
import sys
3737
import os
3838
import urllib.parse

Lib/dbm/dumb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
import ast as _ast
2525
import io as _io
2626
import os as _os
27-
import collections
27+
import collections.abc
2828

2929
__all__ = ["error", "open"]
3030

3131
_BLOCKSIZE = 512
3232

3333
error = OSError
3434

35-
class _Database(collections.MutableMapping):
35+
class _Database(collections.abc.MutableMapping):
3636

3737
# The on-disk directory and data files can remain in mutually
3838
# inconsistent states for an arbitrarily long time (see comments

Lib/http/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import io
7575
import re
7676
import socket
77-
import collections
77+
import collections.abc
7878
from urllib.parse import urlsplit
7979

8080
# HTTPMessage, parse_headers(), and the HTTP status code constants are
@@ -977,7 +977,7 @@ def send(self, data):
977977
try:
978978
self.sock.sendall(data)
979979
except TypeError:
980-
if isinstance(data, collections.Iterable):
980+
if isinstance(data, collections.abc.Iterable):
981981
for d in data:
982982
self.sock.sendall(d)
983983
else:

Lib/idlelib/pyparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections import Mapping
1+
from collections.abc import Mapping
22
import re
33
import sys
44

Lib/lib2to3/fixes/fix_operator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
operator.irepeat(obj, n) -> operator.imul(obj, n)
1010
"""
1111

12-
import collections
12+
import collections.abc
1313

1414
# Local imports
1515
from lib2to3 import fixer_base
@@ -88,7 +88,7 @@ def _handle_type2abc(self, node, results, module, abc):
8888

8989
def _check_method(self, node, results):
9090
method = getattr(self, "_" + results["method"][0].value)
91-
if isinstance(method, collections.Callable):
91+
if isinstance(method, collections.abc.Callable):
9292
if "module" in results:
9393
return method
9494
else:

Lib/locale.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import encodings
1515
import encodings.aliases
1616
import re
17-
import collections
17+
import collections.abc
1818
from builtins import str as _builtin_str
1919
import functools
2020
import warnings
@@ -215,7 +215,7 @@ def format_string(f, val, grouping=False, monetary=False):
215215
percents = list(_percent_re.finditer(f))
216216
new_f = _percent_re.sub('%s', f)
217217

218-
if isinstance(val, collections.Mapping):
218+
if isinstance(val, collections.abc.Mapping):
219219
new_val = []
220220
for perc in percents:
221221
if perc.group()[-1]=='%':

Lib/logging/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
To use, simply 'import logging' and log away!
2424
"""
2525

26-
import sys, os, time, io, traceback, warnings, weakref, collections
26+
import sys, os, time, io, traceback, warnings, weakref, collections.abc
2727

2828
from string import Template
2929

@@ -273,8 +273,8 @@ def __init__(self, name, level, pathname, lineno,
273273
# to hasattr(args[0], '__getitem__'). However, the docs on string
274274
# formatting still seem to suggest a mapping object is required.
275275
# Thus, while not removing the isinstance check, it does now look
276-
# for collections.Mapping rather than, as before, dict.
277-
if (args and len(args) == 1 and isinstance(args[0], collections.Mapping)
276+
# for collections.abc.Mapping rather than, as before, dict.
277+
if (args and len(args) == 1 and isinstance(args[0], collections.abc.Mapping)
278278
and args[0]):
279279
args = args[0]
280280
self.args = args

Lib/pathlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import posixpath
77
import re
88
import sys
9-
from collections import Sequence
9+
from collections.abc import Sequence
1010
from errno import EINVAL, ENOENT, ENOTDIR
1111
from operator import attrgetter
1212
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO

Lib/selectors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77

88
from abc import ABCMeta, abstractmethod
9-
from collections import namedtuple, Mapping
9+
from collections import namedtuple
10+
from collections.abc import Mapping
1011
import math
1112
import select
1213
import sys

Lib/shelve.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
from pickle import Pickler, Unpickler
6060
from io import BytesIO
6161

62-
import collections
62+
import collections.abc
6363

6464
__all__ = ["Shelf", "BsdDbShelf", "DbfilenameShelf", "open"]
6565

66-
class _ClosedDict(collections.MutableMapping):
66+
class _ClosedDict(collections.abc.MutableMapping):
6767
'Marker for a closed dict. Access attempts raise a ValueError.'
6868

6969
def closed(self, *args):
@@ -74,7 +74,7 @@ def __repr__(self):
7474
return '<Closed Dictionary>'
7575

7676

77-
class Shelf(collections.MutableMapping):
77+
class Shelf(collections.abc.MutableMapping):
7878
"""Base class for shelf implementations.
7979
8080
This is initialized with a dictionary-like object.

Lib/test/test_dictviews.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import collections
1+
import collections.abc
22
import copy
33
import pickle
44
import unittest
@@ -249,23 +249,23 @@ def test_pickle(self):
249249
def test_abc_registry(self):
250250
d = dict(a=1)
251251

252-
self.assertIsInstance(d.keys(), collections.KeysView)
253-
self.assertIsInstance(d.keys(), collections.MappingView)
254-
self.assertIsInstance(d.keys(), collections.Set)
255-
self.assertIsInstance(d.keys(), collections.Sized)
256-
self.assertIsInstance(d.keys(), collections.Iterable)
257-
self.assertIsInstance(d.keys(), collections.Container)
258-
259-
self.assertIsInstance(d.values(), collections.ValuesView)
260-
self.assertIsInstance(d.values(), collections.MappingView)
261-
self.assertIsInstance(d.values(), collections.Sized)
262-
263-
self.assertIsInstance(d.items(), collections.ItemsView)
264-
self.assertIsInstance(d.items(), collections.MappingView)
265-
self.assertIsInstance(d.items(), collections.Set)
266-
self.assertIsInstance(d.items(), collections.Sized)
267-
self.assertIsInstance(d.items(), collections.Iterable)
268-
self.assertIsInstance(d.items(), collections.Container)
252+
self.assertIsInstance(d.keys(), collections.abc.KeysView)
253+
self.assertIsInstance(d.keys(), collections.abc.MappingView)
254+
self.assertIsInstance(d.keys(), collections.abc.Set)
255+
self.assertIsInstance(d.keys(), collections.abc.Sized)
256+
self.assertIsInstance(d.keys(), collections.abc.Iterable)
257+
self.assertIsInstance(d.keys(), collections.abc.Container)
258+
259+
self.assertIsInstance(d.values(), collections.abc.ValuesView)
260+
self.assertIsInstance(d.values(), collections.abc.MappingView)
261+
self.assertIsInstance(d.values(), collections.abc.Sized)
262+
263+
self.assertIsInstance(d.items(), collections.abc.ItemsView)
264+
self.assertIsInstance(d.items(), collections.abc.MappingView)
265+
self.assertIsInstance(d.items(), collections.abc.Set)
266+
self.assertIsInstance(d.items(), collections.abc.Sized)
267+
self.assertIsInstance(d.items(), collections.abc.Iterable)
268+
self.assertIsInstance(d.items(), collections.abc.Container)
269269

270270

271271
if __name__ == "__main__":

Lib/test/test_functools.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import abc
22
import builtins
33
import collections
4+
import collections.abc
45
import copy
56
from itertools import permutations
67
import pickle
@@ -910,7 +911,7 @@ def mycmp(x, y):
910911
key = self.cmp_to_key(mycmp)
911912
k = key(10)
912913
self.assertRaises(TypeError, hash, k)
913-
self.assertNotIsInstance(k, collections.Hashable)
914+
self.assertNotIsInstance(k, collections.abc.Hashable)
914915

915916

916917
@unittest.skipUnless(c_functools, 'requires the C _functools module')
@@ -1707,18 +1708,18 @@ def _(obj):
17071708

17081709
def test_compose_mro(self):
17091710
# None of the examples in this test depend on haystack ordering.
1710-
c = collections
1711+
c = collections.abc
17111712
mro = functools._compose_mro
17121713
bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set]
17131714
for haystack in permutations(bases):
17141715
m = mro(dict, haystack)
17151716
self.assertEqual(m, [dict, c.MutableMapping, c.Mapping,
17161717
c.Collection, c.Sized, c.Iterable,
17171718
c.Container, object])
1718-
bases = [c.Container, c.Mapping, c.MutableMapping, c.OrderedDict]
1719+
bases = [c.Container, c.Mapping, c.MutableMapping, collections.OrderedDict]
17191720
for haystack in permutations(bases):
1720-
m = mro(c.ChainMap, haystack)
1721-
self.assertEqual(m, [c.ChainMap, c.MutableMapping, c.Mapping,
1721+
m = mro(collections.ChainMap, haystack)
1722+
self.assertEqual(m, [collections.ChainMap, c.MutableMapping, c.Mapping,
17221723
c.Collection, c.Sized, c.Iterable,
17231724
c.Container, object])
17241725

@@ -1728,39 +1729,39 @@ def test_compose_mro(self):
17281729
# test_mro_conflicts).
17291730
bases = [c.Container, c.Sized, str]
17301731
for haystack in permutations(bases):
1731-
m = mro(c.defaultdict, [c.Sized, c.Container, str])
1732-
self.assertEqual(m, [c.defaultdict, dict, c.Sized, c.Container,
1733-
object])
1732+
m = mro(collections.defaultdict, [c.Sized, c.Container, str])
1733+
self.assertEqual(m, [collections.defaultdict, dict, c.Sized,
1734+
c.Container, object])
17341735

17351736
# MutableSequence below is registered directly on D. In other words, it
17361737
# precedes MutableMapping which means single dispatch will always
17371738
# choose MutableSequence here.
1738-
class D(c.defaultdict):
1739+
class D(collections.defaultdict):
17391740
pass
17401741
c.MutableSequence.register(D)
17411742
bases = [c.MutableSequence, c.MutableMapping]
17421743
for haystack in permutations(bases):
17431744
m = mro(D, bases)
17441745
self.assertEqual(m, [D, c.MutableSequence, c.Sequence, c.Reversible,
1745-
c.defaultdict, dict, c.MutableMapping, c.Mapping,
1746+
collections.defaultdict, dict, c.MutableMapping, c.Mapping,
17461747
c.Collection, c.Sized, c.Iterable, c.Container,
17471748
object])
17481749

17491750
# Container and Callable are registered on different base classes and
17501751
# a generic function supporting both should always pick the Callable
17511752
# implementation if a C instance is passed.
1752-
class C(c.defaultdict):
1753+
class C(collections.defaultdict):
17531754
def __call__(self):
17541755
pass
17551756
bases = [c.Sized, c.Callable, c.Container, c.Mapping]
17561757
for haystack in permutations(bases):
17571758
m = mro(C, haystack)
1758-
self.assertEqual(m, [C, c.Callable, c.defaultdict, dict, c.Mapping,
1759+
self.assertEqual(m, [C, c.Callable, collections.defaultdict, dict, c.Mapping,
17591760
c.Collection, c.Sized, c.Iterable,
17601761
c.Container, object])
17611762

17621763
def test_register_abc(self):
1763-
c = collections
1764+
c = collections.abc
17641765
d = {"a": "b"}
17651766
l = [1, 2, 3]
17661767
s = {object(), None}
@@ -1786,7 +1787,7 @@ def g(obj):
17861787
self.assertEqual(g(s), "sized")
17871788
self.assertEqual(g(f), "sized")
17881789
self.assertEqual(g(t), "sized")
1789-
g.register(c.ChainMap, lambda obj: "chainmap")
1790+
g.register(collections.ChainMap, lambda obj: "chainmap")
17901791
self.assertEqual(g(d), "mutablemapping") # irrelevant ABCs registered
17911792
self.assertEqual(g(l), "sized")
17921793
self.assertEqual(g(s), "sized")
@@ -1854,7 +1855,7 @@ def g(obj):
18541855
self.assertEqual(g(t), "tuple")
18551856

18561857
def test_c3_abc(self):
1857-
c = collections
1858+
c = collections.abc
18581859
mro = functools._c3_mro
18591860
class A(object):
18601861
pass
@@ -1895,7 +1896,7 @@ def _(a):
18951896
self.assertEqual(fun(aa), 'fun A')
18961897

18971898
def test_mro_conflicts(self):
1898-
c = collections
1899+
c = collections.abc
18991900
@functools.singledispatch
19001901
def g(arg):
19011902
return "base"
@@ -1956,15 +1957,15 @@ def _(arg):
19561957
# MutableMapping's bases implicit as well from defaultdict's
19571958
# perspective.
19581959
with self.assertRaises(RuntimeError) as re_two:
1959-
h(c.defaultdict(lambda: 0))
1960+
h(collections.defaultdict(lambda: 0))
19601961
self.assertIn(
19611962
str(re_two.exception),
19621963
(("Ambiguous dispatch: <class 'collections.abc.Container'> "
19631964
"or <class 'collections.abc.Sized'>"),
19641965
("Ambiguous dispatch: <class 'collections.abc.Sized'> "
19651966
"or <class 'collections.abc.Container'>")),
19661967
)
1967-
class R(c.defaultdict):
1968+
class R(collections.defaultdict):
19681969
pass
19691970
c.MutableSequence.register(R)
19701971
@functools.singledispatch
@@ -2041,7 +2042,7 @@ def clear(self):
20412042
_orig_wkd = functools.WeakKeyDictionary
20422043
td = TracingDict()
20432044
functools.WeakKeyDictionary = lambda: td
2044-
c = collections
2045+
c = collections.abc
20452046
@functools.singledispatch
20462047
def g(arg):
20472048
return "base"

Lib/test/test_hash.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
import unittest
1010
from test.support.script_helper import assert_python_ok
11-
from collections import Hashable
11+
from collections.abc import Hashable
1212

1313
IS_64BIT = sys.maxsize > 2**32
1414

0 commit comments

Comments
 (0)