Skip to content
Open
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
11 changes: 1 addition & 10 deletions src/tyro/_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,11 @@ def get_callable_description(f: Callable) -> str:
if docstring is None:
return ""

docstring = _strings.dedent(docstring)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP-257 guarantees that this step is not necessary!


if dataclasses.is_dataclass(f):
default_doc = f.__name__ + str(inspect.signature(f)).replace(" -> None", "") # type: ignore
if docstring == default_doc:
return ""

import docstring_parser

parsed_docstring = docstring_parser.parse(docstring)

parts: List[str] = []
if parsed_docstring.short_description is not None:
parts.append(parsed_docstring.short_description)
if parsed_docstring.long_description is not None:
parts.append(parsed_docstring.long_description)
return "\n".join(parts)
return docstring_parser.parse(docstring).description
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one line does the same as the above, except it doesn't remove the carriage return between the first and second paragraphs.

It's also conceptually simpler to do exactly what docstring_parser does.

16 changes: 16 additions & 0 deletions tests/test_helptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ class Helptext:
assert "Documentation 3 (default: 3)" in helptext


def test_helptext_paragraphs() -> None:
@dataclasses.dataclass
class Helptext:
"""
First

Second

Third
"""

usage, *lines = get_helptext_with_checks(Helptext).split("\n")
assert usage.startswith("usage:")
assert lines[:7] == ["", "First", "", "Second", "", "Third", ""]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this commit, this code would have read:

assert lines[:5] == ["", "First Second", "", "Third", ""]



def test_helptext_sphinx_autodoc_style() -> None:
@dataclasses.dataclass
class Helptext:
Expand Down