Skip to content

Commit 1205255

Browse files
committed
Cloase #9445: :py:property: directive now supports :classmethod: option
Since python 3.9, `classmethod` starts to support creating a "class property". This allows to describe it.
1 parent 939c7bb commit 1205255

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Deprecated
1313
Features added
1414
--------------
1515

16+
* #9445: py domain: ``:py:property:`` directive supports ``:classmethod:``
17+
option to describe the class property
18+
1619
Bugs fixed
1720
----------
1821

doc/usage/restructuredtext/domains.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,13 @@ The following directives are provided for module and class contents:
329329
330330
Indicate the property is abstract.
331331
332+
.. rst:directive:option:: classmethod
333+
:type: no value
334+
335+
Indicate the property is a classmethod.
336+
337+
.. versionaddedd: 4.2
338+
332339
.. rst:directive:option:: type: type of the property
333340
:type: text
334341

sphinx/domains/python.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ class PyProperty(PyObject):
852852
option_spec = PyObject.option_spec.copy()
853853
option_spec.update({
854854
'abstractmethod': directives.flag,
855+
'classmethod': directives.flag,
855856
'type': directives.unchanged,
856857
})
857858

@@ -865,10 +866,13 @@ def handle_signature(self, sig: str, signode: desc_signature) -> Tuple[str, str]
865866
return fullname, prefix
866867

867868
def get_signature_prefix(self, sig: str) -> str:
868-
prefix = ['property']
869+
prefix = []
869870
if 'abstractmethod' in self.options:
870-
prefix.insert(0, 'abstract')
871+
prefix.append('abstract')
872+
if 'classmethod' in self.options:
873+
prefix.append('class')
871874

875+
prefix.append('property')
872876
return ' '.join(prefix) + ' '
873877

874878
def get_index_text(self, modname: str, name_cls: Tuple[str, str]) -> str:

tests/test_domain_py.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -813,24 +813,38 @@ def test_pyattribute(app):
813813
def test_pyproperty(app):
814814
text = (".. py:class:: Class\n"
815815
"\n"
816-
" .. py:property:: prop\n"
816+
" .. py:property:: prop1\n"
817817
" :abstractmethod:\n"
818+
" :type: str\n"
819+
"\n"
820+
" .. py:property:: prop2\n"
821+
" :classmethod:\n"
818822
" :type: str\n")
819823
domain = app.env.get_domain('py')
820824
doctree = restructuredtext.parse(app, text)
821825
assert_node(doctree, (addnodes.index,
822826
[desc, ([desc_signature, ([desc_annotation, "class "],
823827
[desc_name, "Class"])],
824828
[desc_content, (addnodes.index,
829+
desc,
830+
addnodes.index,
825831
desc)])]))
826832
assert_node(doctree[1][1][0], addnodes.index,
827-
entries=[('single', 'prop (Class property)', 'Class.prop', '', None)])
833+
entries=[('single', 'prop1 (Class property)', 'Class.prop1', '', None)])
828834
assert_node(doctree[1][1][1], ([desc_signature, ([desc_annotation, "abstract property "],
829-
[desc_name, "prop"],
835+
[desc_name, "prop1"],
836+
[desc_annotation, ": str"])],
837+
[desc_content, ()]))
838+
assert_node(doctree[1][1][2], addnodes.index,
839+
entries=[('single', 'prop2 (Class property)', 'Class.prop2', '', None)])
840+
assert_node(doctree[1][1][3], ([desc_signature, ([desc_annotation, "class property "],
841+
[desc_name, "prop2"],
830842
[desc_annotation, ": str"])],
831843
[desc_content, ()]))
832-
assert 'Class.prop' in domain.objects
833-
assert domain.objects['Class.prop'] == ('index', 'Class.prop', 'property', False)
844+
assert 'Class.prop1' in domain.objects
845+
assert domain.objects['Class.prop1'] == ('index', 'Class.prop1', 'property', False)
846+
assert 'Class.prop2' in domain.objects
847+
assert domain.objects['Class.prop2'] == ('index', 'Class.prop2', 'property', False)
834848

835849

836850
def test_pydecorator_signature(app):

0 commit comments

Comments
 (0)