Skip to content

Commit

Permalink
py domain: Allow to make a style for arguments of functions and metho…
Browse files Browse the repository at this point in the history
…ds (refs: sphinx-doc#6417)
  • Loading branch information
tk0miya committed Jan 4, 2020
1 parent 1d027d2 commit eaec90d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -17,6 +17,8 @@ Incompatible changes
:confval:`autosummary_generate_overwrite` to change the behavior
* #5923: autodoc: the members of ``object`` class are not documented by default
when ``:inherited-members:`` and ``:special-members:`` are given.
* #6417: py domain: doctree of desc_parameterlist has been changed. The
argument names, annotations and default values are wrapped with inline node

Deprecated
----------
Expand All @@ -32,6 +34,7 @@ Features added
not to document inherited members of the class and uppers
* #6558: glossary: emit a warning for duplicated glossary entry
* #6558: std domain: emit a warning for duplicated generic objects
* #6417: py domain: Allow to make a style for arguments of functions and methods

Bugs fixed
----------
Expand Down
24 changes: 23 additions & 1 deletion sphinx/domains/python.py
Expand Up @@ -26,6 +26,7 @@
from sphinx.domains import Domain, ObjType, Index, IndexEntry
from sphinx.environment import BuildEnvironment
from sphinx.locale import _, __
from sphinx.pycode.function import Signature
from sphinx.roles import XRefRole
from sphinx.util import logging
from sphinx.util.docfields import Field, GroupedField, TypedField
Expand Down Expand Up @@ -62,6 +63,23 @@
}


def _parse_arglist(arglist: str) -> addnodes.desc_parameterlist:
"""Parse a list of arguments using python parser"""
params = addnodes.desc_parameterlist(arglist)
sig = Signature('(%s)' % arglist)
for param in sig.parameters.values():
params += addnodes.desc_parameter()
params[-1] += nodes.inline('', param.name, classes=['argument'])
if param.annotation is not param.empty:
params[-1] += nodes.Text(': ')
params[-1] += nodes.inline('', param.annotation, classes=['annotation'])
if param.default is not param.empty:
params[-1] += nodes.Text(' = ')
params[-1] += nodes.inline('', param.default, classes=['default_value'])

return params


def _pseudo_parse_arglist(signode: desc_signature, arglist: str) -> None:
""""Parse" a list of arguments separated by commas.
Expand Down Expand Up @@ -284,7 +302,11 @@ def handle_signature(self, sig: str, signode: desc_signature) -> Tuple[str, str]

signode += addnodes.desc_name(name, name)
if arglist:
_pseudo_parse_arglist(signode, arglist)
try:
signode += _parse_arglist(arglist)
except RuntimeError:
# failed to parse arglist by python's parser
_pseudo_parse_arglist(signode, arglist)
else:
if self.needs_arglist():
# for callables, add an empty parameter list
Expand Down

0 comments on commit eaec90d

Please sign in to comment.