Skip to content

Put a custom, nonfunctional @property in place of slotted @cached_property for documentation purposes#1512

Open
clayote wants to merge 51 commits intopython-attrs:mainfrom
clayote:cache_property
Open

Put a custom, nonfunctional @property in place of slotted @cached_property for documentation purposes#1512
clayote wants to merge 51 commits intopython-attrs:mainfrom
clayote:cache_property

Conversation

@clayote
Copy link

@clayote clayote commented Feb 19, 2026

Add a tiny @property in place of any deleted @cached_property in a rewritten class with slots=True. The @property won't be called, but still has the same return annotation and docstring as its @cached_property, so that help() and Sphinx can read them.

This renames the slots previously named the same as the @cached_property they were made for. They now have the suffix _cache, so that we can name the @property the same as the @cached_property.

Summary

Fixes #1325

Pull Request Check List

  • Do not open pull requests from your main branch – use a separate branch!
    • There's a ton of footguns waiting if you don't heed this warning. You can still go back to your project, create a branch from your main branch, push it, and open the pull request from the new branch.
    • This is not a pre-requisite for your pull request to be accepted, but you have been warned.
  • Added tests for changed code.
    Our CI fails if coverage is not 100%.
  • n/a Changes or additions to public APIs are reflected in our type stubs (files ending in .pyi).
    • n/a ...and used in the stub test file typing-examples/baseline.py or, if necessary, typing-examples/mypy.py.
    • n/a If they've been added to attr/__init__.pyi, they've also been re-imported in attrs/__init__.pyi.
  • Updated documentation for changed code.
    • n/a New functions/classes have to be added to docs/api.rst by hand.
    • n/a Changes to the signatures of @attr.s() and @attrs.define() have to be added by hand too.
    • ??? Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated
      Is this still a thing with strictly internal functions?
  • directives.
    The next version is the second number in the current release + 1.
    The first number represents the current year.
    So if the current version on PyPI is 22.2.0, the next version is gonna be 22.3.0.
    If the next version is the first in the new year, it'll be 23.1.0.
    - n/a If something changed that affects both attrs.define() and attr.s(), you have to add version directives to both.
  • n/a Documentation in .rst and .md files is written using semantic newlines.
  • Changes (and possible deprecations) have news fragments in changelog.d.
  • Consider granting push permissions to the PR branch, so maintainers can fix minor issues themselves without pestering you.

clayote and others added 9 commits February 19, 2026 17:43
This renames the slots previously named the same as the `@cached_property` they were made for. They now have the suffix `_cache`, so that we can name the `@property` the same as the `@cached_property`.
I'm still confused about why this code gets run at all, but at least it acts the same as cached properties now
Better than calling __getattr__ ourselves because of recursion
At least, I think this is correct. When I run the original code in a terminal, without the changes from this branch, that's how it behaves. Is my Python setup messed?
@clayote
Copy link
Author

clayote commented Feb 19, 2026

I'm not super committed to this approach; if #1488 bears fruit, this can be closed.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 19, 2026

Merging this PR will degrade performance by 86.32%

❌ 2 regressed benchmarks
✅ 13 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
test_first_access 6.4 ms 7.7 ms -17.36%
test_repeated_access 238.5 µs 1,743.1 µs -86.32%

Comparing clayote:cache_property (ab9de61) with main (c44b8b0)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (fb9ea67) during the generation of this report, so c44b8b0 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

clayote and others added 23 commits February 19, 2026 21:39
I haven't worked out why those return annotations only *sometimes* come back as `str`.
I guess this will break if the metaclass has `__slots__`? Seems unlikely...
It seemed to be getting called anyway! May as well!

This made it pretty pointless to generate a `__getattr__`, so I've removed `_make_cached_property_getattr`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

slots + cached_property + sphinx

1 participant

Comments