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+ """Determine object member type.
16+
17+ E.g.: `.. auto{{ '{{' }} fullname | member_type {{ '}}' }}::`
18+ """
19+ # https://jinja.palletsprojects.com/en/stable/api/#custom-filters
20+ cls_path , member_name = obj_path .rsplit ("." , 1 )
21+ cls = import_string (cls_path )
22+ member = getattr (cls , member_name , None )
23+ match member :
24+ case property ():
25+ return "property"
26+ case _ if callable (member ):
27+ return "method"
28+ case _:
29+ return "attribute"
30+
31+
32+ def setup (app : Sphinx ):
33+ """App setup hook."""
34+ DEFAULT_FILTERS ["member_type" ] = member_type
You can’t perform that action at this time.
0 commit comments