FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-30143: 2to3 now generates a code that uses abstract collection classes by serhiy-storchaka · Pull Request #1262 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 6 additions & 6 deletions Doc/library/2to3.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,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__')``

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sorry for off-topic, but what is the reason of using hasattr(obj, '__call__') instead of just callable(obj)? The latter is much faster and looks better IMO.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't know. Maybe for compatibility with Python 3.0 and 3.1.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Do you mind to open an issue for this @ilevkivskyi? If ignore Python 3.0 and 3.1, callable(obj) looks better replacement.

``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

Expand Down
12 changes: 6 additions & 6 deletions Lib/lib2to3/fixes/fix_operator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions Lib/lib2to3/tests/test_fixers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2to3 now generates a code that uses abstract collection classes from
collections.abc rather than collections.

Back | FazBrowse Home | New Git URL