Skip to content

Commit c72828a

Browse files
evhubclaude
andcommitted
Address second round of PR review comments
- Revert grammar to use default="" for Optional lazy keyword - Simplify import_handle token parsing back to tokens[0] == "lazy" - Move lazy import handling from single_import to import_stmt function - single_import now just passes lazy through to import_stmt Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5f33251 commit c72828a

2 files changed

Lines changed: 23 additions & 33 deletions

File tree

coconut/compiler/compiler.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

coconut/compiler/grammar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ class Grammar(object):
20102010
- keyword("import").suppress() - from_import_names
20112011
)
20122012
import_stmt = Forward()
2013-
import_stmt_ref = Optional(keyword("lazy")) + (from_import | basic_import)
2013+
import_stmt_ref = Optional(keyword("lazy"), default="") + (from_import | basic_import)
20142014

20152015
augassign_stmt = Forward()
20162016
augassign_rhs = (

0 commit comments

Comments
 (0)