from collections.abc rather than collections.
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
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)
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):
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):
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):
--- /dev/null
+2to3 now generates a code that uses abstract collection classes from
+collections.abc rather than collections.