Skip to content

fix(sdk/python): discover @function methods inherited from base classes - #13090

Closed
eunomie wants to merge 2 commits into
dagger:mainfrom
eunomie:sdk-python-inherited-functions
Closed

eunomie wants to merge 2 commits into
dagger:mainfrom
eunomie:sdk-python-inherited-functions

Conversation

@eunomie

@eunomie eunomie commented May 5, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fixes Python SDK: inherited @function methods from parent classes no longer registered since v0.20.7 (AST analyzer regression) #13089. Inherited @function methods stopped showing up in dagger functions / dagger call --help when the Python SDK switched from runtime introspection to a static AST analyzer in v0.20.7 (feat(sdk/python): AST based analysis to introspect #11803). The new analyzer iterated only over a class's own node.body and never walked base classes for @function methods, while inspect.getmembers() previously discovered them via Python's MRO.
  • The fix mirrors the existing _find_inherited_constructor pattern: a new _find_inherited_functions recursively walks node.bases, skips names already defined on the child (overrides win), and uses a visited set for cycle safety. Inherited functions are parsed with the child's class name so Self resolves to the child type — matching runtime behavior. _find_class_def is split so the parent's file path can also be recovered, giving inherited functions accurate location metadata.
  • Refactors the existing inheritance subtest out of TestDocs into a dedicated TestInheritance suite (first patch, no behavior change), then adds the new integration test next to it (second patch).

Test plan

  • pytest sdk/python/tests/mod/ — 178 passed (existing 175 + 3 new analyzer tests covering: issue repro, child override of an inherited @function, grandparent inheritance).
  • ruff check clean.
  • go vet ./core/integration/... clean.
  • go test ./core/integration/ -run 'TestPython/TestInheritance' — runs the two integration subtests (inherited create constructor, inherited functions) end-to-end against the dev engine.

Move the existing inheritance subtest out of TestDocs into a dedicated
TestInheritance suite so future inheritance-related tests have a clear
home. Pure refactor - no behavior change. Renames "inheritance" to the
more specific "inherited create constructor" since it tests inheritance
of the create() classmethod constructor.

Signed-off-by: Yves Brissaud <[email protected]>
Closes dagger#13089

PR dagger#11803 (v0.20.7) replaced the runtime introspection-based analyzer
with a static AST parser. The previous code relied on
inspect.getmembers(), which walks the Python MRO and naturally
discovers @function-decorated methods inherited from base classes.
The AST parser only walks node.body, missing every inherited method.

Constructor inheritance was already handled via
_find_inherited_constructor; mirror that pattern with
_find_inherited_functions so @function methods follow the same MRO
traversal. Walks node.bases recursively, skips names already defined
on the child (overrides win), and uses a visited set for cycle
safety. Inherited functions are parsed with the child's class_name
so Self annotations resolve to the child type, matching runtime
behavior.

_find_class_def is split so callers can also recover the parent's
file path; inherited functions are then parsed against that path so
location metadata points at the actual definition site.

Tests:
- AST analyzer unit tests cover the issue repro, child override, and
  multi-level (grandparent) inheritance.
- New TestInheritance/inherited functions integration test mirrors
  the issue's "Steps to reproduce" through inspectModuleObjects.

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
eunomie added a commit to eunomie/dagger that referenced this pull request May 6, 2026
PR dagger#13090 fixed inherited ``@function`` methods but left
``dagger.field()`` declarations on base classes invisible —
``_extract_class_members`` only iterated the child's ``node.body``.
A ``class Child(Base):`` where ``Base`` declared
``name: str = dagger.field(default="x")`` would have an empty
``fields=[]`` and an auto-derived constructor with no ``name``
parameter, diverging from how ``dataclasses.fields()`` walks the MRO
at runtime.

Mirror the ``_find_inherited_functions`` pattern with
``_find_inherited_fields`` that walks ``node.bases`` recursively
(visited set, override wins, parsed against the child's class name so
``Self`` resolves correctly). The walker emits both the FieldMetadata
and the matching constructor ParameterMetadata so the auto-derived
``__init__`` picks up the inherited fields too.

Closes dagger#11803 follow-up gap F1.

Signed-off-by: Yves Brissaud <[email protected]>
eunomie added a commit to eunomie/dagger that referenced this pull request May 6, 2026
PR dagger#13090 fixed inherited ``@function`` methods but left
``dagger.field()`` declarations on base classes invisible —
``_extract_class_members`` only iterated the child's ``node.body``.
A ``class Child(Base):`` where ``Base`` declared
``name: str = dagger.field(default="x")`` would have an empty
``fields=[]`` and an auto-derived constructor with no ``name``
parameter, diverging from how ``dataclasses.fields()`` walks the MRO
at runtime.

Mirror the ``_find_inherited_functions`` pattern with
``_find_inherited_fields`` that walks ``node.bases`` recursively
(visited set, override wins, parsed against the child's class name so
``Self`` resolves correctly). The walker emits both the FieldMetadata
and the matching constructor ParameterMetadata so the auto-derived
``__init__`` picks up the inherited fields too.

The walker is gated on ``_is_dataclass_like`` — at runtime, dagger
fields flow through ``dataclasses.fields(cls)``, which only inherits
from base classes that are themselves dataclasses (i.e.
``@dagger.object_type`` / ``@dagger.interface``, which apply
``dataclass`` internally, or an explicit ``@dataclass``). An
undecorated base's annotations are *not* promoted to fields on the
child. Without the gate, the analyzer claimed otherwise and produced
phantom fields the engine wouldn't actually expose; the new
differential test suite caught the divergence and a new
``test_ast_undecorated_base_does_not_inherit_fields`` locks in the
correct behaviour.

Closes dagger#11803 follow-up gap F1.

Signed-off-by: Yves Brissaud <[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.

Python SDK: inherited @function methods from parent classes no longer registered since v0.20.7 (AST analyzer regression)

1 participant