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

Fix JSON Schema generation with referenceable core schemas holding JSON metadata#11475

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 intomainfromno-type-alias-unpack
Feb 24, 2025

Conversation

Viicos
Copy link
Member

@ViicosViicos commentedFeb 21, 2025
edited
Loading

Change Summary

Fixes#11470,closes#11467.

Documentation was updated to warn about the valid usage of field metadata in PEP 695 type aliases. In the core schema generation process, we could warn/error when such metadata is encountered, but this is probably too disruptive for 2.11.

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

…ON metadataThis caused bugs in particular when using PEP 695 type aliases.
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelFeb 21, 2025
@@ -128,89 +128,6 @@ def annotated_type(tp: Any, /) -> Any | None:
return get_args(tp)[0] if is_annotated(tp) else None


def unpack_annotated(annotation: Any, /) -> tuple[Any, list[Any]]:
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This logic is not lost, it is present intyping-inspection and type aliases unpacking is configurable there

sydney-runkle reacted with thumbs up emoji
@cloudflare-workers-and-pagesCloudflare Workers and Pages
Copy link

cloudflare-workers-and-pagesbot commentedFeb 21, 2025
edited
Loading

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:df48cc9
Status: ✅  Deploy successful!
Preview URL:https://a9ece144.pydantic-docs.pages.dev
Branch Preview URL:https://no-type-alias-unpack.pydantic-docs.pages.dev

View logs

@cloudflare-workers-and-pagesCloudflare Workers and Pages

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:55d30b3
Status:⚡️  Build in progress...

View logs

@cloudflare-workers-and-pagesCloudflare Workers and Pages

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:55d30b3
Status: ✅  Deploy successful!
Preview URL:https://cd680f65.pydantic-docs.pages.dev
Branch Preview URL:https://no-type-alias-unpack.pydantic-docs.pages.dev

View logs

@github-actionsGitHub Actions
Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  fields.py
  json_schema.py
  pydantic/_internal
  _generate_schema.py
Project Total 

This report was generated bypython-coverage-comment-action

@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedFeb 21, 2025
edited
Loading

CodSpeed Performance Report

Merging#11475 willnot alter performance

Comparingno-type-alias-unpack (df48cc9) withmain (6eeb99b)

Summary

✅ 46 untouched benchmarks

@thejcannon
Copy link
Contributor

It's admittedly minor, but none of these commits attribute my changes (from#10913) to me.

Would you mind folding my commits in or usinghttps://github.blog/news-insights/product-news/commit-together-with-co-authors/

sydney-runkle reacted with heart emoji

@Viicos
Copy link
MemberAuthor

@thejcannon yes, I'm planning on adding you as a co-author but this will happen when squash merging the PR

thejcannon reacted with thumbs up emojithejcannon reacted with heart emoji

@ViicosViicos mentioned this pull requestFeb 23, 2025
5 tasks
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.

This is very much so in the typing weeds - I'm impressed with your understanding here.

Looking good generally, just left a few questions.

Comment on lines -2053 to -2061
# 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.

Are we giving up hope here?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is no longer an issue because we no longer eagerly unpack type aliases here. So all the concerns written here don't stand anymore, as if you have something like:

typeAlias=list['ForwardRef']GenerateSchema().generate_schema(Annotated[Alias, ...])

We previously tried to callunpack_annotated withAlias (and we couldn't resolve forward annotations insideunpack_annotated).

Now, We just leaveAlias as is, generate a schema for it (so it will go throughGenerateSchema._type_alias_type_schema(), and we can use the type namespace here if we have forward references), and apply the annotations after.

sydney-runkle reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Makes sense, thanks for clarifying!

if _core_utils.is_core_schema(schema_or_field):
json_schema = populate_defs(schema_or_field, json_schema)
return json_schema
return js_modify_function(schema_or_field, current_handler)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Surprised we drop the populate_defs here - why is that?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Forgot to add an explanation, but basically thisnew_handler_func is the last handler to be defined. It is the first one to be called, but by callingjs_modify_function, it will trigger the chain of calls to the inner handlers, meaning at this point (after L556, onmain), all the handlers have been called.

On L560 (on this branch), we also callpopulate_defs, meaning the now removed call topopulate_defs was redundant

sydney-runkle reacted with thumbs up emoji
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.

Approved, thanks for the clarifcations :)

@ViicosViicos merged commit67e4f59 intomainFeb 24, 2025
60 checks passed
@ViicosViicos deleted the no-type-alias-unpack branchFebruary 24, 2025 15:45
sydney-runkle pushed a commit that referenced this pull requestFeb 26, 2025
…ON metadata (#11475)This change reverts the previous solution which was to eagerly unpack type aliases during fields collection, so that in the event that those aliases where annotated forms, we could detect some field-specific metadata such as aliases or defaults.This broke the assumption that type aliases are referenceable and as such should be kept as is. An alternative fix provided by Joshua Cannon only affects JSON Schema generation and was used.The downside is that we don't support field-specific annotated metadata in such aliases, but this is now documented as expected (and generally makes sense).Co-authored-by: Joshua Cannon <3956745+thejcannon@users.noreply.github.com>
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.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Schema generation loses Field annotations when using PEP 695 type alias syntax Unpacking annotated metadata from PEP 695 type aliases
3 participants
@Viicos@thejcannon@sydney-runkle

[8]ページ先頭

©2009-2025 Movatter.jp