1
"""Fix incompatible renames
4
* sys.maxint -> sys.maxsize
6
# Author: Christian Heimes
7
# based on Collin Winter's fix_import
10
from .. import fixer_base
11
from ..fixer_util import Name, attr_chain
13
MAPPING = {"sys": {"maxint" : "maxsize"},
17
def alternates(members):
18
return "(" + "|".join(map(repr, members)) + ")"
23
for module, replace in MAPPING.items():
24
for old_attr, new_attr in replace.items():
25
LOOKUP[(module, old_attr)] = new_attr
29
# import_name< 'import' (module=%r
30
# | dotted_as_names< any* module=%r any* >) >
31
# """ % (module, module)
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)
37
power< module_name=%r trailer< '.' attr_name=%r > any* >
38
""" % (module, old_attr)
39
#yield """bare_name=%s""" % alternates(bare)
42
class FixRenames(fixer_base.BaseFix):
43
PATTERN = "|".join(build_pattern())
45
order = "pre" # Pre-order tree traversal
47
# Don't match the node if it's within another match
48
def match(self, node):
49
match = super(FixRenames, self).match
52
if any([match(obj) for obj in attr_chain(node, "parent")]):
57
#def start_tree(self, tree, filename):
58
# super(FixRenames, self).start_tree(tree, filename)
61
def transform(self, node, results):
62
mod_name = results.get("module_name")
63
attr_name = results.get("attr_name")
64
#bare_name = results.get("bare_name")
65
#import_mod = results.get("module")
67
if mod_name and attr_name:
68
new_attr = LOOKUP[(mod_name.value, attr_name.value)]
69
attr_name.replace(Name(new_attr, prefix=attr_name.get_prefix()))