Skip to content

Commit 9bfd44e

Browse files
committed
Remove check if package is imported in from imports
Remove the check whether the package being imported (the part before `import`) is already loaded, instead of avoiding the import entirely if it's already loaded as we do for regular imports. This avoids a crash when dealing with multiple modules imported from the same package because only the modules listed in the first import will actually be loaded. For example, if we have something like: ``` from pkg import mod1 from pkg import mod2 ``` The second import statement will skip the import because it thinks pkg is already imported, so attempts to access pkg.mod2 will fail with an AttributeError. It might be worth combining multiple from imports like the ones above into one from import statement, but that would probably involve analyzing a lot of separate code, which might slow down compile times, and would probably be more trouble than it's worth.
1 parent 69e051e commit 9bfd44e

File tree

1 file changed

+0
-5
lines changed

1 file changed

+0
-5
lines changed

mypyc/irbuild/builder.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,6 @@ def add_to_non_ext_dict(self, non_ext: NonExtClassInfo,
291291
def gen_import_from(self, id: str, line: int, imported: List[str]) -> None:
292292
self.imports[id] = None
293293

294-
needs_import, out = BasicBlock(), BasicBlock()
295-
self.check_if_module_loaded(id, line, needs_import, out)
296-
297-
self.activate_block(needs_import)
298294
globals_dict = self.load_globals_dict()
299295
locals_dict = self.call_c(get_locals, [], line)
300296
names_to_import = self.new_list_op([self.load_str(name) for name in imported], line)
@@ -306,7 +302,6 @@ def gen_import_from(self, id: str, line: int, imported: List[str]) -> None:
306302
line,
307303
)
308304
self.add(InitStatic(value, id, namespace=NAMESPACE_MODULE))
309-
self.goto_and_activate(out)
310305

311306
def gen_import(self, id: str, line: int) -> None:
312307
self.imports[id] = None

0 commit comments

Comments
 (0)