Skip to content

fix(sdk/python): handle module-level type aliases in AST analyzer - #13093

Closed
eunomie wants to merge 1 commit into
dagger:mainfrom
eunomie:sdk-python-type-aliases
Closed

eunomie wants to merge 1 commit into
dagger:mainfrom
eunomie:sdk-python-type-aliases

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 silently mishandled module-level type aliases such as:

Source = Annotated[dagger.Directory, dagger.DefaultPath(".")]

@dagger.object_type
class Foo:
    @dagger.function
    def build(self, src: Source) -> dagger.Container: ...

Two things went wrong:

  • Type resolution: Source was never registered in the analyzer's namespace, so _resolve_name fell through to its catch-all path and warn-assumed Source was an opaque object type. The resulting TypeDef carried kind=object name=Source instead of name=Directory.
  • Annotated metadata: extract_annotated_metadata operated on the raw ast.Name("Source") and saw no Annotated[...] wrapper, so DefaultPath(".") (and any Doc, Name, Ignore, Deprecated) were dropped on the floor.

Fix

Collect type-shaped top-level assignments into a per-file alias map and expand the alias before metadata extraction and resolution at every annotation entry point (field, class assignment, function parameter, and function/constructor return type).

  • Only assignments whose RHS looks like a type (Subscript, Attribute, Name, T | None) are recorded — plain string/number constants stay out of the type-resolution path.
  • Chained aliases (B = A; A = dagger.Directory) are followed; a visited-set guards against cycles.
  • The user-facing annotation string still keeps the alias name ("Source"), only resolution and metadata extraction see the expansion.

Test plan

  • New tests in sdk/python/tests/mod/test_ast_analyzer.py covering: Annotated alias as parameter (asserts both Directory resolution and default_path="."), Annotated alias as field, plain alias (Src = dagger.Directory), optional alias (MaybeDir = dagger.Directory | None), chained alias, and a self-referential cycle (must not recurse).
  • CI green on the Python SDK test suite.

Module-level type aliases like

    Source = Annotated[dagger.Directory, dagger.DefaultPath(".")]

were silently mishandled. The resolver fell through ``_resolve_name``
and warn-assumed ``Source`` was an opaque object type, while
``extract_annotated_metadata`` saw only ``Name("Source")`` and dropped
``DefaultPath`` along with the rest of the ``Annotated`` payload.

Collect type-shaped top-level assignments into a per-file alias map
and expand the alias before metadata extraction and resolution at every
annotation entry point (field, class assignment, function parameter,
return type). The surface annotation string still keeps the alias name
so the user-facing representation is unchanged. Chained aliases are
followed; a visited-set guards against cycles.

Signed-off-by: Yves Brissaud <[email protected]>
@eunomie
eunomie requested a review from a team as a code owner May 5, 2026 16:15
suprjinx pushed a commit to G-Research-Forks/dagger that referenced this pull request May 5, 2026
… AST analyzer

Module-level type aliases like

    Source = Annotated[dagger.Directory, dagger.DefaultPath(".")]

were silently mishandled. The resolver fell through ``_resolve_name``
and warn-assumed ``Source`` was an opaque object type, while
``extract_annotated_metadata`` saw only ``Name("Source")`` and dropped
``DefaultPath`` along with the rest of the ``Annotated`` payload.

Collect type-shaped top-level assignments into a per-file alias map
and expand the alias before metadata extraction and resolution at every
annotation entry point (field, class assignment, function parameter,
return type). The surface annotation string still keeps the alias name
so the user-facing representation is unchanged. Chained aliases are
followed; a visited-set guards against cycles.

Signed-off-by: Yves Brissaud <[email protected]>
Signed-off-by: Geoff Wilson <[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
eunomie added a commit to eunomie/dagger that referenced this pull request May 6, 2026
PEP 695 (Python 3.12+) introduced a dedicated ``ast.TypeAlias`` node
for the ``type Source = ...`` statement. ``_collect_module_constants``
only iterated ``ast.Assign`` and ``ast.AnnAssign``, so PEP 695 aliases
never made it into ``_module_type_aliases`` and were silently treated
as opaque object types — the same failure mode the legacy alias path
fixed in dagger#13093.

Add an explicit ``ast.TypeAlias`` branch that registers the alias
under the same alias map. ``_expand_alias`` then walks PEP 695 aliases
through chains and the existing cycle protection, so all the existing
alias machinery applies.

Closes dagger#11803 follow-up gap C1.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request May 6, 2026
PR dagger#13093 made module-level type aliases work, but its alias map was
keyed per-file: ``Source = Annotated[dagger.Directory, …]`` defined in
``types.py`` and imported via ``from .types import Source`` in
``main.py`` was not expanded — the analyzer fell back to treating
``Source`` as an opaque object type and the ``DefaultPath`` metadata
was dropped.

Track which names each file brought in via relative imports
(``_collect_relative_imports``) and follow those edges in
``_expand_alias`` so cross-file aliases resolve. Cycle protection now
keys on ``(file, name)`` rather than just ``name`` so the same alias
name can appear in multiple files without false-positive cycle stops.

The relative-import-target resolver maps the AST level + module path
to a concrete parsed file (``.py`` or ``__init__.py``) and is
best-effort — if the target isn't in the parsed set (third-party
package, dynamic ``__path__``), the entry is omitted and existing
warn-and-stub behavior takes over.

Closes dagger#11803 follow-up gap C2.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request May 6, 2026
PEP 695 (Python 3.12+) introduced a dedicated ``ast.TypeAlias`` node
for the ``type Source = ...`` statement. ``_collect_module_constants``
only iterated ``ast.Assign`` and ``ast.AnnAssign``, so PEP 695 aliases
never made it into ``_module_type_aliases`` and were silently treated
as opaque object types — the same failure mode the legacy alias path
fixed in dagger#13093. Add an explicit ``ast.TypeAlias`` branch that
registers the alias under the same alias map; ``_expand_alias`` then
walks PEP 695 aliases through chains and the existing cycle
protection.

The alias map was also keyed per-file: ``Source = Annotated[…]``
defined in ``types.py`` and imported via ``from .types import Source``
in ``main.py`` was not expanded — the analyzer fell back to treating
``Source`` as an opaque object type and the metadata was dropped.
Track which names each file brought in via relative imports
(``_collect_relative_imports``) and follow those edges in
``_expand_alias`` so cross-file aliases resolve. Cycle protection now
keys on ``(file, name)`` rather than just ``name`` so the same alias
name can appear in multiple files without false-positive cycle stops.
The relative-import-target resolver maps the AST level + module path
to a concrete parsed file (``.py`` or ``__init__.py``) and is
best-effort — if the target isn't in the parsed set (third-party
package, dynamic ``__path__``), the entry is omitted and existing
warn-and-stub behaviour takes over.

``ast.TypeAlias`` itself only exists on Python 3.12+; accessing it on
3.10 / 3.11 raises ``AttributeError`` and crashed every test against
the analyzer at module-constants collection. Resolve the attribute
once at import time via ``getattr(ast, "TypeAlias", None)`` and
short-circuit the ``isinstance`` check on that. PEP 695 source can't
be parsed by the older ``ast`` module anyway, so the branch is
genuinely a no-op there. Test fixtures that pass PEP 695 source
strings to ``analyze_source_string`` (``test_ast_pep695_*``) are
gated with a ``@requires_py312`` marker so they don't ``SyntaxError``
at parse time on 3.10 / 3.11. ``dagger call python-sdk python-{310,
311,312,313,314} unit`` all green.

Closes dagger#11803 follow-up gaps C1, C2.

Signed-off-by: Yves Brissaud <[email protected]>
suprjinx pushed a commit to G-Research-Forks/dagger that referenced this pull request May 12, 2026
… AST analyzer

Module-level type aliases like

    Source = Annotated[dagger.Directory, dagger.DefaultPath(".")]

were silently mishandled. The resolver fell through ``_resolve_name``
and warn-assumed ``Source`` was an opaque object type, while
``extract_annotated_metadata`` saw only ``Name("Source")`` and dropped
``DefaultPath`` along with the rest of the ``Annotated`` payload.

Collect type-shaped top-level assignments into a per-file alias map
and expand the alias before metadata extraction and resolution at every
annotation entry point (field, class assignment, function parameter,
return type). The surface annotation string still keeps the alias name
so the user-facing representation is unchanged. Chained aliases are
followed; a visited-set guards against cycles.

Signed-off-by: Yves Brissaud <[email protected]>
Signed-off-by: Geoff Wilson <[email protected]>
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