Skip to content

FIX Handle param names ending in underscore #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 2 deletions doc/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def foo(var1, var2, long_var_name='hi'):

Returns
-------
type
Explanation of anonymous return value of type ``type``.
type_
Explanation of anonymous return value of type ``type_``.
describe : type
Explanation of return value named `describe`.
out : type
Expand Down
14 changes: 13 additions & 1 deletion numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def _str_returns(self, name='Returns'):
out += self._str_field_list(name)
out += ['']
for param, param_type, desc in self[name]:
param = self._escape_param(param)
if param_type:
out += self._str_indent([typed_fmt % (param.strip(),
param_type)])
Expand All @@ -91,6 +92,16 @@ def _str_returns(self, name='Returns'):
out += ['']
return out

def _escape_param(self, param):
"""Escape a parameter name for reST

Currently only handles param names with final underscore
"""
param = param.strip()
if param[-1:] == '_':
param = param[:-1] + '\\_'
return param

def _process_param(self, param, desc, fake_autosummary):
"""Determine how to display a parameter

Expand Down Expand Up @@ -126,7 +137,8 @@ def _process_param(self, param, desc, fake_autosummary):
relies on Sphinx's plugin mechanism.
"""
param = param.strip()
display_param = ('**%s**' if self.use_blockquotes else '%s') % param
display_param = ('**%s**' if self.use_blockquotes
else '%s') % self._escape_param(param)

if not fake_autosummary:
return display_param, desc
Expand Down
14 changes: 7 additions & 7 deletions numpydoc/tests/test_docscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


doc_txt = '''\
numpy.multivariate_normal(mean, cov, shape=None, spam=None)
numpy.multivariate_normal(mean, cov, shape=None, spam_=None)

Draw values from a multivariate normal distribution with specified
mean and covariance.
Expand Down Expand Up @@ -67,7 +67,7 @@

Other Parameters
----------------
spam : parrot
spam_ : parrot
A parrot off its mortal coil.

Raises
Expand Down Expand Up @@ -153,7 +153,7 @@

def test_signature():
assert doc['Signature'].startswith('numpy.multivariate_normal(')
assert doc['Signature'].endswith('spam=None)')
assert doc['Signature'].endswith('spam_=None)')


def test_summary():
Expand All @@ -177,7 +177,7 @@ def test_parameters():

def test_other_parameters():
assert_equal(len(doc['Other Parameters']), 1)
assert_equal([n for n,_,_ in doc['Other Parameters']], ['spam'])
assert_equal([n for n,_,_ in doc['Other Parameters']], ['spam_'])
arg, arg_type, desc = doc['Other Parameters'][0]
assert_equal(arg_type, 'parrot')
assert desc[0].startswith('A parrot off its mortal coil')
Expand Down Expand Up @@ -338,7 +338,7 @@ def test_str():
# This should be handled automatically, and so, one thing this test does
# is to make sure that See Also precedes Notes in the output.
line_by_line_compare(str(doc),
"""numpy.multivariate_normal(mean, cov, shape=None, spam=None)
"""numpy.multivariate_normal(mean, cov, shape=None, spam_=None)

Draw values from a multivariate normal distribution with specified
mean and covariance.
Expand Down Expand Up @@ -376,7 +376,7 @@ def test_str():

Other Parameters
----------------
spam : parrot
spam_ : parrot
A parrot off its mortal coil.

Raises
Expand Down Expand Up @@ -508,7 +508,7 @@ def test_sphinx_str():

:Other Parameters:

spam : parrot
spam\\_ : parrot
A parrot off its mortal coil.

:Raises:
Expand Down