Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.2k
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Also clarify documentation of `model_copy()`.
cloudflare-workers-and-pagesbot commentedFeb 12, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Deploying pydantic-docs with |
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 |
pydantic/main.py Outdated
if isinstance(attr, cached_property): | ||
return _SIMPLE_SETATTR_HANDLERS['cached_property'] | ||
model_frozen = cls.model_config.get('frozen') |
There was a problem hiding this comment.
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-actionsbot commentedFeb 12, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Coverage reportClick to see where and how coverage changed
This report was generated bypython-coverage-comment-action |
codspeed-hqbot commentedFeb 12, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
CodSpeed Performance ReportMerging#11432 willnot alter performanceComparing Summary
|
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): |
There was a problem hiding this comment.
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) ?
There was a problem hiding this comment.
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
Can we add a test that's more in line with the initial issue report as well? |
There was a problem hiding this 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!
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
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). |
134bea6
intomainUh oh!
There was an error while loading.Please reload this page.
Also clarify documentation of
model_copy()
.Fixes#11428,closes#11431.
Change Summary
Related issue number
Checklist