@@ -261,8 +261,21 @@ def strip_raw_and_b(string):
261261 return raw , has_b , string
262262
263263
264- def import_stmt (imp_from , imp , imp_as , raw = False ):
264+ def import_stmt (imp_from , imp , imp_as , raw = False , lazy = False ):
265265 """Generate an import statement."""
266+ if lazy :
267+ bind_name = imp_as if imp_as is not None else imp .split ("." , 1 )[0 ]
268+ if imp_from is not None :
269+ return '{name} = _coconut_lazy_module("{module}").{attr}' .format (
270+ name = bind_name ,
271+ module = imp_from ,
272+ attr = imp ,
273+ )
274+ else :
275+ return '{name} = _coconut_lazy_module("{module}")' .format (
276+ name = bind_name ,
277+ module = imp ,
278+ )
266279 if not raw and imp != "*" :
267280 module_path = (imp if imp_from is None else imp_from ).split ("." , 1 )
268281 existing_imp = import_existing .get (module_path [0 ])
@@ -4023,22 +4036,9 @@ def single_import(self, loc, path, imp_as, type_ignore=False, lazy=False):
40234036 imp_from += imp .rsplit ("." + imp_as , 1 )[0 ]
40244037 imp , imp_as = imp_as , None
40254038
4026- if lazy :
4027- bind_name = imp_as if imp_as is not None else imp .split ("." , 1 )[0 ]
4028- if imp_from is not None :
4029- out .append ('{name} = _coconut_lazy_module("{module}").{attr}' .format (
4030- name = bind_name ,
4031- module = imp_from ,
4032- attr = imp ,
4033- ))
4034- else :
4035- out .append ('{name} = _coconut_lazy_module("{module}")' .format (
4036- name = bind_name ,
4037- module = imp ,
4038- ))
4039- elif imp_as is not None and "." in imp_as :
4039+ if imp_as is not None and "." in imp_as :
40404040 import_as_var = self .get_temp_var ("import" , loc )
4041- out .append (import_stmt (imp_from , imp , import_as_var ))
4041+ out .append (import_stmt (imp_from , imp , import_as_var , lazy = lazy ))
40424042 fake_mods = imp_as .split ("." )
40434043 for i in range (1 , len (fake_mods )):
40444044 mod_name = "." .join (fake_mods [:i ])
@@ -4053,7 +4053,7 @@ def single_import(self, loc, path, imp_as, type_ignore=False, lazy=False):
40534053 ]
40544054 out .append ("." .join (fake_mods ) + " = " + import_as_var )
40554055 else :
4056- out .append (import_stmt (imp_from , imp , imp_as ))
4056+ out .append (import_stmt (imp_from , imp , imp_as , lazy = lazy ))
40574057
40584058 if type_ignore :
40594059 for i , line in enumerate (out ):
@@ -4139,24 +4139,14 @@ def universal_import(self, loc, imports, imp_from=None, lazy=False):
41394139
41404140 def import_handle (self , original , loc , tokens ):
41414141 """Universalizes imports."""
4142- # Detect lazy prefix by number of tokens:
4143- # basic_import: 1 token (imports)
4144- # from_import: 2 tokens (imp_from, imports)
4145- # lazy basic_import: 2 tokens ("lazy", imports)
4146- # lazy from_import: 3 tokens ("lazy", imp_from, imports)
4142+ # First token is always either "lazy" or "" (from Optional default)
4143+ lazy = tokens [0 ] == "lazy"
4144+ tokens = tokens [1 :]
4145+
41474146 if len (tokens ) == 1 :
4148- lazy = False
41494147 imp_from , imports = None , tokens [0 ]
41504148 elif len (tokens ) == 2 :
4151- if tokens [0 ] == "lazy" :
4152- lazy = True
4153- imp_from , imports = None , tokens [1 ]
4154- else :
4155- lazy = False
4156- imp_from , imports = tokens
4157- elif len (tokens ) == 3 :
4158- lazy = True
4159- imp_from , imports = tokens [1 ], tokens [2 ]
4149+ imp_from , imports = tokens
41604150 else :
41614151 raise CoconutInternalException ("invalid import tokens" , tokens )
41624152
0 commit comments