python / python/cpython

functools.cached_property doc warnings about key-sharing are obsolete

Open
#101,815 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Documentation

The documentation for functools.cached_property currently says "this decorator interferes with the operation of PEP 412 key-sharing dictionaries. This means that instance dictionaries can take more space than usual." This warning is no longer accurate as of Python 3.12; dict key-sharing is now resilient to instance attributes being created in different orders across different instances of the same type, as happens with cached properties; this no longer causes keys to become unshared for the type. The different keys are just appended to the shared keys for the type and some instances may store a NULL for some keys.

It remains true that cached_property can cause instances to use slightly more memory, but the reason for this has changed (and the extra memory used will be significantly less.) The new reason is that any access of __dict__ on an instance will force creation of a real dictionary object for that instance, rather than just a PyDictValues. The keys remain shared in the created dict, though. So the added memory use is no longer "all the keys" but rather just "a PyDictObject" (which consists of only three fields.)

I'll file a PR to update the wording in the docs so that it is accurate to the new behavior.

(Accessing __dict__ in cached_property is probably not strictly required, but that's a topic for a separate issue/PR.)

Linked PRs
  • gh-101816

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the functools.cached_property documentation wording and linked PR gh-101816, using the issue's description of Python 3.12 key-sharing behavior as context. Done means the obsolete PEP 412 warning is replaced with an accurate explanation of the remaining memory impact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.