-
-
Notifications
You must be signed in to change notification settings - Fork 79
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make cached_property
derive from property
#137
base: main
Are you sure you want to change the base?
Conversation
6c2af32
to
f3ac288
Compare
Rebased to latest |
@pydanny : Any reason to stall this ? |
Sounds like a clear improvement. |
For anyone interested (@carver, @bashtage ?): this has been released on PyPI ( |
Looks good to me. Should also fix #168 |
Thanks for the heads up @vbraun .
|
@Qu4tro : seems like you have a slotted object but it doesn't have |
I've updated https://github.com/Qu4tro/cachedproperty_immutable_classes with Yeah, it's derived from NamedTuple. I guess the only to do it now, since python 3.5.1, is to maintain an external store to save the cache to? |
@Qu4tro : maybe you could open an issue on python/typing? I don't know if Otherwise, you could write a metaclass that does the same as |
@althonos : I've created the ticket. Thanks for the advice. |
@althonos Sorry about that - seems to be something else! |
Wasn't that bad. Notified me of this thread which I had forgotten to type into some findings. Basically, if anyone has the same issue as I do: caching NamedTuples properties, you can totally use If anyone knows any other issue to this approach it would also be great to hear. |
By using the weak dictionary in the decorating class, compatibility with any non-hashable types is lost. |
@matfax : but in exchange you gain support for slotted classes, that |
@althonos I do not doubt that it would be a valuable addition. But maybe having both options in one library would be the wiser choice instead of superseding the current one so that dependents that rely on this library for having support for non-hashables do not become trapped in an old version. After all, there are valid use cases in that hashes can not be more than a purely random number. This package seems to be widely adopted. There would be quite an affair if existing builds stopped working after a version bump. |
Hi @pydanny ! Since I'm using this library on a daily basis I figured it would be time to contribute at last 😉
Class hierarchy (#26, #27)
cached_property
now derives fromproperty
.threaded_cached_property
andcached_property_with_ttl
arecached_property
subclasses.threaded_cached_property_with_ttl
is a subclass of bothcached_property_with_ttl
andthreaded_cached_property
.This is not as drastic as #30 suggests but still helps avoiding code duplication.
Support
__slots__
and stop usingobj.__dict__
(#69, #123)The
cached_property
does not use the object__dict__
to store the cached value, instead using aWeakKeyDictionary
with the object as the key. This allows supportings slotted classes as long as we can createweakref
to the object, i.e."__weakref__"
is in__slots__
. Not usingweakrefs
would cause objects not to be garbage collected.Better function wrapping (#72)
In Python 3,
functools.update_wrapper
is now used to extract attributes from the wrapped function. This means thecached_property
will also expose the__qualname__
and__annotations__
of the wrapped function.Miscellaneous (#124)
Supersedes #124, so that you don't have to resolve the merge yourself (I added @VadimPushtaev to the
AUTHORS.rst
since I discovered the__set_name__
protocol thanks to his PR, and I think he deserves some praise 😄 )