Repository navigation
fix(idempotency): resolve postponed return annotations for serializer classes - #8738
Open
sumedh-aerram wants to merge 2 commits into
Conversation
… classes With `from __future__ import annotations`, every annotation is a string. idempotent_function passed function.__annotations__["return"] straight to PydanticSerializer.instantiate, which ran issubclass() on a string and raised TypeError at decoration time, so the module failed to import. DataclassSerializer had the same root cause and rejected every postponed annotation with IdempotencyModelTypeError. Resolve the return annotation with typing.get_type_hints, falling back to the annotation as written when it can't be resolved (e.g. a model defined inside another function), which keeps today's behaviour for those cases. Guard PydanticSerializer.instantiate with inspect.isclass so annotations that aren't classes, like list[Model], raise IdempotencyModelTypeError instead of TypeError.
Resolve only postponed return annotations, retaining evaluated types and custom Annotated metadata. Unrelated type-only parameters and no_type_check must not prevent serializer construction. Retrieve native deferred annotations safely on Python 3.14 and reject parameterized model aliases before subclass checks on Python 3.10.
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
|
Contributor
|
Thanks for the PR and for filling in the verification section, that helps. Heads up that the review may take a few days: this goes into 3.41.0 and I review every PR against a real service before approving, one at a time. No need to rebase or ping in the meantime, I'll come back here. |
This branch has not been deployed
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.



Issue number:
closes #8722
Summary
Changes
Resolve postponed return annotations when a serializer class infers its model. Resolve only the return type in the original function's namespace, preserve evaluated types and
Annotatedmetadata for custom serializers, and leave unrelated type-only input annotations out of inference. Python 3.14 uses public deferred-annotation retrieval. Pydantic rejects parameterized containers withIdempotencyModelTypeError, including Python 3.10 with the minimum supported Pydantic.The change adds public decorator regressions and updates both inference examples and the utility guide. Existing test expectations, serializer signatures and stored response formats are unchanged. Whole-function
get_type_hintswas rejected because it strips metadata by default, honorsno_type_checkby discarding usable returns, and fails on unrelated unresolved parameters. Container serialization is a separate feature; explicit serializer instances remain the option for locally defined models.User experience
Previously, importing a module using
from __future__ import annotationsandoutput_serializer=PydanticSerializercould fail before invocation. Pydantic and dataclass return models now infer correctly and replay without running the function again. Unsupported containers and unresolvable local output-model names receive a model-type error; supplying an explicit model avoids inference.Verification
AI assistants (Claude and Codex) helped implement this change. Local commands were executed and output inspected during development.
make -k pr ty. Alternate-runtime checks used isolated environments, including minimum Pydantic on 3.10.The final committed, clean-tree
make -k pr tyrepeated successfully (exit 0).make test-dependenciesalso passed all 15 Nox sessions on Python 3.12.14 with newly recreated environments. Independent re-review closed all five prior findings and found no new actionable regression. The reviewer separately reran the focused suite on Python 3.10/minimum Pydantic, Python 3.12, and Python 3.14; all passed and the worktree remained clean.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.