Skip to content

Extend attribute docstrings to also accept comments in previous line #880

@plutonium-239

Description

@plutonium-239

🚀 Feature request

Extension of #150 to also read docstrings from comments in the previous or same line, like SimpleParsing.
So that all of these are valid:

@dataclass
class Args:
    pep_257_style: int
    """This docstring uses the PEP 257 style that is currently used in jsonargparse and docstring_parser"""

    # This docstring precedes the attribute
    preceding_line_comment_style: int

    same_line_comment_style: int  # This docstring is written right on the same line

Motivation

I do not like the current style of attribute docstrings, in my opinion it only makes sense for classes (and that too only for very long proper docstrings, not one-line help text).
In general, comments in code describing a statement/attribute are left before it (for example the complete, official documentation system of golang - godoc is comment-based, and all comments precede.).

There are cases when comments are left after a statement, however that is usually to denote what the effect of the statement was.
Like

# x is [B, C, T]
x = x.transpose(1, 2)
# x is now [B, T, C]

All in all, I really like SimpleParsing's comment doc approach (and golang's), and I would like that to also be usable in this library. Because it doesn't look like SimpleParsing is actively maintained and has lots of open issues.

Pitch

It only makes sense to add this feature in docstring_parser itself, as communicated in #150. On how to do this, we can refer to SimpleParsing's implementation, which ultimately lands on the function _get_comment_ending_at_line which takes code_lines = inspect.getsource().splitlines() and the line number of the preceding line.

If there are any better approaches with ast/etc. then please chime in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions