From 358e5824900a5fe14cace232aac5728e615862c0 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 14 May 2019 23:45:46 +0900 Subject: [PATCH] Add :abstractmethod: option to py:method directive (refs: #6138) --- CHANGES | 1 + doc/usage/restructuredtext/domains.rst | 9 +++++---- sphinx/domains/python.py | 3 +++ tests/test_domain_py.py | 16 +++++++++++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index a90602d9d..066cc6120 100644 --- a/CHANGES +++ b/CHANGES @@ -88,6 +88,7 @@ Features added * #4777: py domain: Add ``:async:`` option to :rst:dir:`py:function` directive * py domain: Add new options to :rst:dir:`py:method` directive + - ``:abstractmethod:`` - ``:async:`` - ``:classmethod:`` - ``:property:`` diff --git a/doc/usage/restructuredtext/domains.rst b/doc/usage/restructuredtext/domains.rst index 8b06bf0e0..2c2d38ee0 100644 --- a/doc/usage/restructuredtext/domains.rst +++ b/doc/usage/restructuredtext/domains.rst @@ -226,16 +226,17 @@ The following directives are provided for module and class contents: The ``async`` option can be given (with no value) to indicate the method is an async method. - The ``classmethod`` option and ``staticmethod`` option can be given (with - no value) to indicate the method is a class method (or a static method). + The ``abstractmethod``, ``classmethod`` option and ``staticmethod`` option + can be given (with no value) to indicate the method is an abstract method, + a class method or a static method. The ``property`` option can be given (with no value) to indicate the method is a property. .. versionchanged:: 2.1 - ``:async:``, ``:classmethod:``, ``:property:`` and ``:staticmethod:`` - options added. + ``:abstractmethod:``, ``:async:``, ``:classmethod:``, ``:property:`` and + ``:staticmethod:`` options added. .. rst:directive:: .. py:staticmethod:: name(parameters) diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index c4971ba60..85ab1c003 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -585,6 +585,7 @@ class PyMethod(PyObject): option_spec = PyObject.option_spec.copy() option_spec.update({ + 'abstractmethod': directives.flag, 'async': directives.flag, 'classmethod': directives.flag, 'property': directives.flag, @@ -601,6 +602,8 @@ class PyMethod(PyObject): def get_signature_prefix(self, sig): # type: (str) -> str prefix = [] + if 'abstractmethod' in self.options: + prefix.append('abstract') if 'async' in self.options: prefix.append('async') if 'classmethod' in self.options: diff --git a/tests/test_domain_py.py b/tests/test_domain_py.py index fac8a838f..12f05cf85 100644 --- a/tests/test_domain_py.py +++ b/tests/test_domain_py.py @@ -335,7 +335,9 @@ def test_pymethod_options(app): " .. py:method:: meth4\n" " :async:\n" " .. py:method:: meth5\n" - " :property:\n") + " :property:\n" + " .. py:method:: meth6\n" + " :abstractmethod:\n") domain = app.env.get_domain('py') doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, @@ -350,6 +352,8 @@ def test_pymethod_options(app): addnodes.index, desc, addnodes.index, + desc, + addnodes.index, desc)])])) # method @@ -400,6 +404,16 @@ def test_pymethod_options(app): assert 'Class.meth5' in domain.objects assert domain.objects['Class.meth5'] == ('index', 'method') + # :abstractmethod: + assert_node(doctree[1][1][10], addnodes.index, + entries=[('single', 'meth6() (Class method)', 'Class.meth6', '', None)]) + assert_node(doctree[1][1][11], ([desc_signature, ([desc_annotation, "abstract "], + [desc_name, "meth6"], + [desc_parameterlist, ()])], + [desc_content, ()])) + assert 'Class.meth6' in domain.objects + assert domain.objects['Class.meth6'] == ('index', 'method') + def test_pyclassmethod(app): text = (".. py:class:: Class\n"