1
"""Fixer for operator.{isCallable,sequenceIncludes}
3
operator.isCallable(obj) -> hasattr(obj, '__call__')
4
operator.sequenceIncludes(obj) -> operator.contains(obj)
8
from .. import fixer_base
9
from ..fixer_util import Call, Name, String
11
class FixOperator(fixer_base.BaseFix):
13
methods = "method=('isCallable'|'sequenceIncludes')"
14
func = "'(' func=any ')'"
16
power< module='operator'
17
trailer< '.' {methods} > trailer< {func} > >
19
power< {methods} trailer< {func} > >
20
""".format(methods=methods, func=func)
22
def transform(self, node, results):
23
method = results["method"][0]
25
if method.value == u"sequenceIncludes":
26
if "module" not in results:
27
# operator may not be in scope, so we can't make a change.
28
self.warning(node, "You should use operator.contains here.")
30
method.value = u"contains"
32
elif method.value == u"isCallable":
33
if "module" not in results:
35
"You should use hasattr(%s, '__call__') here." %
36
results["func"].value)
38
func = results["func"]
39
args = [func.clone(), String(u", "), String(u"'__call__'")]
40
return Call(Name(u"hasattr"), args, prefix=node.prefix)