File tree Expand file tree Collapse file tree
{{cookiecutter.project_name}}/docs Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3939 ~~~~~~~~~~
4040
4141{% endif %}
42- .. autoattribute :: {{ [fullname, item] | join(".") }}
42+ .. auto{{ [fullname, item] | join(".") | member_type }} :: {{ [fullname, item] | join(".") }}
4343 {%- endfor %}
4444{% endblock %}
4545
Original file line number Diff line number Diff line change 1+ """Extension adding a jinja2 filter that determines a class member’s type."""
2+
3+ from __future__ import annotations
4+
5+ from typing import TYPE_CHECKING , Literal
6+
7+ from jinja2 .defaults import DEFAULT_FILTERS
8+ from jinja2 .utils import import_string
9+
10+ if TYPE_CHECKING :
11+ from sphinx .application import Sphinx
12+
13+
14+ def member_type (obj_path : str ) -> Literal ["method" , "property" , "attribute" ]:
15+ {% raw % }
16+ """Determine object member type.
17+
18+ E.g.: `.. auto{{ fullname | member_type }}::`
19+ """
20+ {% - endraw % }
21+ # https://jinja.palletsprojects.com/en/stable/api/#custom-filters
22+ cls_path , member_name = obj_path .rsplit ("." , 1 )
23+ cls = import_string (cls_path )
24+ member = getattr (cls , member_name , None )
25+ match member :
26+ case property ():
27+ return "property"
28+ case _ if callable (member ):
29+ return "method"
30+ case _:
31+ return "attribute"
32+
33+
34+ def setup (app : Sphinx ):
35+ """App setup hook."""
36+ DEFAULT_FILTERS ["member_type" ] = member_type
You can’t perform that action at this time.
0 commit comments