Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
Viicos merged 4 commits intomainfromfields-pep695
Dec 27, 2024

Conversation

Viicos
Copy link
Member

@ViicosViicos commentedDec 13, 2024
edited
Loading

Change Summary

Fixes#6352
Fixes#10219

This PR also makes a first step in refactoring theFieldInfo 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.

Thefrom_annotated_attribute should also be updated, but this is left for#11122.

Related issue number

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review,please add a comment including the phrase "please review" to assign reviewers

a3ng7n reacted with heart emoji
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelDec 13, 2024
@cloudflare-workers-and-pagesCloudflare Workers and Pages
Copy link

cloudflare-workers-and-pagesbot commentedDec 13, 2024
edited
Loading

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:848d3ee
Status: ✅  Deploy successful!
Preview URL:https://011878fd.pydantic-docs.pages.dev
Branch Preview URL:https://fields-pep695.pydantic-docs.pages.dev

View logs

@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedDec 13, 2024
edited
Loading

CodSpeed Performance Report

Merging#11109 willnot alter performance

Comparingfields-pep695 (848d3ee) withmain (aea47de)

Summary

✅ 46 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedDec 13, 2024
edited
Loading

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  fields.py
  pydantic/_internal
  _generate_schema.py1981,1983
  _typing_extra.py174-178,195-196
Project Total 

This report was generated bypython-coverage-comment-action

@ViicosViicosforce-pushed thefields-pep695 branch 2 times, most recently fromaed47cc toec8f53bCompareDecember 14, 2024 18:31
@ViicosViicos changed the titleEagerly evaluate type aliases if annotatedEagerly evaluate PEP 695type aliases if using theAnnotated formDec 14, 2024
@ViicosViicos changed the titleEagerly evaluate PEP 695type aliases if using theAnnotated formEagerly evaluate PEP 695 type aliases if using theAnnotated formDec 14, 2024
@ViicosViicos marked this pull request as ready for reviewDecember 14, 2024 18:36
@ViicosViicos changed the titleEagerly evaluate PEP 695 type aliases if using theAnnotated formUnpack PEP 695 type aliases if using theAnnotated form during fields collectionDec 14, 2024
Comment on lines 405 to 407
# 2. Check if the annotation is an `Annotated` form.
# In this case, `annotation` will be the annotated type:
annotation, metadata = _unpack_annotated(annotation)
Copy link
MemberAuthor

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.

sydney-runkle reacted with thumbs up emoji
@@ -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:
Copy link
MemberAuthor

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

Comment on lines +1976 to +1978
# 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.
Copy link
MemberAuthor

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.

Copy link
Contributor

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.

@ViicosViicos changed the titleUnpack PEP 695 type aliases if using theAnnotated form during fields collectionUnpack PEP 695 type aliases if using theAnnotated formDec 17, 2024
@ViicosViicos added the third-party-testsAdd this label on a PR to trigger 3rd party tests labelDec 20, 2024
Copy link
Contributor

@sydney-runklesydney-runkle left a 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.

Comment on lines +1976 to +1978
# 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.
Copy link
Contributor

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.

@sydney-runkle
Copy link
Contributor

I feel ok approving this once you add new changes because:

  • Third party tests are passing
  • Existing tests are passing
  • You've added new tests showing extended support

Copy link
Contributor

@sydney-runklesydney-runkle left a 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!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@sydney-runklesydney-runklesydney-runkle approved these changes

Assignees
No one assigned
Labels
relnotes-fixUsed for bugfixes.third-party-testsAdd this label on a PR to trigger 3rd party tests
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

defaulting behavior ofField inside ofTypeAliasType not working when used inside discriminated unionField doesn't play well withTypeAliasType
2 participants
@Viicos@sydney-runkle

[8]ページ先頭

©2009-2025 Movatter.jp