Skip to content

Commit c7ef123

Browse files
committed
Handle numpy-style *args parameters
1 parent d0f5b28 commit c7ef123

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

docstring_to_markdown/rst.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _start_block(self, language: str):
164164

165165
def consume(self, line: str):
166166
if not self._block_started:
167-
raise ValueError('Block has not started')
167+
raise ValueError('Block has not started') # pragma: no cover
168168
self._buffer.append(line)
169169

170170
def finish_consumption(self, final: bool) -> str:
@@ -399,13 +399,22 @@ def flush_buffer():
399399
# ok, we are not in any code block (it may start with the next line, but this line is clear - or empty)
400400

401401
# lists handling: items detection
402-
match = re.match(r'^(?P<argument>[^: ]+) : (?P<type>.+)$', trimmed_line)
402+
# this one does NOT allow spaces on the left hand side (to avoid false positive matches)
403+
match = re.match(r'^(?P<argument>[^:\s]+) : (?P<type>.+)$', trimmed_line)
403404
if match:
404405
line = '- `' + match.group('argument') + '`: ' + match.group('type') + ''
405406
elif most_recent_section == 'Parameters':
406407
kwargs_or_args_match = re.match(r'^(?P<other_args>\*\*kwargs|\*args)$', trimmed_line)
407408
if kwargs_or_args_match:
408409
line = '- `' + kwargs_or_args_match.group('other_args') + '`'
410+
else:
411+
numpy_args_match = re.match(
412+
r'^(?P<arg1>[^:\s]+\d), (?P<arg2>[^:\s]+\d), \.\.\. : (?P<type>.+)$',
413+
trimmed_line
414+
)
415+
if numpy_args_match:
416+
groups = numpy_args_match.groupdict()
417+
line = f'- `{groups["arg1"]}`, `{groups["arg2"]}`, `...`: {groups["type"]}'
409418
else:
410419
if trimmed_line.rstrip() in RST_SECTIONS:
411420
most_recent_section = trimmed_line.rstrip()

tests/test_rst.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,22 @@ def func(): pass
293293
For other keyword-only arguments, see the ufunc docs.
294294
"""
295295

296+
NUMPY_ARGS_PARAMETERS = """
297+
Parameters
298+
----------
299+
arys1, arys2, ... : array_like
300+
One or more input arrays.
301+
"""
302+
303+
304+
NUMPY_ARGS_PARAMETERS_MARKDOWN = """
305+
#### Parameters
306+
307+
- `arys1`, `arys2`, `...`: array_like
308+
One or more input arrays.
309+
"""
310+
311+
296312
INITIAL_SIGNATURE = """\
297313
absolute1(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
298314
@@ -467,6 +483,10 @@ def func(): pass
467483
'rst': KWARGS_PARAMETERS,
468484
'md': KWARGS_PARAMETERS_MARKDOWN
469485
},
486+
'converts numpy-style *args parameters': {
487+
'rst': NUMPY_ARGS_PARAMETERS,
488+
'md': NUMPY_ARGS_PARAMETERS_MARKDOWN
489+
},
470490
'converts signature in the first line': {
471491
'rst': INITIAL_SIGNATURE,
472492
'md': INITIAL_SIGNATURE_MARKDOWN

0 commit comments

Comments
 (0)