Repository navigation
Conversation
`from . import x` raised ValueError because importlib was called with an empty module name; `from .x import y` and other relative forms silently fell through to importlib, where they would either error or bind the wrong top-level module. Detect ImportFrom.level > 0 and bind the imported names as stubs without dispatching through importlib. Cross-file decorated classes are still resolved via add_declared_type, so type annotations referencing them keep working. Signed-off-by: Yves Brissaud <[email protected]>
eunomie
force-pushed
the
sdk-python-relative-imports
branch
from
May 5, 2026 15:57
71c9ed3 to
8340a95
Compare
This was referenced May 5, 2026
Member
Author
|
Included in #13095 that covers more python AST issues |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The AST analyzer used by
dagger developfor the Python SDK didn't handle relative imports correctly:from . import xraisedValueErrorbecauseimportlib.import_modulewas called with an empty module name.from .x import y(and other dotted relative forms) fell through toimportlib, where they either errored or silently resolved to the wrong top-level module.This meant a Dagger module split across multiple files using ordinary Python relative imports could fail to load at codegen time.
Fix
Detect
ImportFrom.level > 0in the analyzer and bind the imported names as stubs in the local namespace, without dispatching throughimportlib. Cross-file types decorated with@object_type/@enum_typeare still picked up viaadd_declared_type, so type annotations referencing them continue to resolve correctly.Test plan
sdk/python/tests/mod/test_ast_analyzer.pycoveringfrom . import x,from .mod import y,from ..pkg import z, and that relative-imported decorated types still resolve in annotations.