Skip to content

Commit 0a2abdf

Browse files
bpo-30143: 2to3 now generates a code that uses abstract collection classes (#1262)
from collections.abc rather than collections.
1 parent a7368ac commit 0a2abdf

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

Doc/library/2to3.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,20 @@ and off individually. They are described here in more detail.
345345
346346
Converts calls to various functions in the :mod:`operator` module to other,
347347
but equivalent, function calls. When needed, the appropriate ``import``
348-
statements are added, e.g. ``import collections``. The following mapping
348+
statements are added, e.g. ``import collections.abc``. The following mapping
349349
are made:
350350
351-
================================== ==========================================
351+
================================== =============================================
352352
From To
353-
================================== ==========================================
353+
================================== =============================================
354354
``operator.isCallable(obj)`` ``hasattr(obj, '__call__')``
355355
``operator.sequenceIncludes(obj)`` ``operator.contains(obj)``
356-
``operator.isSequenceType(obj)`` ``isinstance(obj, collections.Sequence)``
357-
``operator.isMappingType(obj)`` ``isinstance(obj, collections.Mapping)``
356+
``operator.isSequenceType(obj)`` ``isinstance(obj, collections.abc.Sequence)``
357+
``operator.isMappingType(obj)`` ``isinstance(obj, collections.abc.Mapping)``
358358
``operator.isNumberType(obj)`` ``isinstance(obj, numbers.Number)``
359359
``operator.repeat(obj, n)`` ``operator.mul(obj, n)``
360360
``operator.irepeat(obj, n)`` ``operator.imul(obj, n)``
361-
================================== ==========================================
361+
================================== =============================================
362362
363363
.. 2to3fixer:: paren
364364

Lib/lib2to3/fixes/fix_operator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
operator.isCallable(obj) -> hasattr(obj, '__call__')
44
operator.sequenceIncludes(obj) -> operator.contains(obj)
5-
operator.isSequenceType(obj) -> isinstance(obj, collections.Sequence)
6-
operator.isMappingType(obj) -> isinstance(obj, collections.Mapping)
5+
operator.isSequenceType(obj) -> isinstance(obj, collections.abc.Sequence)
6+
operator.isMappingType(obj) -> isinstance(obj, collections.abc.Mapping)
77
operator.isNumberType(obj) -> isinstance(obj, numbers.Number)
88
operator.repeat(obj, n) -> operator.mul(obj, n)
99
operator.irepeat(obj, n) -> operator.imul(obj, n)
@@ -63,13 +63,13 @@ def _repeat(self, node, results):
6363
def _irepeat(self, node, results):
6464
return self._handle_rename(node, results, "imul")
6565

66-
@invocation("isinstance(%s, collections.Sequence)")
66+
@invocation("isinstance(%s, collections.abc.Sequence)")
6767
def _isSequenceType(self, node, results):
68-
return self._handle_type2abc(node, results, "collections", "Sequence")
68+
return self._handle_type2abc(node, results, "collections.abc", "Sequence")
6969

70-
@invocation("isinstance(%s, collections.Mapping)")
70+
@invocation("isinstance(%s, collections.abc.Mapping)")
7171
def _isMappingType(self, node, results):
72-
return self._handle_type2abc(node, results, "collections", "Mapping")
72+
return self._handle_type2abc(node, results, "collections.abc", "Mapping")
7373

7474
@invocation("isinstance(%s, numbers.Number)")
7575
def _isNumberType(self, node, results):

Lib/lib2to3/tests/test_fixers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4427,12 +4427,12 @@ def test_operator_sequenceIncludes(self):
44274427

44284428
def test_operator_isSequenceType(self):
44294429
b = "operator.isSequenceType(x)"
4430-
a = "import collections\nisinstance(x, collections.Sequence)"
4430+
a = "import collections.abc\nisinstance(x, collections.abc.Sequence)"
44314431
self.check(b, a)
44324432

44334433
def test_operator_isMappingType(self):
44344434
b = "operator.isMappingType(x)"
4435-
a = "import collections\nisinstance(x, collections.Mapping)"
4435+
a = "import collections.abc\nisinstance(x, collections.abc.Mapping)"
44364436
self.check(b, a)
44374437

44384438
def test_operator_isNumberType(self):
@@ -4478,12 +4478,12 @@ def test_bare_sequenceIncludes(self):
44784478

44794479
def test_bare_operator_isSequenceType(self):
44804480
s = "isSequenceType(z)"
4481-
t = "You should use 'isinstance(z, collections.Sequence)' here."
4481+
t = "You should use 'isinstance(z, collections.abc.Sequence)' here."
44824482
self.warns_unchanged(s, t)
44834483

44844484
def test_bare_operator_isMappingType(self):
44854485
s = "isMappingType(x)"
4486-
t = "You should use 'isinstance(x, collections.Mapping)' here."
4486+
t = "You should use 'isinstance(x, collections.abc.Mapping)' here."
44874487
self.warns_unchanged(s, t)
44884488

44894489
def test_bare_operator_isNumberType(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2to3 now generates a code that uses abstract collection classes from
2+
collections.abc rather than collections.

0 commit comments

Comments
 (0)