| 1 |
"""Fix incompatible renames |
| 2 |
|
| 3 |
Fixes: |
| 4 |
* sys.maxint -> sys.maxsize |
| 5 |
""" |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
from .. import fixer_base |
| 11 |
from ..fixer_util import Name, attr_chain |
| 12 |
|
| 13 |
MAPPING = {"sys": {"maxint" : "maxsize"}, |
| 14 |
} |
| 15 |
LOOKUP = {} |
| 16 |
|
| 17 |
def alternates(members): |
| 18 |
return "(" + "|".join(map(repr, members)) + ")" |
| 19 |
|
| 20 |
|
| 21 |
def build_pattern(): |
| 22 |
|
| 23 |
for module, replace in list(MAPPING.items()): |
| 24 |
for old_attr, new_attr in list(replace.items()): |
| 25 |
LOOKUP[(module, old_attr)] = new_attr |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
yield """ |
| 33 |
import_from< 'from' module_name=%r 'import' |
| 34 |
( attr_name=%r | import_as_name< attr_name=%r 'as' any >) > |
| 35 |
""" % (module, old_attr, old_attr) |
| 36 |
yield """ |
| 37 |
power< module_name=%r trailer< '.' attr_name=%r > any* > |
| 38 |
""" % (module, old_attr) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class FixRenames(fixer_base.BaseFix): |
| 43 |
BM_compatible = True |
| 44 |
PATTERN = "|".join(build_pattern()) |
| 45 |
|
| 46 |
order = "pre" |
| 47 |
|
| 48 |
|
| 49 |
def match(self, node): |
| 50 |
match = super(FixRenames, self).match |
| 51 |
results = match(node) |
| 52 |
if results: |
| 53 |
if any(match(obj) for obj in attr_chain(node, "parent")): |
| 54 |
return False |
| 55 |
return results |
| 56 |
return False |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
def transform(self, node, results): |
| 63 |
mod_name = results.get("module_name") |
| 64 |
attr_name = results.get("attr_name") |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if mod_name and attr_name: |
| 69 |
new_attr = LOOKUP[(mod_name.value, attr_name.value)] |
| 70 |
attr_name.replace(Name(new_attr, prefix=attr_name.prefix)) |