mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-30144: Import collections ABC from collections.abc rather than collections. (#1263)
This commit is contained in:
parent
9eb5ca0774
commit
2e576f5aec
22 changed files with 90 additions and 85 deletions
|
@ -1,6 +1,7 @@
|
|||
import abc
|
||||
import builtins
|
||||
import collections
|
||||
import collections.abc
|
||||
import copy
|
||||
from itertools import permutations
|
||||
import pickle
|
||||
|
@ -910,7 +911,7 @@ class TestCmpToKey:
|
|||
key = self.cmp_to_key(mycmp)
|
||||
k = key(10)
|
||||
self.assertRaises(TypeError, hash, k)
|
||||
self.assertNotIsInstance(k, collections.Hashable)
|
||||
self.assertNotIsInstance(k, collections.abc.Hashable)
|
||||
|
||||
|
||||
@unittest.skipUnless(c_functools, 'requires the C _functools module')
|
||||
|
@ -1707,7 +1708,7 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
|
||||
def test_compose_mro(self):
|
||||
# None of the examples in this test depend on haystack ordering.
|
||||
c = collections
|
||||
c = collections.abc
|
||||
mro = functools._compose_mro
|
||||
bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set]
|
||||
for haystack in permutations(bases):
|
||||
|
@ -1715,10 +1716,10 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
self.assertEqual(m, [dict, c.MutableMapping, c.Mapping,
|
||||
c.Collection, c.Sized, c.Iterable,
|
||||
c.Container, object])
|
||||
bases = [c.Container, c.Mapping, c.MutableMapping, c.OrderedDict]
|
||||
bases = [c.Container, c.Mapping, c.MutableMapping, collections.OrderedDict]
|
||||
for haystack in permutations(bases):
|
||||
m = mro(c.ChainMap, haystack)
|
||||
self.assertEqual(m, [c.ChainMap, c.MutableMapping, c.Mapping,
|
||||
m = mro(collections.ChainMap, haystack)
|
||||
self.assertEqual(m, [collections.ChainMap, c.MutableMapping, c.Mapping,
|
||||
c.Collection, c.Sized, c.Iterable,
|
||||
c.Container, object])
|
||||
|
||||
|
@ -1728,39 +1729,39 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
# test_mro_conflicts).
|
||||
bases = [c.Container, c.Sized, str]
|
||||
for haystack in permutations(bases):
|
||||
m = mro(c.defaultdict, [c.Sized, c.Container, str])
|
||||
self.assertEqual(m, [c.defaultdict, dict, c.Sized, c.Container,
|
||||
object])
|
||||
m = mro(collections.defaultdict, [c.Sized, c.Container, str])
|
||||
self.assertEqual(m, [collections.defaultdict, dict, c.Sized,
|
||||
c.Container, object])
|
||||
|
||||
# MutableSequence below is registered directly on D. In other words, it
|
||||
# precedes MutableMapping which means single dispatch will always
|
||||
# choose MutableSequence here.
|
||||
class D(c.defaultdict):
|
||||
class D(collections.defaultdict):
|
||||
pass
|
||||
c.MutableSequence.register(D)
|
||||
bases = [c.MutableSequence, c.MutableMapping]
|
||||
for haystack in permutations(bases):
|
||||
m = mro(D, bases)
|
||||
self.assertEqual(m, [D, c.MutableSequence, c.Sequence, c.Reversible,
|
||||
c.defaultdict, dict, c.MutableMapping, c.Mapping,
|
||||
collections.defaultdict, dict, c.MutableMapping, c.Mapping,
|
||||
c.Collection, c.Sized, c.Iterable, c.Container,
|
||||
object])
|
||||
|
||||
# Container and Callable are registered on different base classes and
|
||||
# a generic function supporting both should always pick the Callable
|
||||
# implementation if a C instance is passed.
|
||||
class C(c.defaultdict):
|
||||
class C(collections.defaultdict):
|
||||
def __call__(self):
|
||||
pass
|
||||
bases = [c.Sized, c.Callable, c.Container, c.Mapping]
|
||||
for haystack in permutations(bases):
|
||||
m = mro(C, haystack)
|
||||
self.assertEqual(m, [C, c.Callable, c.defaultdict, dict, c.Mapping,
|
||||
self.assertEqual(m, [C, c.Callable, collections.defaultdict, dict, c.Mapping,
|
||||
c.Collection, c.Sized, c.Iterable,
|
||||
c.Container, object])
|
||||
|
||||
def test_register_abc(self):
|
||||
c = collections
|
||||
c = collections.abc
|
||||
d = {"a": "b"}
|
||||
l = [1, 2, 3]
|
||||
s = {object(), None}
|
||||
|
@ -1786,7 +1787,7 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
self.assertEqual(g(s), "sized")
|
||||
self.assertEqual(g(f), "sized")
|
||||
self.assertEqual(g(t), "sized")
|
||||
g.register(c.ChainMap, lambda obj: "chainmap")
|
||||
g.register(collections.ChainMap, lambda obj: "chainmap")
|
||||
self.assertEqual(g(d), "mutablemapping") # irrelevant ABCs registered
|
||||
self.assertEqual(g(l), "sized")
|
||||
self.assertEqual(g(s), "sized")
|
||||
|
@ -1854,7 +1855,7 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
self.assertEqual(g(t), "tuple")
|
||||
|
||||
def test_c3_abc(self):
|
||||
c = collections
|
||||
c = collections.abc
|
||||
mro = functools._c3_mro
|
||||
class A(object):
|
||||
pass
|
||||
|
@ -1895,7 +1896,7 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
self.assertEqual(fun(aa), 'fun A')
|
||||
|
||||
def test_mro_conflicts(self):
|
||||
c = collections
|
||||
c = collections.abc
|
||||
@functools.singledispatch
|
||||
def g(arg):
|
||||
return "base"
|
||||
|
@ -1956,7 +1957,7 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
# MutableMapping's bases implicit as well from defaultdict's
|
||||
# perspective.
|
||||
with self.assertRaises(RuntimeError) as re_two:
|
||||
h(c.defaultdict(lambda: 0))
|
||||
h(collections.defaultdict(lambda: 0))
|
||||
self.assertIn(
|
||||
str(re_two.exception),
|
||||
(("Ambiguous dispatch: <class 'collections.abc.Container'> "
|
||||
|
@ -1964,7 +1965,7 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
("Ambiguous dispatch: <class 'collections.abc.Sized'> "
|
||||
"or <class 'collections.abc.Container'>")),
|
||||
)
|
||||
class R(c.defaultdict):
|
||||
class R(collections.defaultdict):
|
||||
pass
|
||||
c.MutableSequence.register(R)
|
||||
@functools.singledispatch
|
||||
|
@ -2041,7 +2042,7 @@ class TestSingleDispatch(unittest.TestCase):
|
|||
_orig_wkd = functools.WeakKeyDictionary
|
||||
td = TracingDict()
|
||||
functools.WeakKeyDictionary = lambda: td
|
||||
c = collections
|
||||
c = collections.abc
|
||||
@functools.singledispatch
|
||||
def g(arg):
|
||||
return "base"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue