summaryrefslogtreecommitdiff
path: root/sphinx/domains/python.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2019-04-13 19:46:34 +0900
committerGitHub <noreply@github.com>2019-04-13 19:46:34 +0900
commita337cb793cf067a94f498b8842d7bc3ad96a87b2 (patch)
tree99e2eda2d64314859f30db18ff5cf6db9ee9abbd /sphinx/domains/python.py
parent817cb6e96b60b9daf2a28f57f2e9d926ade9a29b (diff)
parentb0b3f5a677162f97f7e0fb62428fa09468b0f23c (diff)
downloadsphinx-git-a337cb793cf067a94f498b8842d7bc3ad96a87b2.tar.gz
Merge pull request #6268 from tk0miya/refactor_py_domain
Refactoring python domain: Add new description classes for methods and attributes
Diffstat (limited to 'sphinx/domains/python.py')
-rw-r--r--sphinx/domains/python.py108
1 files changed, 103 insertions, 5 deletions
diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py
index 29c0c2536..de31eef00 100644
--- a/sphinx/domains/python.py
+++ b/sphinx/domains/python.py
@@ -9,13 +9,16 @@
"""
import re
+import warnings
from typing import cast
from docutils import nodes
from docutils.parsers.rst import directives
from sphinx import addnodes, locale
-from sphinx.deprecation import DeprecatedDict, RemovedInSphinx30Warning
+from sphinx.deprecation import (
+ DeprecatedDict, RemovedInSphinx30Warning, RemovedInSphinx40Warning
+)
from sphinx.directives import ObjectDescription
from sphinx.domains import Domain, ObjType, Index, IndexEntry
from sphinx.locale import _, __
@@ -453,6 +456,13 @@ class PyClassmember(PyObject):
Description of a class member (methods, attributes).
"""
+ def run(self):
+ # type: () -> List[nodes.Node]
+ warnings.warn('PyClassmember is deprecated.',
+ RemovedInSphinx40Warning)
+
+ return super().run()
+
def needs_arglist(self):
# type: () -> bool
return self.objtype.endswith('method')
@@ -523,6 +533,94 @@ class PyClassmember(PyObject):
return ''
+class PyMethod(PyObject):
+ """Description of a method."""
+
+ def needs_arglist(self):
+ # type: () -> bool
+ return True
+
+ def get_index_text(self, modname, name_cls):
+ # type: (str, Tuple[str, str]) -> str
+ name, cls = name_cls
+ try:
+ clsname, methname = name.rsplit('.', 1)
+ if modname and self.env.config.add_module_names:
+ clsname = '.'.join([modname, clsname])
+ except ValueError:
+ if modname:
+ return _('%s() (in module %s)') % (name, modname)
+ else:
+ return '%s()' % name
+
+ return _('%s() (%s method)') % (methname, clsname)
+
+
+class PyClassMethod(PyMethod):
+ """Description of a classmethod."""
+
+ def get_signature_prefix(self, sig):
+ # type: (str) -> str
+ return 'classmethod '
+
+ def get_index_text(self, modname, name_cls):
+ # type: (str, Tuple[str, str]) -> str
+ name, cls = name_cls
+ try:
+ clsname, methname = name.rsplit('.', 1)
+ if modname and self.env.config.add_module_names:
+ clsname = '.'.join([modname, clsname])
+ except ValueError:
+ if modname:
+ return _('%s() (in module %s)') % (name, modname)
+ else:
+ return '%s()' % name
+
+ return _('%s() (%s class method)') % (methname, clsname)
+
+
+class PyStaticMethod(PyMethod):
+ """Description of a staticmethod."""
+
+ def get_signature_prefix(self, sig):
+ # type: (str) -> str
+ return 'static '
+
+ def get_index_text(self, modname, name_cls):
+ # type: (str, Tuple[str, str]) -> str
+ name, cls = name_cls
+ try:
+ clsname, methname = name.rsplit('.', 1)
+ if modname and self.env.config.add_module_names:
+ clsname = '.'.join([modname, clsname])
+ except ValueError:
+ if modname:
+ return _('%s() (in module %s)') % (name, modname)
+ else:
+ return '%s()' % name
+
+ return _('%s() (%s static method)') % (methname, clsname)
+
+
+class PyAttribute(PyObject):
+ """Description of an attribute."""
+
+ def get_index_text(self, modname, name_cls):
+ # type: (str, Tuple[str, str]) -> str
+ name, cls = name_cls
+ try:
+ clsname, attrname = name.rsplit('.', 1)
+ if modname and self.env.config.add_module_names:
+ clsname = '.'.join([modname, clsname])
+ except ValueError:
+ if modname:
+ return _('%s (in module %s)') % (name, modname)
+ else:
+ return name
+
+ return _('%s (%s attribute)') % (attrname, clsname)
+
+
class PyDecoratorMixin:
"""
Mixin for decorator directives.
@@ -745,10 +843,10 @@ class PythonDomain(Domain):
'data': PyModulelevel,
'class': PyClasslike,
'exception': PyClasslike,
- 'method': PyClassmember,
- 'classmethod': PyClassmember,
- 'staticmethod': PyClassmember,
- 'attribute': PyClassmember,
+ 'method': PyMethod,
+ 'classmethod': PyClassMethod,
+ 'staticmethod': PyStaticMethod,
+ 'attribute': PyAttribute,
'module': PyModule,
'currentmodule': PyCurrentModule,
'decorator': PyDecoratorFunction,