#35743: Add a method to cached_property for clearing its cached value
-------------------------------+-----------------------------------------
Reporter: Jae Hyuck Sa | Owner: Jae Hyuck Sa
Type: New feature | Status: assigned
Component: Utilities | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-----------------------------------------
Comment (by Jae Hyuck Sa ):
Replying to [comment:3 Simon Charette]:
> Given that there appears to be a consensus to move to
`functools.cached_property` once we drop support for Python 3.11 (see
#30949) I would see the addition of this method as a step that prevents us
from doing that.
>
> What benefits are there in doing
>
> {{{#!python
> SomeClass.some_cached_property.clear_cache(some_class_instance)
> }}}
>
> over
>
> {{{#!python
> some_class_instance.__dict__.pop("some_cached_property", None)
> }}}
>
> when you can't be sure the property has been cached and `del` or
`delattr` otherwise?
>
> It is a pattern
[https://github.com/search?q=repo%3Adjango%2Fdjango+%22__dict__.pop%22&type=code
used extensively in the code base] but that we could admittedly document
better (#30278)
Thank you for your feedback. I was not aware of the consensus to move to
functools.cached_property once support for Python 3.11 is dropped (see
#30949). I understand that adding a method to clear the cache directly on
cached_property might hinder this transition. Additionally, I used del
because I assumed that the property would always have a cached value. I
now realize that there is a standardized pattern for clearing cached
properties, which I was not aware of before. I appreciate the feedback
and, after considering your points, I agree that better documentation is
the appropriate approach. I will raise a new issue to address this in the
documentation.
Thank you again for your valuable input.
--
Ticket URL: <https://code.djangoproject.com/ticket/35743#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070191d51a8edb-332b7053-8fba-4f3f-b155-7689e7aa4227-000000%40eu-central-1.amazonses.com.