-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Unpack PEP 695 type aliases if using the Annotated
form
#11109
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
Conversation
Deploying pydantic-docs with
|
Latest commit: |
848d3ee
|
Status: | ✅ Deploy successful! |
Preview URL: | https://011878fd.pydantic-docs.pages.dev |
Branch Preview URL: | https://fields-pep695.pydantic-docs.pages.dev |
CodSpeed Performance ReportMerging #11109 will not alter performanceComparing Summary
|
a999599
to
7987e26
Compare
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
aed47cc
to
ec8f53b
Compare
Annotated
form
Annotated
formAnnotated
form
ec8f53b
to
da649cd
Compare
Annotated
formAnnotated
form during fields collection
pydantic/fields.py
Outdated
# 2. Check if the annotation is an `Annotated` form. | ||
# In this case, `annotation` will be the annotated type: | ||
annotation, metadata = _unpack_annotated(annotation) |
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 replaces:
if _typing_extra.is_annotated(annotation):
first_arg, *extra_args = typing_extensions.get_args(annotation)
if metadata == []
, it means _typing_extra.is_annotated(annotation) == False
.
@@ -314,28 +314,14 @@ def test_recursive_generic_type_alias_annotated_defs() -> None: | |||
} | |||
|
|||
|
|||
@pytest.mark.xfail(reason='description is currently dropped') | |||
def test_field() -> None: |
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.
Now works, was moved below
# Ideally, we should delegate all this to `_typing_extra.unpack_annotated`, e.g.: | ||
# `typ, annotations = _typing_extra.unpack_annotated(annotated_type); schema = self.apply_annotations(...)` | ||
# if it was able to use a `NsResolver`. But because `unpack_annotated` is also used | ||
# when constructing `FieldInfo` instances (where we don't have access to a `NsResolver`), | ||
# the implementation of the function does *not* resolve forward annotations. This could | ||
# be solved by calling `unpack_annotated` directly inside `collect_model_fields`. | ||
# For now, we at least resolve the annotated type if it is a forward ref, but note that | ||
# unexpected results will happen if you have something like `Annotated[Alias, ...]` and | ||
# `Alias` is a PEP 695 type alias containing forward references. |
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.
See test_nested_annotated_with_type_aliases_and_forward_ref
showing an example of when it fails.
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.
Yeesh. This comment is helpful, as this logic is pretty specialized and complicated. Thanks for including.
I honestly don't know if my mind is fully wrapped around this yet.
Annotated
form during fields collectionAnnotated
form
389f32a
to
3153ed2
Compare
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.
I'm very impressed with your understanding of these nuances and edge cases here.
I've left a few questions / comments. Generally, looks good to me. Happy to do another quick pass after you've looked at feedback.
# Ideally, we should delegate all this to `_typing_extra.unpack_annotated`, e.g.: | ||
# `typ, annotations = _typing_extra.unpack_annotated(annotated_type); schema = self.apply_annotations(...)` | ||
# if it was able to use a `NsResolver`. But because `unpack_annotated` is also used | ||
# when constructing `FieldInfo` instances (where we don't have access to a `NsResolver`), | ||
# the implementation of the function does *not* resolve forward annotations. This could | ||
# be solved by calling `unpack_annotated` directly inside `collect_model_fields`. | ||
# For now, we at least resolve the annotated type if it is a forward ref, but note that | ||
# unexpected results will happen if you have something like `Annotated[Alias, ...]` and | ||
# `Alias` is a PEP 695 type alias containing forward references. |
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.
Yeesh. This comment is helpful, as this logic is pretty specialized and complicated. Thanks for including.
I honestly don't know if my mind is fully wrapped around this yet.
I feel ok approving this once you add new changes because:
|
29b3705
to
848d3ee
Compare
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.
Feedback commit looks good. Thanks for digging into this!
Change Summary
Fixes #6352
Fixes #10219
This PR also makes a first step in refactoring the
FieldInfo
class (see #11122), at least for thefrom_annotation
constructor method. The method logic is alsmost untouched, it was just adapted to use the new_unpack_annotated
utility function (also some variables had to be renamed) and comments were added to better describe the logic.The
from_annotated_attribute
should also be updated, but this is left for #11122.Related issue number
Checklist