Fix PEP 563 string annotations in JSON schema generation#1670
Merged
bendichter merged 4 commits intomainfrom Mar 3, 2026
Merged
Fix PEP 563 string annotations in JSON schema generation#1670bendichter merged 4 commits intomainfrom
bendichter merged 4 commits intomainfrom
Conversation
When a module uses `from __future__ import annotations` (PEP 563), all type annotations are stored as strings at runtime. This causes pydantic to fail when creating models from method signatures, as it cannot resolve types like `DirectoryPath` from their string form. Use `typing.get_type_hints()` to resolve string annotations back to their actual types before passing them to pydantic. Only apply this resolution when the annotation is a string, preserving pydantic-specific types like `DirectoryPath` and `FilePath` for non-PEP-563 modules. When a class is passed (rather than a method), target `__init__` for hint resolution since `inspect.signature` also uses `__init__`. Fixes MiniscopeConverter schema generation failure. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
daedb0c to
444de5d
Compare
h-mayorquin
approved these changes
Mar 3, 2026
Collaborator
h-mayorquin
left a comment
There was a problem hiding this comment.
LGTM.
I added a test and the changelog.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1670 +/- ##
==========================================
- Coverage 80.23% 80.23% -0.01%
==========================================
Files 160 160
Lines 12663 12672 +9
==========================================
+ Hits 10160 10167 +7
- Misses 2503 2505 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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
from __future__ import annotations(PEP 563), all type annotations are stored as strings at runtimeget_json_schema_from_method_signature()passes these string annotations topydantic.create_model(), which fails because pydantic cannot resolve types likeDirectoryPathfrom their string formtyping.get_type_hints()to resolve string annotations back to their actual types before passing to pydantic__init__for hint resolution sinceinspect.signaturealso uses__init__in that caseCurrently affects
MiniscopeConverter, which is the only interface usingfrom __future__ import annotations.Test plan
MiniscopeConverter.get_source_schema()succeeds instead of raisingPydanticUserError🤖 Generated with Claude Code