[Python-checkins] bpo-32046: Update 2to3 when converts operator.isCallable(obj). (#4417)
Éric Araujo
webhook-mailer at python.org
Tue Nov 28 11:27:01 EST 2017
https://github.com/python/cpython/commit/a489599793f9b00ddc2c68e2ce3bce9cbb2c09a2
commit: a489599793f9b00ddc2c68e2ce3bce9cbb2c09a2
branch: master
author: Dong-hee Na <donghee.na92 at gmail.com>
committer: Éric Araujo <merwok at users.noreply.github.com>
date: 2017-11-28T11:26:56-05:00
summary:
bpo-32046: Update 2to3 when converts operator.isCallable(obj). (#4417)
files:
A Misc/NEWS.d/next/Library/2017-11-16-20-09-45.bpo-32046.9sGDtw.rst
M Doc/library/2to3.rst
M Lib/lib2to3/fixes/fix_operator.py
M Lib/lib2to3/tests/test_fixers.py
diff --git a/Doc/library/2to3.rst b/Doc/library/2to3.rst
index faf06d91c3d..deb5e10f6ef 100644
--- a/Doc/library/2to3.rst
+++ b/Doc/library/2to3.rst
@@ -351,7 +351,7 @@ and off individually. They are described here in more detail.
================================== =============================================
From To
================================== =============================================
- ``operator.isCallable(obj)`` ``hasattr(obj, '__call__')``
+ ``operator.isCallable(obj)`` ``callable(obj)``
``operator.sequenceIncludes(obj)`` ``operator.contains(obj)``
``operator.isSequenceType(obj)`` ``isinstance(obj, collections.abc.Sequence)``
``operator.isMappingType(obj)`` ``isinstance(obj, collections.abc.Mapping)``
diff --git a/Lib/lib2to3/fixes/fix_operator.py b/Lib/lib2to3/fixes/fix_operator.py
index 0d82454023f..d303cd2018b 100644
--- a/Lib/lib2to3/fixes/fix_operator.py
+++ b/Lib/lib2to3/fixes/fix_operator.py
@@ -1,6 +1,6 @@
"""Fixer for operator functions.
-operator.isCallable(obj) -> hasattr(obj, '__call__')
+operator.isCallable(obj) -> callable(obj)
operator.sequenceIncludes(obj) -> operator.contains(obj)
operator.isSequenceType(obj) -> isinstance(obj, collections.abc.Sequence)
operator.isMappingType(obj) -> isinstance(obj, collections.abc.Mapping)
@@ -49,11 +49,10 @@ def transform(self, node, results):
def _sequenceIncludes(self, node, results):
return self._handle_rename(node, results, "contains")
- @invocation("hasattr(%s, '__call__')")
+ @invocation("callable(%s)")
def _isCallable(self, node, results):
obj = results["obj"]
- args = [obj.clone(), String(", "), String("'__call__'")]
- return Call(Name("hasattr"), args, prefix=node.prefix)
+ return Call(Name("callable"), [obj.clone()], prefix=node.prefix)
@invocation("operator.mul(%s)")
def _repeat(self, node, results):
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
index e50b7dadae8..bfe7a23e700 100644
--- a/Lib/lib2to3/tests/test_fixers.py
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -4409,7 +4409,7 @@ class Test_operator(FixerTestCase):
def test_operator_isCallable(self):
b = "operator.isCallable(x)"
- a = "hasattr(x, '__call__')"
+ a = "callable(x)"
self.check(b, a)
def test_operator_sequenceIncludes(self):
@@ -4468,7 +4468,7 @@ def test_operator_irepeat(self):
def test_bare_isCallable(self):
s = "isCallable(x)"
- t = "You should use 'hasattr(x, '__call__')' here."
+ t = "You should use 'callable(x)' here."
self.warns_unchanged(s, t)
def test_bare_sequenceIncludes(self):
diff --git a/Misc/NEWS.d/next/Library/2017-11-16-20-09-45.bpo-32046.9sGDtw.rst b/Misc/NEWS.d/next/Library/2017-11-16-20-09-45.bpo-32046.9sGDtw.rst
new file mode 100644
index 00000000000..a6fc3c46fc3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-11-16-20-09-45.bpo-32046.9sGDtw.rst
@@ -0,0 +1,2 @@
+Updates 2to3 to convert from operator.isCallable(obj) to callable(obj).
+Patch by Dong-hee Na.
More information about the Python-checkins
mailing list