Skip to content

fix(sdk/python): handle relative imports in AST analyzer - #13091

Closed
eunomie wants to merge 1 commit into
dagger:mainfrom
eunomie:sdk-python-relative-imports
Closed

eunomie wants to merge 1 commit into
dagger:mainfrom
eunomie:sdk-python-relative-imports

Conversation

@eunomie

@eunomie eunomie commented May 5, 2026

Copy link
Copy Markdown
Member

Summary

The AST analyzer used by dagger develop for the Python SDK didn't handle relative imports correctly:

  • from . import x raised ValueError because importlib.import_module was called with an empty module name.
  • from .x import y (and other dotted relative forms) fell through to importlib, 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 > 0 in the analyzer and bind the imported names as stubs in the local namespace, without dispatching through importlib. Cross-file types decorated with @object_type / @enum_type are still picked up via add_declared_type, so type annotations referencing them continue to resolve correctly.

Test plan

  • New tests in sdk/python/tests/mod/test_ast_analyzer.py covering from . import x, from .mod import y, from ..pkg import z, and that relative-imported decorated types still resolve in annotations.
  • CI green on the Python SDK test suite.

@eunomie
eunomie requested a review from a team as a code owner May 5, 2026 15:39
@eunomie
eunomie requested review from TomChv and grouville May 5, 2026 15:41
`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

eunomie commented May 5, 2026

Copy link
Copy Markdown
Member Author

Included in #13095 that covers more python AST issues

@eunomie eunomie closed this May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant