| 1 | n/a | """Fixer for operator functions. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | operator.isCallable(obj) -> hasattr(obj, '__call__') |
|---|
| 4 | n/a | operator.sequenceIncludes(obj) -> operator.contains(obj) |
|---|
| 5 | n/a | operator.isSequenceType(obj) -> isinstance(obj, collections.Sequence) |
|---|
| 6 | n/a | operator.isMappingType(obj) -> isinstance(obj, collections.Mapping) |
|---|
| 7 | n/a | operator.isNumberType(obj) -> isinstance(obj, numbers.Number) |
|---|
| 8 | n/a | operator.repeat(obj, n) -> operator.mul(obj, n) |
|---|
| 9 | n/a | operator.irepeat(obj, n) -> operator.imul(obj, n) |
|---|
| 10 | n/a | """ |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | import collections |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | # Local imports |
|---|
| 15 | n/a | from lib2to3 import fixer_base |
|---|
| 16 | n/a | from lib2to3.fixer_util import Call, Name, String, touch_import |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def invocation(s): |
|---|
| 20 | n/a | def dec(f): |
|---|
| 21 | n/a | f.invocation = s |
|---|
| 22 | n/a | return f |
|---|
| 23 | n/a | return dec |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | class FixOperator(fixer_base.BaseFix): |
|---|
| 27 | n/a | BM_compatible = True |
|---|
| 28 | n/a | order = "pre" |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | methods = """ |
|---|
| 31 | n/a | method=('isCallable'|'sequenceIncludes' |
|---|
| 32 | n/a | |'isSequenceType'|'isMappingType'|'isNumberType' |
|---|
| 33 | n/a | |'repeat'|'irepeat') |
|---|
| 34 | n/a | """ |
|---|
| 35 | n/a | obj = "'(' obj=any ')'" |
|---|
| 36 | n/a | PATTERN = """ |
|---|
| 37 | n/a | power< module='operator' |
|---|
| 38 | n/a | trailer< '.' %(methods)s > trailer< %(obj)s > > |
|---|
| 39 | n/a | | |
|---|
| 40 | n/a | power< %(methods)s trailer< %(obj)s > > |
|---|
| 41 | n/a | """ % dict(methods=methods, obj=obj) |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def transform(self, node, results): |
|---|
| 44 | n/a | method = self._check_method(node, results) |
|---|
| 45 | n/a | if method is not None: |
|---|
| 46 | n/a | return method(node, results) |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | @invocation("operator.contains(%s)") |
|---|
| 49 | n/a | def _sequenceIncludes(self, node, results): |
|---|
| 50 | n/a | return self._handle_rename(node, results, "contains") |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | @invocation("hasattr(%s, '__call__')") |
|---|
| 53 | n/a | def _isCallable(self, node, results): |
|---|
| 54 | n/a | obj = results["obj"] |
|---|
| 55 | n/a | args = [obj.clone(), String(", "), String("'__call__'")] |
|---|
| 56 | n/a | return Call(Name("hasattr"), args, prefix=node.prefix) |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | @invocation("operator.mul(%s)") |
|---|
| 59 | n/a | def _repeat(self, node, results): |
|---|
| 60 | n/a | return self._handle_rename(node, results, "mul") |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | @invocation("operator.imul(%s)") |
|---|
| 63 | n/a | def _irepeat(self, node, results): |
|---|
| 64 | n/a | return self._handle_rename(node, results, "imul") |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | @invocation("isinstance(%s, collections.Sequence)") |
|---|
| 67 | n/a | def _isSequenceType(self, node, results): |
|---|
| 68 | n/a | return self._handle_type2abc(node, results, "collections", "Sequence") |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | @invocation("isinstance(%s, collections.Mapping)") |
|---|
| 71 | n/a | def _isMappingType(self, node, results): |
|---|
| 72 | n/a | return self._handle_type2abc(node, results, "collections", "Mapping") |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | @invocation("isinstance(%s, numbers.Number)") |
|---|
| 75 | n/a | def _isNumberType(self, node, results): |
|---|
| 76 | n/a | return self._handle_type2abc(node, results, "numbers", "Number") |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def _handle_rename(self, node, results, name): |
|---|
| 79 | n/a | method = results["method"][0] |
|---|
| 80 | n/a | method.value = name |
|---|
| 81 | n/a | method.changed() |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def _handle_type2abc(self, node, results, module, abc): |
|---|
| 84 | n/a | touch_import(None, module, node) |
|---|
| 85 | n/a | obj = results["obj"] |
|---|
| 86 | n/a | args = [obj.clone(), String(", " + ".".join([module, abc]))] |
|---|
| 87 | n/a | return Call(Name("isinstance"), args, prefix=node.prefix) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | def _check_method(self, node, results): |
|---|
| 90 | n/a | method = getattr(self, "_" + results["method"][0].value) |
|---|
| 91 | n/a | if isinstance(method, collections.Callable): |
|---|
| 92 | n/a | if "module" in results: |
|---|
| 93 | n/a | return method |
|---|
| 94 | n/a | else: |
|---|
| 95 | n/a | sub = (str(results["obj"]),) |
|---|
| 96 | n/a | invocation_str = method.invocation % sub |
|---|
| 97 | n/a | self.warning(node, "You should use '%s' here." % invocation_str) |
|---|
| 98 | n/a | return None |
|---|