Skip to content

Python SDK: AST-based module analysis misses several Python patterns #13097

Closed

Description

@eunomie

What happened

In v0.20.7 (#11803) we changed how the Python SDK reads your module to register it with the engine. The old way ran your code; the new way reads it as text only. Reading text instead of running code is the right long-term move — it unlocks a feature called "self calls" — but the new reader doesn't yet recognise everything Python can express.

That means modules that worked fine in v0.20.6 may now register the wrong types or skip parts of the schema in v0.20.7 and later.

Symptoms users have hit

  • A function disappears from dagger functions because we wrote it in a way the new reader didn't recognise — for example, importing the dagger package under a different name (import dagger as d), or inheriting a function from a base class.
  • A type ends up wrong in the schema — for example, a parameter that should accept a directory shows up as some other kind of value, or its default is silently dropped.

Three follow-up PRs already shipped fixes for the most common shapes:

Then a wider audit (#13095) catalogued many more shapes that still misbehave and fixes them, plus adds a differential test suite that compares the new reader's output against the old run-the-code path so the two stay in sync going forward.

Track:

Activity

  1. added this to the v0.21.0 milestone on May 5, 2026
  2. grouville commented on May 5, 2026

    @grouville
    Member

    As discussed, one approach could be to integrate the CPython's Lib/test/test_typing.py suite into our static analysis port (maybe others?)?

    As the previous runtime path relied on CPython's typing.get_type_hints() function, we can be sure that the contract upon which people were writing their Python Dagger SDK functions is the contract that these test coverage should support

  3. grouville commented on May 5, 2026

    @grouville
    Member

    Another question: what about the different Python versions, as typing.get_type_hints() is dependent on the Python version ? How does our Typescript SDK static analysis handle that 👀 ?

  4. eunomie commented on May 5, 2026

    @eunomie
    MemberAuthor

    We can't directly integrate test_typing.py, it's not testing the same thing (it's testing the runtime, and many things we don't care about.

    For now #13095 is doing:

    • Differential testing — for any Python source, run the AST analyzer and a runtime introspector backed by typing.get_type_hints, then assert the two outputs match. Pattern coverage is informed by test_typing.py, but every test directly checks the contract you described.
    • Property-based testing with Hypothesis — generates random combinations of supported shapes and runs the same diff. Catches combinations no fixture lists.
    • Real-world corpus — runs the diff against every Python module in telchak/daggerverse (18 modules). All pass.

    That should gives us a similar guarantee than test_typing.py but only for what Dagger supports

  5. eunomie commented on May 5, 2026

    @eunomie
    MemberAuthor

    Regaring the property based testing, this is what it looks like. Instead of writing one fixture per pattern by hand, we describe the shape of valid inputs and let Hypothesis generate them.

    # A small grammar of what a Dagger module parameter can look like.
    @composite
    def parameter(draw):
        base = draw(sampled_from(["str", "int", "bool",
                                  "dagger.Directory", "dagger.Container"]))
        if draw(booleans()):
            base = f"Optional[{base}]"            # wrap in Optional[…]
        if draw(booleans()):
            meta = draw(sampled_from(['Doc("hi")', 'Name("alt")',
                                      'DefaultPath(".")']))
            base = f"Annotated[{base}, {meta}]"   # wrap in Annotated[…]
        return base
    
    @given(annotation=parameter())
    def test_ast_matches_runtime(annotation):
        source = f"""
            import dagger
            from typing import Annotated, Optional
            from dagger import Doc, Name, DefaultPath
    
            @dagger.object_type
            class Foo:
                @dagger.function
                def f(self, x: {annotation}) -> str: ...
        """
        ast_md = analyze_source_string(source, "Foo")     # static analysis
        runtime_md = runtime_introspect(source, "Foo")    # typing.get_type_hints
        assert_metadata_equivalent(ast_md, runtime_md)

    Hypothesis then runs this dozens or hundreds of times with combinations like:

    • Optional[Annotated[dagger.Directory, DefaultPath(".")]]
    • Annotated[int, Doc("hi"), Name("alt")]
    • dagger.Container
    • Optional[bool]
    • … and so on

    It already finds some issues.

    Regarding the different python versions, I'll see to use the matrix of python versions we already supports. Or to extend them in some way, but we already run all our tests against 5 different python versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions