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

Add default factory with validated data to PrivateAttr#11685

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
andresliszt wants to merge10 commits intopydantic:main
base:main
Choose a base branch
Loading
fromandresliszt:add/validated-data-to-private-factory

Conversation

andresliszt
Copy link
Contributor

@andreslisztandresliszt commentedApr 2, 2025
edited
Loading

Change Summary

This PR addsdefault_factory toPrivateAttr working in a similar way that it works withField. Due that private attributes happen after init, thedefault_factory callable has available the entire__dict__ attribute. Also, private attributes definedbefore the current private are available (similar as this callable work with field)

classModel(BaseModel):x:str_y:str=PrivateAttr(default='y')_z:str=PrivateAttr(default_factory=lambdadata:data['x']+data['_y']+'z')m=Model(x='x')>>>m._z'xyz'# Fails with KeyErrorclassModel(BaseModel):x:str_y:str=PrivateAttr(default_factory=lambdadata:data['x']+data['_z']+'y')_z:str=PrivateAttr(default='z')Model(x='x')>>>KeyError

Related issue number

Fixes#10992

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

Selected Reviewer:@sydney-runkle

shadycuz reacted with thumbs up emoji
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelApr 2, 2025
@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedApr 2, 2025
edited
Loading

CodSpeed Performance Report

Merging#11685 willnot alter performance

Comparingandresliszt:add/validated-data-to-private-factory (312c1ab) withmain (bf06866)

Summary

✅ 46 untouched benchmarks

@andresliszt
Copy link
ContributorAuthor

please review

pydantic-hooky[bot] reacted with thumbs up emoji

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedApr 2, 2025
edited
Loading

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  fields.py
  pydantic/_internal
  _fields.py
  _model_construction.py
Project Total 

This report was generated bypython-coverage-comment-action

Copy link
Member

@ViicosViicos left a comment
edited
Loading

Choose a reason for hiding this comment

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

Thanks for the contribution, this is looking good. Could you also addressmissing coverage for this PR?

@@ -597,15 +597,6 @@ def deprecation_message(self) -> str | None:
return 'deprecated' if self.deprecated else None
return self.deprecated if isinstance(self.deprecated, str) else self.deprecated.message

@property
def default_factory_takes_validated_data(self) -> bool | None:
Copy link
Member

Choose a reason for hiding this comment

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

Although this was used inFieldInfo.get_default(), this is meant to be used by users as well so we should keep it.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I moved this logic inside the new common method:_fields.resolve_default_value --- Do you want to keep it as a property here and also in thePrivateAttr?

Copy link
Member

Choose a reason for hiding this comment

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

Yes please, and indeed it makes sense to have it onPrivateAttr as well.

@ViicosViicos added the awaiting author revisionawaiting changes from the PR author labelApr 4, 2025
@andresliszt
Copy link
ContributorAuthor

Sure, I will be updating it soon! Thanks

@shadycuz
Copy link

@Viicos This MR is now at 100% coverage, could you or someone else on the pydantic team please re-review it. I also need this fix. Thanks

@ViicosViicos added relnotes-feature and removed relnotes-fixUsed for bugfixes. labelsApr 23, 2025
@ViicosViicos mentioned this pull requestApr 24, 2025
13 tasks
andreslisztand others added8 commitsApril 27, 2025 10:00
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
@andreslisztandreslisztforce-pushed theadd/validated-data-to-private-factory branch from4ce1ef3 tof426ad6CompareApril 27, 2025 14:05
@andresliszt
Copy link
ContributorAuthor

@Viicos I already took care of your comments.

@Viicos
Copy link
Member

@Viicos I already took care of your comments.

Thanks, but it looks likehttps://github.com/pydantic/pydantic/pull/11685/files#r2056629172 wasn't addressed.

@andresliszt
Copy link
ContributorAuthor

@Viicos I already took care of your comments.

Thanks, but it looks likehttps://github.com/pydantic/pydantic/pull/11685/files#r2056629172 wasn't addressed.

oh ups, will be addressing this soon

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

@ViicosViicosViicos requested changes

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
awaiting author revisionawaiting changes from the PR authorrelnotes-feature
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

PrivateAttr'sdefault_factory should support taking validated data as an argument, likeField does as of Pydantic 2.10
3 participants
@andresliszt@shadycuz@Viicos

[8]ページ先頭

©2009-2025 Movatter.jp