Expose cached properties of slotted classes via __attrs_cached_properties__ - #1613
Open
cjchanh wants to merge 1 commit into
Open
Expose cached properties of slotted classes via __attrs_cached_properties__#1613cjchanh wants to merge 1 commit into
__attrs_cached_properties__#1613cjchanh wants to merge 1 commit into
Conversation
…ties__ Slotted classes now carry a public __attrs_cached_properties__ class attribute: a plain dict mapping the names of the cached_property functions defined on the class to the original functions. It is the same mapping the generated __getattr__ uses, so accessing it never evaluates the properties. Non-slotted classes are unaffected. Documented in docs/extending.md and changelog.d/1532.change.md. Fixes python-attrs#1532
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Expose cached properties of slotted classes via
__attrs_cached_properties__Fixes #1532.
Summary
Slotted classes created by attrs now carry a public
__attrs_cached_properties__class attribute: a plaindictthat maps the names of thecached_propertyfunctions defined on the class to the original functions.This gives tooling (documentation generators, debuggers, serializers, framework code) a stable, documented way to introspect cached properties on attrs slotted classes — something that is currently only possible by reading the private generated
__getattr__closure.Public surface choice
__attrs_cached_properties__follows the existing__attrs_*dunder family (__attrs_attrs__,__attrs_own_setattr__,__attrs_init__, …), signalling that it is part of the attrs runtime contract on generated classes.__getattr__reads for cached-property dispatch, so accessing the attribute copies no state and calls none of the functions. It contains the original function objects (cached_property.func), which never triggers evaluation. This is documented in the docs note.__mro__._ClassBuilder._create_slots_class(), so non-slotted classes are completely unaffected (they don't grow the attribute at all). Every slotted class gets it, including those with no cached properties (an empty dict), so consumers never need ahasattrcheck.Implementation
src/attr/_make.py— one assignment in_create_slots_class()exposing the existingcached_propertiesdict as a class attribute.src/attr/__init__.py— unchanged: the new surface is a class attribute on generated classes, so no module-level export is needed.Docs & changelog
docs/extending.md— new "Cached Properties" section with a runnable doctest, own-only/MRO guidance, and a note about the shared mapping.changelog.d/1532.change.md— towncrier fragment (validated withtowncrier build --draft).Tests
Five new tests in
tests/test_slots.py:Verification
All green on
matrix/attrs1532-arm6(base:30fd617, feature commita440453):Not run: docs build (Sphinx) and pyright/pyrefly typing gates — out of scope here; the change is runtime-only with no typing surface. Doctest was executed manually and matches the documented output shape.