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

Refactor logic to support Pydantic'sField() function in dataclasses#12051

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

Open
Viicos wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromdataclass-field-fix

Conversation

Viicos
Copy link
Member

@ViicosViicos commentedJul 10, 2025
edited
Loading

Change Summary

Fixes#12045.
Part of#11613.

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

@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelJul 10, 2025
@cloudflare-workers-and-pagesCloudflare Workers and Pages
Copy link

cloudflare-workers-and-pagesbot commentedJul 10, 2025
edited
Loading

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:bceff23
Status: ✅  Deploy successful!
Preview URL:https://3fa10258.pydantic-docs.pages.dev
Branch Preview URL:https://dataclass-field-fix.pydantic-docs.pages.dev

View logs

@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedJul 10, 2025
edited
Loading

CodSpeed Performance Report

Merging#12051 willnot alter performance

Comparingdataclass-field-fix (bceff23) withmain (d156ba0)

Summary

✅ 46 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedJul 10, 2025
edited
Loading

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  dataclasses.py
  pydantic/_internal
  _dataclasses.py
Project Total 

This report was generated bypython-coverage-comment-action

@ViicosViicos added the third-party-testsAdd this label on a PR to trigger 3rd party tests labelJul 10, 2025
@ViicosViicos closed thisJul 10, 2025
@ViicosViicos reopened thisJul 10, 2025
@ViicosViicos added the needs-blogpost-entryThis PR needs to be documented in the release notes blog post labelJul 10, 2025
Copy link
Contributor

@davidhewittdavidhewitt left a comment

Choose a reason for hiding this comment

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

Wow, powerful!

If there's no alternative, moving ahead with this seems the most reasonable option to preserve correct semantics.

It would be nice if we could localise the patching just to the under-construction type somehow. I posted an idea but no idea if it actually works.

Comment on lines +275 to +278
!!! note
This approach is far from ideal, and can probably be the source of unwanted side effects/race conditions.
The previous implemented approach was mutating the `__annotations__` dict of `cls`, which is no longer a
safe operation in Python 3.14+, and resulted in unexpected behavior with field ordering anyway.
Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, scary. If this became a problem, I think we could do stuff like set the field to be a wrapper object which checks what thread it's on and only exhibits the patched behaviour on this thread, but even that is observable so why bother 😢

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Yeah I think if we ever get issues about this, we could wrap the mutation in a threading lock (similar to what I've initially tried to do in#11851). For this to be an issue, a user would have to dynamically create Pydantic dataclasses in a multi-threaded environment, and have them subclassing a stdlib dataclass with at least one field using the Pydantic'sField() function which is quite unlikely to happen.

new_dc_field.kw_only = True
if default.repr is not True:
new_dc_field.repr = default.repr
dc_fields[field_name] = new_dc_field
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder, rather than mutating the fields of the base objects, is there a world where we instead patch the class attributes of the type under construction? That might be significantly less far-reaching in potential conflict?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

The issue is that the dataclasses module is doing roughly:

fields:dict[str,Field]= {}forbincls.__mro__[-1:0:-1]:base_fields=getattr(b,'__dataclass_fields__',None)ifbase_fieldsisnotNone:forf_name,finbase_fields.items():fields[f_name]=f# Then fetch the annotations of the class under construction and build fields for it

@Viicos
Copy link
MemberAuthor

FastAPI failures are unrelated, we merged a PR that changed the JSON Schema for decimals.

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

@davidhewittdavidhewittdavidhewitt left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
needs-blogpost-entryThis PR needs to be documented in the release notes blog postrelnotes-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.

Dataclass field inconsistencies
2 participants
@Viicos@davidhewitt

[8]ページ先頭

©2009-2025 Movatter.jp