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

Appearance settings

we should not recommend using functools.cache on methods #104112

Copy link
Copy link
Closed
@carljm

Description

@carljm
Issue body actions

Documentation

Currently the documentation for functools.cached_property recommends stacking @property and @functools.cache decorators to "achieve an effect similar to" functools.cached_property. But this omits a critical problem with using @functools.cache on instance methods: it is an unbounded cache, and will use self as a cache key, so any instance on which this "cached property" is ever accessed will be kept alive by the cache forever (unless the cache is manually cleared), and the memory used by these cached instances can grow unboundedly.

The FAQ entry on caching methods does not recommend (or even mention) functools.cache, and it explicitly recommends a bounded functools.lru_cache in order to avoid keeping alive all instances indefinitely.

Specific proposals:

  1. The cached_property documentation should not recommend using functools.cache on a method.
  2. It also doesn't need to give a full example of using lru_cache for a method cache; it can instead just mention the possibility, and link to the FAQ entry for more detailed discussion.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.