mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
merge 2to3 improvments
This commit is contained in:
parent
afcd5f36f0
commit
dd6a4edc45
18 changed files with 405 additions and 140 deletions
40
Lib/lib2to3/fixes/fix_operator.py
Normal file
40
Lib/lib2to3/fixes/fix_operator.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
"""Fixer for operator.{isCallable,sequenceIncludes}
|
||||
|
||||
operator.isCallable(obj) -> hasattr(obj, '__call__')
|
||||
operator.sequenceIncludes(obj) -> operator.contains(obj)
|
||||
"""
|
||||
|
||||
# Local imports
|
||||
from .. import fixer_base
|
||||
from ..fixer_util import Call, Name, String
|
||||
|
||||
class FixOperator(fixer_base.BaseFix):
|
||||
|
||||
methods = "method=('isCallable'|'sequenceIncludes')"
|
||||
func = "'(' func=any ')'"
|
||||
PATTERN = """
|
||||
power< module='operator'
|
||||
trailer< '.' {methods} > trailer< {func} > >
|
||||
|
|
||||
power< {methods} trailer< {func} > >
|
||||
""".format(methods=methods, func=func)
|
||||
|
||||
def transform(self, node, results):
|
||||
method = results["method"][0]
|
||||
|
||||
if method.value == "sequenceIncludes":
|
||||
if "module" not in results:
|
||||
# operator may not be in scope, so we can't make a change.
|
||||
self.warning(node, "You should use operator.contains here.")
|
||||
else:
|
||||
method.value = "contains"
|
||||
method.changed()
|
||||
elif method.value == "isCallable":
|
||||
if "module" not in results:
|
||||
self.warning(node,
|
||||
"You should use hasattr(%s, '__call__') here." %
|
||||
results["func"].value)
|
||||
else:
|
||||
func = results["func"]
|
||||
args = [func.clone(), String(", "), String("'__call__'")]
|
||||
return Call(Name("hasattr"), args, prefix=node.prefix)
|
Loading…
Add table
Add a link
Reference in a new issue