-
-
Notifications
You must be signed in to change notification settings - Fork 420
Add a Sphinx extension to document cached properties of slots classes #1519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
clayote
wants to merge
32
commits into
python-attrs:main
Choose a base branch
from
clayote:sphinx-attr-getter-ext
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+340
−5
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
6f18ee3
Add a test for the @cached_property hack
clayote f4c12de
Write a Sphinx extension to document `@cached_property` on slots classes
clayote 4dacd2c
Add news snippet
clayote 47ffde5
Fix pyproject.toml
clayote 221a0ac
Add a test for Sphinx's autodoc `:members:`
clayote 0dc4bf2
Add a weird hack to make Sphinx pretend slots for cached props aren't…
clayote b8a0bfa
Add `__eq__` and `__ne__` to `_TupleProxy`
clayote 6ba8d9a
Write docstrings for tests
clayote b5be48d
Add docstring to sphinx_cached_property.py
clayote d39bf70
"Implement" `_TupleProxy.__hash__`
clayote 876be7a
Make the `tests` dependency group depend on the `docs` one
clayote 1a5d11f
Add `test_tuple_proxy`
clayote 4ae5ce1
Delete `_TupleProxy.__ne__`
clayote 49d7b8b
Add equality assertion to `test_tuple_proxy`
clayote 7fd2ac3
Put some stuff in the tuples for `test_tuple_proxy`
clayote 7d4206a
Test hashes in `test_tuple_proxy`
clayote c550aa1
Document `sphinx_cached_property` in `how-does-it-work.md`
clayote 8bb6660
Remove the :mod: role from "Sphinx" in how-does-it-work.md
clayote 6e25315
Improve module docstring in `sphinx_cached_property.py`
clayote fe06e2f
Update changelog.d/1519.change.md
clayote fef1adf
Remove `@need_sphinx` from test_slots.py
clayote 7d3165b
Use pytest's `tmp_path` fixture for Sphinx tests
clayote 8222d2c
Make Sphinx tests more elegant with `read_text`
clayote 699d1bc
Use shutil to make Sphinx tests more elegant in test_slots.py
clayote 28da483
Remove the now-pointless import guard around Sphinx in test_slots.py
clayote 8bb2fea
Report attrs version in `attrs.sphinx_cached_property`
clayote 522ff70
Add docstrings to `sphinx_cached_property.py`
clayote 7cf6ab4
Include Sphinx &c in uv.lock
clayote 1008e7f
Fix `_TupleProxy` docstring
clayote 4b432c3
Put the Sphinx test data in its own subdir
clayote a1f6d3a
Add type hints to `sphinx_cached_property.py`
clayote 260eac3
Fix Python 3.9-incompatible union syntax
clayote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Added a Sphinx extension to show cached properties on slots classes. | ||
|
|
||
| To use it, append `'attrs.sphinx_cached_property'` to the `extensions` in your Sphinx configuration module: | ||
|
|
||
| ```python | ||
| # docs/conf.py | ||
|
|
||
| extensions += ['attrs.sphinx_cached_property'] | ||
| ``` |
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # SPDX-License-Identifier: MIT | ||
| """A Sphinx extension to document cached properties on slots classes | ||
|
|
||
| Add ``"attrs.sphinx_cached_property"`` to the ``extensions`` list in Sphinx's | ||
| conf.py to use this. Otherwise, cached properties of ``@define(slots=True)`` | ||
| classes will be inaccessible. | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from functools import cached_property | ||
| from typing import Any | ||
|
|
||
| from sphinx.application import Sphinx | ||
|
|
||
| from . import __version__ | ||
|
|
||
|
|
||
| def get_cached_property_for_member_descriptor( | ||
| cls: type, name: str, default=None | ||
| ) -> cached_property | Any: | ||
| """If the attribute is for a cached property, return the ``cached_property`` | ||
|
|
||
| Otherwise, delegate to normal ``getattr`` | ||
|
|
||
| """ | ||
| props = getattr(cls, "__attrs_cached_properties__", None) | ||
| if props is None or name not in props: | ||
| return getattr(cls, name, default) | ||
| return props[name] | ||
|
|
||
|
|
||
| def setup(app: Sphinx) -> dict[str, str | bool]: | ||
| """Install the special attribute getter for cached properties of slotted classes""" | ||
| app.add_autodoc_attrgetter( | ||
| object, get_cached_property_for_member_descriptor | ||
| ) | ||
| return { | ||
| "version": __version__, | ||
| "parallel_read_safe": True, | ||
| "parallel_write_safe": True, | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .. autoclass:: tests.test_slots.SphinxDocTest | ||
|
|
||
| .. autoproperty:: documented |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class tests.test_slots.SphinxDocTest | ||
|
|
||
| Test that slotted cached_property shows up in Sphinx docs | ||
|
|
||
| property documented | ||
|
|
||
| A very well documented function |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .. autoclass:: tests.test_slots.SphinxDocTest | ||
| :members: |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.