Skip to content

Commit 653fd30

Browse files
MarkDaoustcopybara-github
authored andcommitted
Fix duplicate entries caused by types in the Args: block
PiperOrigin-RevId: 530431744
1 parent abfbe6e commit 653fd30

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

tools/tensorflow_docs/api_generator/parser.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,11 @@ def __str__(self) -> str:
381381
r"""
382382
^(\*?\*?'?"? # Optional * to allow *args, **kwargs and quotes
383383
\w[\w.'"]*? # words, dots and closing quotes
384-
(?:[ ]?[\(\]][\(\[\w.\)\]]*?)? # maybe a space and some words in parens.
384+
(?: # non capturing
385+
[ ]? # maybe a space
386+
\( # a `(`
387+
[\ \(\[\w.,\)\]]*? # word chars, dots or more open/close or space
388+
)? # all optional
385389
)\s*:\s # Allow any whitespace around the colon.""",
386390
re.MULTILINE | re.VERBOSE,
387391
)
@@ -447,11 +451,26 @@ def split_string(cls, docstring: str):
447451
text = split.pop(0)
448452
items = _pairs(split)
449453

454+
items = list(cls._split_items(items))
455+
450456
title_block = cls(title=title, text=text, items=items)
451457
parts.append(title_block)
452458

453459
return parts
454460

461+
@classmethod
462+
def _split_items(
463+
cls, items: Iterable[Tuple[str, str]]
464+
) -> Iterable[Tuple[str, str]]:
465+
"""If there's a type in the name, move it to the top of the description."""
466+
for name, value in items:
467+
if '(' in name:
468+
name, type_str = re.split(r' ?[\(]', name, maxsplit=1)
469+
type_str = f"`{type_str.rstrip(')')}`"
470+
value = f'{type_str}\n\n{value}'
471+
472+
yield name, value
473+
455474

456475
class DocstringInfo(typing.NamedTuple):
457476
brief: str

tools/tensorflow_docs/api_generator/parser_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,8 @@ def test_title_block(self):
884884
block = docstring_parts[1]
885885
self.assertEqual('\nextra paragraph?\n', block.text)
886886
self.assertEqual('item', block.items[0][0])
887-
self.assertEqual('item2 (int)', block.items[1][0])
887+
self.assertEqual('item2', block.items[1][0])
888+
self.assertStartsWith(block.items[1][1], '`int`')
888889
self.assertLen(block.items, 2)
889890

890891
def test_strip_todos(self):

0 commit comments

Comments
 (0)