Skip to content
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

Add functools.cached_property backport #5031

Merged
merged 3 commits into from
Mar 1, 2022
Merged

Conversation

maffoo
Copy link
Contributor

@maffoo maffoo commented Feb 25, 2022

The @functools.cached_property decorator is really useful but not currently available in cirq since it was introduced in python 3.8 but we still support python 3.7. This adds backports.cached_property to make cached properties available in python 3.7, using import logic in cirq._compat to get it from the standard library or backport module, as needed.

@maffoo maffoo requested review from a team, vtomole and cduck as code owners February 25, 2022 23:08
@maffoo maffoo requested a review from viathor February 25, 2022 23:08
@CirqBot CirqBot added the size: S 10< lines changed <50 label Feb 25, 2022
@MichaelBroughton MichaelBroughton self-assigned this Feb 25, 2022
@MichaelBroughton
Copy link
Collaborator

Does doing something like:

@property
@functools.lru_cache(maxsize=None)
def thing(x):
   stuff(x)

not work ?

@maffoo
Copy link
Contributor Author

maffoo commented Feb 25, 2022

@MichaelBroughton, no, that doesn't work generally because it it requires all parameters to be hashable, including self. It also attaches the cache to the class, so that the cache will grow without bound if you create lots of instances. With cached_property the value is cached on the instance instead, so values get gc'd with the instances they are attached to.

@MichaelBroughton
Copy link
Collaborator

Fair enough. Last question: Why should it be Cirq's responsibility to provide this wrapper for cached_property ? Do we have use cases in the actual code base ? I don't think we should expose cirq.cached_property just as an unrelated convenience method for users otherwise.

@maffoo
Copy link
Contributor Author

maffoo commented Feb 28, 2022

Agree we shouldn't export this at the top level, that's why I put it in cirq._compat but did not export outside of that. There are quite a few places where we implement ad hoc property caching that I think could be converted to use this, but it's more of an internal implementation convenience than something we want to expose to users.

@MichaelBroughton MichaelBroughton added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Mar 1, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Mar 1, 2022
@CirqBot CirqBot merged commit 5ff22ce into master Mar 1, 2022
@CirqBot CirqBot removed the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Mar 1, 2022
@CirqBot CirqBot deleted the u/maffoo/cached-property branch March 1, 2022 00:29
@CirqBot CirqBot removed the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Mar 1, 2022
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
The [`@functools.cached_property`](https://docs.python.org/3/library/functools.html#functools.cached_property) decorator is really useful but not currently available in cirq since it was introduced in python 3.8 but we still support python 3.7. This adds `backports.cached_property` to make cached properties available in python 3.7, using import logic in `cirq._compat` to get it from the standard library or backport module, as needed.
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
The [`@functools.cached_property`](https://docs.python.org/3/library/functools.html#functools.cached_property) decorator is really useful but not currently available in cirq since it was introduced in python 3.8 but we still support python 3.7. This adds `backports.cached_property` to make cached properties available in python 3.7, using import logic in `cirq._compat` to get it from the standard library or backport module, as needed.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
The [`@functools.cached_property`](https://docs.python.org/3/library/functools.html#functools.cached_property) decorator is really useful but not currently available in cirq since it was introduced in python 3.8 but we still support python 3.7. This adds `backports.cached_property` to make cached properties available in python 3.7, using import logic in `cirq._compat` to get it from the standard library or backport module, as needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants