diff options
Diffstat (limited to 'Lib/lib2to3/fixes/fix_import.py')
-rw-r--r-- | Lib/lib2to3/fixes/fix_import.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/Lib/lib2to3/fixes/fix_import.py b/Lib/lib2to3/fixes/fix_import.py index 625611d..ef9b619 100644 --- a/Lib/lib2to3/fixes/fix_import.py +++ b/Lib/lib2to3/fixes/fix_import.py @@ -43,7 +43,13 @@ class FixImport(fixer_base.BaseFix): import_name< 'import' imp=any > """ + def start_tree(self, tree, name): + super(FixImport, self).start_tree(tree, name) + self.skip = "absolute_import" in tree.future_features + def transform(self, node, results): + if self.skip: + return imp = results['imp'] if node.type == syms.import_from: @@ -71,19 +77,22 @@ class FixImport(fixer_base.BaseFix): self.warning(node, "absolute and local imports together") return - new = FromImport('.', [imp]) + new = FromImport(".", [imp]) new.prefix = node.prefix return new def probably_a_local_import(self, imp_name): - imp_name = imp_name.split('.', 1)[0] + if imp_name.startswith("."): + # Relative imports are certainly not local imports. + return False + imp_name = imp_name.split(".", 1)[0] base_path = dirname(self.filename) base_path = join(base_path, imp_name) # If there is no __init__.py next to the file its not in a package # so can't be a relative import. - if not exists(join(dirname(base_path), '__init__.py')): + if not exists(join(dirname(base_path), "__init__.py")): return False - for ext in ['.py', sep, '.pyc', '.so', '.sl', '.pyd']: + for ext in [".py", sep, ".pyc", ".so", ".sl", ".pyd"]: if exists(base_path + ext): return True return False |