Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sphinx/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
from typing import Any, Dict, Type


class RemovedInSphinx40Warning(DeprecationWarning):
pass


class RemovedInSphinx50Warning(DeprecationWarning):
pass

Expand Down
9 changes: 6 additions & 3 deletions sphinx/domains/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,20 +783,23 @@ def get_signature_prefix(self, sig: str) -> List[nodes.Node]:

def get_index_text(self, modname: str, name_cls: Tuple[str, str]) -> str:
name, cls = name_cls
is_property = 'property' in self.options
try:
clsname, methname = name.rsplit('.', 1)
if modname and self.env.config.add_module_names:
clsname = '.'.join([modname, clsname])
except ValueError:
if modname:
if is_property:
return _('%s (in module %s)') % (name, modname)
return _('%s() (in module %s)') % (name, modname)
else:
return '%s()' % name
return name if is_property else '%s()' % name

if 'classmethod' in self.options:
return _('%s() (%s class method)') % (methname, clsname)
elif 'property' in self.options:
return _('%s() (%s property)') % (methname, clsname)
elif is_property:
return _('%s (%s property)') % (methname, clsname)
elif 'staticmethod' in self.options:
return _('%s() (%s static method)') % (methname, clsname)
else:
Expand Down
3 changes: 3 additions & 0 deletions tests/roots/test-domain-py-method-property/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project = 'domain-py-method-property'
extensions = []
master_doc = 'index'
7 changes: 7 additions & 0 deletions tests/roots/test-domain-py-method-property/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Method property index test
===========================

.. py:method:: Foo.bar
:property:

.. py:property:: Foo.baz
31 changes: 30 additions & 1 deletion tests/test_domain_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def test_pymethod_options(app):

# :property:
assert_node(doctree[1][1][8], addnodes.index,
entries=[('single', 'meth5() (Class property)', 'Class.meth5', '', None)])
entries=[('single', 'meth5 (Class property)', 'Class.meth5', '', None)])
assert_node(doctree[1][1][9], ([desc_signature, ([desc_annotation, ("property", desc_sig_space)],
[desc_name, "meth5"])],
[desc_content, ()]))
Expand Down Expand Up @@ -784,6 +784,35 @@ def test_pymethod_options(app):
assert domain.objects['Class.meth7'] == ('index', 'Class.meth7', 'method', False)


def test_pymethod_property_without_class(app):
text = (".. py:module:: example\n"
"\n"
".. py:method:: module_property\n"
" :property:\n")
domain = app.env.get_domain('py')
doctree = restructuredtext.parse(app, text)

indexes = list(doctree.traverse(addnodes.index))
entries = [entry for node in indexes for entry in node['entries']]
assert ('single', 'module_property (in module example)', 'example.module_property', '', None) in entries
for _, text, *_ in entries:
if text.startswith('module_property'):
assert '()' not in text
assert 'example.module_property' in domain.objects
assert domain.objects['example.module_property'] == ('index', 'example.module_property', 'method', False)


@pytest.mark.sphinx('html', testroot='domain-py-method-property')
def test_pymethod_property_genindex(app):
app.build()
result = (app.outdir / 'genindex.html').read_text()

assert 'bar() (Foo property)' not in result
assert 'bar (Foo property)' in result
assert 'baz() (Foo property)' not in result
assert 'baz (Foo property)' in result


def test_pyclassmethod(app):
text = (".. py:class:: Class\n"
"\n"
Expand Down