Skip to content

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

Closed
@carljm

Description

@carljm

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 dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions