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

Allow cached properties to be altered on frozen models#11432

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 2 commits intomainfromcached-property-frozen
Feb 12, 2025

Conversation

Viicos
Copy link
Member

Also clarify documentation ofmodel_copy().

Fixes#11428,closes#11431.

Change Summary

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

Also clarify documentation of `model_copy()`.
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelFeb 12, 2025
@cloudflare-workers-and-pagesCloudflare Workers and Pages
Copy link

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

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:ecea8a6
Status: ✅  Deploy successful!
Preview URL:https://a644b8b1.pydantic-docs.pages.dev
Branch Preview URL:https://cached-property-frozen.pydantic-docs.pages.dev

View logs

if isinstance(attr, cached_property):
return _SIMPLE_SETATTR_HANDLERS['cached_property']

model_frozen = cls.model_config.get('frozen')
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I inlined the logic as I couldn't find a good way to keep it in a single method as I need to raise it differently in__delattr__. A bit unfortunate, but at least this removes the_check_frozen method on theBaseModel class, so it avoids polluting the namespace.

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedFeb 12, 2025
edited
Loading

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  dataclasses.py
  main.py
Project Total 

This report was generated bypython-coverage-comment-action

@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedFeb 12, 2025
edited
Loading

CodSpeed Performance Report

Merging#11432 willnot alter performance

Comparingcached-property-frozen (ecea8a6) withmain (a1f12c7)

Summary

✅ 46 untouched benchmarks

attr = getattr(cls, name, None)
# NOTE: We currently special case properties and `cached_property`, but we might need
# to generalize this to all data/non-data descriptors at some point. For non-data descriptors
# (such as `cached_property`), it isn't obvious though. `cached_property` caches the value
# to the instance's `__dict__`, but other non-data descriptors might do things differently.
if isinstance(attr, cached_property):

Choose a reason for hiding this comment

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

Can you apply the same thing that I requested in#11431 (comment) ?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

As per the comment above this line, we currently only special case@cached_property, and could extend to other descriptors (but it isn't obvious to do so, as per the comment).

We currently don't plan on changing the behavior unless someone has an explicit request for it

ddorian and sydney-runkle reacted with thumbs up emoji
@sydney-runkle
Copy link
Contributor

Can we add a test that's more in line with the initial issue report as well?

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.

Left a few nitpicks, thanks for picking up this fix!

@pydantic-hookypydantic-hookybot added the awaiting author revisionawaiting changes from the PR author labelFeb 12, 2025
@ViicosViicos removed the awaiting author revisionawaiting changes from the PR author labelFeb 12, 2025
@Viicos
Copy link
MemberAuthor

Can we add a test that's more in line with the initial issue report as well?

The initial issue report isn't really fixed as it's still up to the user to clear the cached properties (which this PR allows). We could also do it ourselves on model copy, but there are arguments to be made in favor ofnot clearing them (e.g. if the cached property is not depending on any other fields but rather caches something really expensive to compute).

sydney-runkle reacted with thumbs up emoji

@ViicosViicos merged commit134bea6 intomainFeb 12, 2025
56 checks passed
@ViicosViicos deleted the cached-property-frozen branchFebruary 12, 2025 20:02
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ddorianddorianddorian left review comments

@sydney-runklesydney-runklesydney-runkle approved these changes

Assignees

@ViicosViicos

Labels
relnotes-fixUsed for bugfixes.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Cached value of@cached_property is copied bymodel_copy(update=...), when new values in update would cause different value.
3 participants
@Viicos@sydney-runkle@ddorian

[8]ページ先頭

©2009-2025 Movatter.jp