Make Sphinx document @cached_property on slotted classes#1517
Conversation
The property objects do nothing, because they're not accessible from the instance -- slotted instances have no __dict__, but their *class object* does have __dict__, and that's the __dict__ that Sphinx looks at to find class members to document.
@cached_property@cached_property on slotted classes
@cached_property on slotted classes@cached_property on slotted classes
|
This turns out not to work for |
|
No, actually, that's intended behavior in Sphinx. I wanted some of the parent class's methods to be documented on the child class, but that's a me problem. |
|
How can we test this does anything at all? Maybe add an example to the docs somewhere? |
It turns out that the new class object's `__dict__` is only sometimes a proxy to the actual dictionary object we pass in.
for more information, see https://pre-commit.ci
|
@hynek It turns out not to be that hard to run Sphinx in a unit test. I've added one. Good thing, too, because I found that the way I was messing with the class dictionary was brittle. Thanks! |
Not the `cached_property` object itself, which is a descriptor
Merging this PR will degrade performance by 50.42%
Performance Changes
Comparing Footnotes |
|
My understanding of what class On the other hand, I've found a Sphinx feature that will do alright... |
Slotted instances have no
__dict__, but their class object does have__dict__, and that's the__dict__that Sphinx looks at to find class members to document. So I put the@cached_propertyobjects back into the class's__dict__after the class object was instantiated, and Sphinx can document them just fine.You can't use the
@cached_propertyobjects for anything else, since there's still no dictionary on any instances of the slotted class. So attrs' generated__getattr__handles their functionality like normal.Summary
Fixes #1325.
Pull Request Check List
mainbranch – use a separate branch!Our CI fails if coverage is not 100%.
.pyi).typing-examples/baseline.pyor, if necessary,typing-examples/mypy.py.attr/__init__.pyi, they've also been re-imported inattrs/__init__.pyi.docs/api.rstby hand.@attr.s()and@attrs.define()have to be added by hand too.versionadded,versionchanged, ordeprecateddirectives.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.
attrs.define()andattr.s(), you have to add version directives to both..rstand.mdfiles is written using semantic newlines.changelog.d.