GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — we should not recommend using functools.cache on methods · Issue #104112 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
#104113
Closed
we should not recommend using functools.cache on methods#104112
#104113
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir

Description

@carljm
carljm
opened on May 3, 2023
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

  • gh-104112: link from cached_property docs to method-caching FAQ #104113
  • [3.11] gh-104112: link from cached_property docs to method-caching FAQ (GH-104113) #104182
Reacted by Ethan Furman, sunmy2019, Donna Gohman and Alex Waygood

Activity

  1. carljm
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on May 3, 2023
  2. carljm
    added a commit that references this issue on May 3, 2023

    pythongh-104112: link from cached_property docs to method-caching FAQ

    b6643b4
  3. sunmy2019 commented on May 3, 2023

    @sunmy2019
    sunmy2019
    on May 3, 2023
    Member
    More actions

    LGTM. May backport to earlier versions?

  4. hauntsaninja commented on May 3, 2023

    @hauntsaninja
    hauntsaninja
    on May 3, 2023
    Contributor
    More actions

    Yup, we usually backport documentation fixes to maintained Python versions.

    Jelle wrote up some general guidelines around backporting here: https://github.com/JelleZijlstra/JelleZijlstra.github.io/blob/main/cpython.md#backporting

  5. carljm
    closed this as completedin #104113on May 4, 2023
  6. carljm
    added a commit that references this issue on May 4, 2023

    gh-104112: link from cached_property docs to method-caching FAQ (#104113

    fa86a77
  7. miss-islington
    added a commit that references this issue on May 4, 2023

    pythongh-104112: link from cached_property docs to method-caching FAQ (…

    0271e1c
  8. carljm
    added 2 commits that reference this issue on May 4, 2023

    [3.11] gh-104112: link from cached_property docs to method-caching FAQ (

    bb4369e

    Merge branch 'main' into inlinecomp2

    8a78a36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.