Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.2k
Unpack PEP 695 type aliases if using theAnnotated
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
cloudflare-workers-and-pagesbot commentedDec 13, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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-hqbot commentedDec 13, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
CodSpeed Performance ReportMerging#11109 willnot alter performanceComparing Summary
|
github-actionsbot commentedDec 13, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Coverage reportClick to see where and how coverage changed
This report was generated bypython-coverage-comment-action |
aed47cc
toec8f53b
CompareAnnotated
formAnnotated
formAnnotated
formAnnotated
formAnnotated
form during fields collectionpydantic/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)
ifmetadata == []
, it means_typing_extra.is_annotated(annotation) == False
.
Uh oh!
There was an error while loading.Please reload this page.
@@ -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.
Seetest_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
formThere 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.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
# 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.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
I feel ok approving this once you add new changes because:
|
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!
b236291
intomainUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
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