From 251672b435e98b16d2136f340d652de4e5a1c079 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 23 Apr 2017 15:12:12 +0300 Subject: [PATCH 1/2] bpo-30143: 2to3 now generates a code that uses abstract collection classes from collections.abc rather than collections. --- Doc/library/2to3.rst | 12 ++++++------ Lib/lib2to3/fixes/fix_operator.py | 12 ++++++------ Lib/lib2to3/tests/test_fixers.py | 8 ++++---- Misc/NEWS | 3 +++ 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/Doc/library/2to3.rst b/Doc/library/2to3.rst index ace1bfaf8cb9d6..26d5088061b54f 100644 --- a/Doc/library/2to3.rst +++ b/Doc/library/2to3.rst @@ -351,20 +351,20 @@ and off individually. They are described here in more detail. Converts calls to various functions in the :mod:`operator` module to other, but equivalent, function calls. When needed, the appropriate ``import`` - statements are added, e.g. ``import collections``. The following mapping + statements are added, e.g. ``import collections.abc``. The following mapping are made: - ================================== ========================================== + ================================== ============================================= From To - ================================== ========================================== + ================================== ============================================= ``operator.isCallable(obj)`` ``hasattr(obj, '__call__')`` ``operator.sequenceIncludes(obj)`` ``operator.contains(obj)`` - ``operator.isSequenceType(obj)`` ``isinstance(obj, collections.Sequence)`` - ``operator.isMappingType(obj)`` ``isinstance(obj, collections.Mapping)`` + ``operator.isSequenceType(obj)`` ``isinstance(obj, collections.abc.Sequence)`` + ``operator.isMappingType(obj)`` ``isinstance(obj, collections.abc.Mapping)`` ``operator.isNumberType(obj)`` ``isinstance(obj, numbers.Number)`` ``operator.repeat(obj, n)`` ``operator.mul(obj, n)`` ``operator.irepeat(obj, n)`` ``operator.imul(obj, n)`` - ================================== ========================================== + ================================== ============================================= .. 2to3fixer:: paren diff --git a/Lib/lib2to3/fixes/fix_operator.py b/Lib/lib2to3/fixes/fix_operator.py index 1aa17bae58da8c..f45f552a0917ea 100644 --- a/Lib/lib2to3/fixes/fix_operator.py +++ b/Lib/lib2to3/fixes/fix_operator.py @@ -2,8 +2,8 @@ operator.isCallable(obj) -> hasattr(obj, '__call__') operator.sequenceIncludes(obj) -> operator.contains(obj) -operator.isSequenceType(obj) -> isinstance(obj, collections.Sequence) -operator.isMappingType(obj) -> isinstance(obj, collections.Mapping) +operator.isSequenceType(obj) -> isinstance(obj, collections.abc.Sequence) +operator.isMappingType(obj) -> isinstance(obj, collections.abc.Mapping) operator.isNumberType(obj) -> isinstance(obj, numbers.Number) operator.repeat(obj, n) -> operator.mul(obj, n) operator.irepeat(obj, n) -> operator.imul(obj, n) @@ -63,13 +63,13 @@ def _repeat(self, node, results): def _irepeat(self, node, results): return self._handle_rename(node, results, "imul") - @invocation("isinstance(%s, collections.Sequence)") + @invocation("isinstance(%s, collections.abc.Sequence)") def _isSequenceType(self, node, results): - return self._handle_type2abc(node, results, "collections", "Sequence") + return self._handle_type2abc(node, results, "collections.abc", "Sequence") - @invocation("isinstance(%s, collections.Mapping)") + @invocation("isinstance(%s, collections.abc.Mapping)") def _isMappingType(self, node, results): - return self._handle_type2abc(node, results, "collections", "Mapping") + return self._handle_type2abc(node, results, "collections.abc", "Mapping") @invocation("isinstance(%s, numbers.Number)") def _isNumberType(self, node, results): diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index 3e1a255737ec4b..e50b7dadae8b7f 100644 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -4427,12 +4427,12 @@ def test_operator_sequenceIncludes(self): def test_operator_isSequenceType(self): b = "operator.isSequenceType(x)" - a = "import collections\nisinstance(x, collections.Sequence)" + a = "import collections.abc\nisinstance(x, collections.abc.Sequence)" self.check(b, a) def test_operator_isMappingType(self): b = "operator.isMappingType(x)" - a = "import collections\nisinstance(x, collections.Mapping)" + a = "import collections.abc\nisinstance(x, collections.abc.Mapping)" self.check(b, a) def test_operator_isNumberType(self): @@ -4478,12 +4478,12 @@ def test_bare_sequenceIncludes(self): def test_bare_operator_isSequenceType(self): s = "isSequenceType(z)" - t = "You should use 'isinstance(z, collections.Sequence)' here." + t = "You should use 'isinstance(z, collections.abc.Sequence)' here." self.warns_unchanged(s, t) def test_bare_operator_isMappingType(self): s = "isMappingType(x)" - t = "You should use 'isinstance(x, collections.Mapping)' here." + t = "You should use 'isinstance(x, collections.abc.Mapping)' here." self.warns_unchanged(s, t) def test_bare_operator_isNumberType(self): diff --git a/Misc/NEWS b/Misc/NEWS index 651d17569bb796..63799298f0d08f 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -317,6 +317,9 @@ Extension Modules Library ------- +- bpo-30143: 2to3 now generates a code that uses abstract collection classes + from collections.abc rather than collections. + - bpo-29960: Preserve generator state when _random.Random.setstate() raises an exception. Patch by Bryan Olson. From 696e3f2a46b65fe39ea0ca1cccc2e6180959c6c7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 12 Oct 2017 19:06:04 +0300 Subject: [PATCH 2/2] Move NEWS to NEWS.d/. --- Misc/NEWS | 3 --- .../next/Library/2017-10-12-19-05-54.bpo-30143.25_hU1.rst | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2017-10-12-19-05-54.bpo-30143.25_hU1.rst diff --git a/Misc/NEWS b/Misc/NEWS index 63799298f0d08f..651d17569bb796 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -317,9 +317,6 @@ Extension Modules Library ------- -- bpo-30143: 2to3 now generates a code that uses abstract collection classes - from collections.abc rather than collections. - - bpo-29960: Preserve generator state when _random.Random.setstate() raises an exception. Patch by Bryan Olson. diff --git a/Misc/NEWS.d/next/Library/2017-10-12-19-05-54.bpo-30143.25_hU1.rst b/Misc/NEWS.d/next/Library/2017-10-12-19-05-54.bpo-30143.25_hU1.rst new file mode 100644 index 00000000000000..a1f83128542532 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-10-12-19-05-54.bpo-30143.25_hU1.rst @@ -0,0 +1,2 @@ +2to3 now generates a code that uses abstract collection classes from +collections.abc rather than collections.