Skip to content

Commit e8fcb04

Browse files
tomcodgenfrainfreezetkfoss
authored andcommitted
[CG-10829] fix: import wildcard doesn't preserve import resolution (#502)
Importing from a file that contains a wildcard can break further resolution. This could occur depending on to_resolve ordering, if the outer file is processed first _wildcards will not be filled in time. --------- Co-authored-by: tomcodegen <[email protected]> Co-authored-by: tomcodgen <[email protected]>
1 parent 538418e commit e8fcb04

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/codegen/sdk/core/import_resolution.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ def names(self) -> Generator[tuple[str, Self | WildcardImport[Self]], None, None
607607
imp.file.invalidate()
608608

609609
return
610+
elif self.resolved_symbol is None:
611+
self._resolving_wildcards = False
610612
yield self.name, self
611613

612614
@property

tests/unit/codegen/sdk/python/import_resolution/test_import_resolution.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,30 @@ def some_func():
341341
# Verify usages are detected
342342
assert len(some_func.usages) > 0
343343
assert len(some_func.symbol_usages) > 0
344+
345+
346+
def test_import_wildcard_preserves_import_resolution(tmpdir: str) -> None:
347+
"""Tests importing from a file that contains a wildcard import doesn't break further resolution.
348+
This could occur depending on to_resolve ordering, if the outer file is processed first _wildcards will not be filled in time.
349+
"""
350+
# language=python
351+
with get_codebase_session(
352+
tmpdir,
353+
files={
354+
"testdir/sub/file.py": """
355+
test_const=5
356+
b=2
357+
""",
358+
"testdir/file.py": """
359+
from testdir.sub.file import *
360+
c=b
361+
""",
362+
"file.py": """
363+
from testdir.file import test_const
364+
test = test_const
365+
""",
366+
},
367+
) as codebase:
368+
mainfile: SourceFile = codebase.get_file("file.py")
369+
370+
assert len(mainfile.ctx.edges) == 5

0 commit comments

Comments
 (0)