77And this import:
88 import spam
99Becomes:
10- import . spam
10+ from . import spam
1111"""
1212
1313# Local imports
1414from . import basefix
1515from os .path import dirname , join , exists , pathsep
16+ from .util import FromImport
1617
1718class FixImport (basefix .BaseFix ):
1819
1920 PATTERN = """
20- import_from< 'from' imp=any 'import' any >
21+ import_from< type= 'from' imp=any 'import' any >
2122 |
22- import_name< 'import' imp=any >
23+ import_name< type= 'import' imp=any >
2324 """
2425
2526 def transform (self , node , results ):
@@ -33,15 +34,19 @@ def transform(self, node, results):
3334 # I guess this is a global import -- skip it!
3435 return
3536
36- # Some imps are top-level (eg: 'import ham')
37- # some are first level (eg: 'import ham.eggs')
38- # some are third level (eg: 'import ham.eggs as spam')
39- # Hence, the loop
40- while not hasattr (imp , 'value' ):
41- imp = imp .children [0 ]
42-
43- imp .value = "." + imp .value
44- node .changed ()
37+ if results ['type' ].value == 'from' :
38+ # Some imps are top-level (eg: 'import ham')
39+ # some are first level (eg: 'import ham.eggs')
40+ # some are third level (eg: 'import ham.eggs as spam')
41+ # Hence, the loop
42+ while not hasattr (imp , 'value' ):
43+ imp = imp .children [0 ]
44+ imp .value = "." + imp .value
45+ node .changed ()
46+ else :
47+ new = FromImport ('.' , getattr (imp , 'content' , None ) or [imp ])
48+ new .prefix = node .get_prefix ()
49+ node = new
4550 return node
4651
4752def probably_a_local_import (imp_name , file_path ):
0 commit comments