-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Refactor func_metadata()
implementation
#1496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
"typing-extensions>=4.9.0", | ||
"typing-inspection>=0.4.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typing-extensions
is already used in the project, but not declared as an explicit dependency. 4.9.0 is the oldest version with (proper) support for typing_extensions.deprecated
.
typing-inspection
is already a Pydantic dependency, so nothing new added here.
if type_hints: | ||
# Classes with type hints can be converted to Pydantic models | ||
model = _create_model_from_class(type_annotation) | ||
model = _create_model_from_class(type_annotation, type_hints) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_create_model_from_class()
used to compute the type hints again, pass them to avoid unnecessarily computing them again.
def _get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: | ||
"""Get function signature while evaluating forward references""" | ||
signature = inspect.signature(call) | ||
globalns = getattr(call, "__globals__", {}) | ||
typed_params = [ | ||
inspect.Parameter( | ||
name=param.name, | ||
kind=param.kind, | ||
default=param.default, | ||
annotation=_get_typed_annotation(param.annotation, globalns), | ||
) | ||
for param in signature.parameters.values() | ||
] | ||
typed_return = _get_typed_annotation(signature.return_annotation, globalns) | ||
typed_signature = inspect.Signature(typed_params, return_annotation=typed_return) | ||
return typed_signature |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed, we can just use inspect.get_annotations()
with eval_str=True
.
It also enables more use cases to be supported, e.g.:
from __future__ import annotations
def func[T](a: T): ...
from_metadata(func) # Used to raise
Let me know if I should add a test for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR keeps the support for Pydantic 2.11.
@Viicos is maintaining Pydantic, so he knows stuff.
5b71783
to
5dff9fa
Compare
5dff9fa
to
cf3ae5c
Compare
Motivation and Context
This is a first step in adding support for Pydantic 2.12 and Python 3.14. It avoids relying on patterns that we discourage using in Pydantic (mostly around the
FieldInfo
class), and removes usage of private utilities.How Has This Been Tested?
Breaking Changes
Types of changes
Checklist
Additional context