Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/2to3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)``
Expand Down
7 changes: 3 additions & 4 deletions Lib/lib2to3/fixes/fix_operator.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Lib/lib2to3/tests/test_fixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Updates 2to3 to convert from operator.isCallable(obj) to callable(obj).
Patch by Dong-hee Na.