-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Update create_model() to support typing.Annotated as input #8947
Conversation
At the moment, dynamic model creation does not support taking input of typing.Annotated but instead only tuple(<type>, <default value>), taking in typing.Annotated allows using of comples input types for dynamic model creation.
CodSpeed Performance ReportMerging #8947 will not alter performanceComparing Summary
|
Please Review! Seems like I cannot add labels related to the pull request, would be great if I can get assistance adding the according label. Thanks! |
Moving from #8947 (comment):
Instead, I would do something like: elif _typing_extra.is_annotated(f_def):
try:
f_annotation = (
f_def.__origin__
) # __origin__ represents the annotation_type in Annotated[annotate_type, ...]
f_value = f_def.__metadata__[
0
] # Python Annotated requires at least one python variable to represent the annotation
except ValueError as e:
raise PydanticUserError(
'Field definitions should be a `typing.Annotation[type, definition]`.',
code='create-model-field-definitions',
) from e The Python version check is not necessary, as I'll note that accessing One way of doing it would be:
Considering Footnotes
|
The typing.Annotated is introduced in Python 3.9 at PEP 593 and the syntax specifies it expects a valid type annotation and a metadata. That will be applicable for Python versions >= 3.9, and for Python versions < 3.9. Also, that seems to be applicable for python version 3.8 as the current Thanks for pointing it out, it seems I can use the existing function for both |
You can use |
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.
Requested some changes. Looking great overall!
If you're able to make these changes in the next few days, we can get this into the upcoming minor release! Ping me if you have any additional questions :). We really appreciate your contribution 🚀 ! |
Hi @sydney-runkle, sorry for the late revision, I've resolved the comments please review once more! Excited to have it part of the release! |
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
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.
These changes look great. Thanks for your awesome work on this!
Change Summary
At the moment, dynamic model creation does not support taking input of typing.Annotated but instead only tuple(, ), taking in typing.Annotated allows using of complex input types for dynamic model creation. At the moment,
typing.Annotated
does not support construction information during runtime so we have to use a private class name to check whether the providedfield_definition
is atyping.Annotated
. Also, I have rather decided that it is best to ignore any additional data types or metadata other than the first and second input variables because 1. first input variable is guaranteed to be a valid input type and 2. second input variable has to be related metadata of the first input variable.Related issue number
Closes #8534
Checklist
Selected Reviewer: @dmontagu