fix(types): resolve forward-reference return annotations via get_type_hints#1350
Open
devteamaegis wants to merge 2 commits into
Open
fix(types): resolve forward-reference return annotations via get_type_hints#1350devteamaegis wants to merge 2 commits into
devteamaegis wants to merge 2 commits into
Conversation
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
Closes #950
When a module uses
from __future__ import annotations(PEP 563), all annotations are stored as lazy strings at runtime.inspect.signature().return_annotationtherefore returns the raw string (e.g.'Recipe') rather than the actual class. This meantPythonFunction.from_functionstored'Recipe'(a string) asreturn_annotation, which broke any downstream code that tried to construct a Pydantic schema from it.Root cause:
inspect.signature()preserves the string literal as-is under PEP 563. Onlytyping.get_type_hints()resolves the forward reference back to the real type.Fix
In
PythonFunction.from_function, replace:with a
get_type_hints()call that resolves the annotation, falling back tosig.return_annotationif resolution fails (e.g. the type is not importable in the current scope):Tests
Two regression tests added in
tests/basic/utilities/test_types.py::TestPythonFunctionFromFutureAnnotations:test_string_annotation_is_resolved_to_type— string annotation-> "Dish"must resolve to theDishclasstest_non_string_annotation_still_works— normal-> list[Ingredient]must continue to work unchanged