fix: Forward reference resolution with decorated functions#1640
Open
nickcatal wants to merge 2 commits intovitalik:masterfrom
Open
fix: Forward reference resolution with decorated functions#1640nickcatal wants to merge 2 commits intovitalik:masterfrom
nickcatal wants to merge 2 commits intovitalik:masterfrom
Conversation
Replaces type() with pydantic.create_model() when dynamically creating parameter models. This fixes PydanticUserError when using request body schemas with forward references in Pydantic v2. The type() approach doesn't properly resolve forward references, causing errors like 'BodyParams is not fully defined'. Using create_model() ensures Pydantic's model creation machinery properly handles forward references and validates schema definitions. Resolves the TODO comment at line 183 that suggested investigating create_model() for special handling.
Two complementary fixes for forward reference resolution: 1. Set __module__ on create_model() to provide namespace context for Pydantic v2 to resolve references 2. Unwrap decorated functions to get correct __globals__ so string annotations can resolve to actual schema classes Both changes are required for complete forward reference support.
Author
|
@vitalik any way we can get this merged in? Thanks! |
Author
|
@vitalik just checking in to see if we can get this in trunk, thanks! |
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.
Fixes #1424
Problem
from __future__ import annotationswith decorated functions (e.g.@paginate) causes Schema parameters to be incorrectly detected as query parameters.Solution
Two complementary fixes:
__module__parameter oncreate_model()to provide namespace context for Pydantic v2get_typed_signature()to get correct__globals__for string annotation resolutionBoth changes are required for complete forward reference support with decorators.
Tests
Added two tests in
test_signature_details.py:test_parameter_model_has_view_module- Verifies__module__parametertest_decorated_function_resolves_string_annotations- Verifies decorated function annotation resolutionAll CI checks pass (ruff, mypy, pytest).