-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from DanCardin/dc/attribute-docstring
feat: Allow "attribute docstrings" as additional method of documenting args.
- Loading branch information
Showing
7 changed files
with
219 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from __future__ import annotations | ||
|
||
import ast | ||
import inspect | ||
import textwrap | ||
import typing | ||
from dataclasses import dataclass | ||
from types import ModuleType | ||
|
||
from typing_extensions import Self | ||
|
||
try: | ||
import docstring_parser as _docstring_parser | ||
|
||
docstring_parser: ModuleType | None = _docstring_parser | ||
except ImportError: # pragma: no cover | ||
docstring_parser = None | ||
|
||
|
||
@dataclass | ||
class ClassHelpText: | ||
summary: str | None | ||
body: str | None | ||
args: dict[str, str] | ||
|
||
@classmethod | ||
def collect(cls, command: type) -> Self: | ||
args = {} | ||
|
||
doc = get_doc(command) | ||
if docstring_parser: | ||
parsed_help = docstring_parser.parse(doc) | ||
for param in parsed_help.params: | ||
args[param.arg_name] = param.description | ||
summary = parsed_help.short_description | ||
body = parsed_help.long_description | ||
else: | ||
doc = inspect.cleandoc(doc).split("\n", 1) | ||
if len(doc) == 1: | ||
summary = doc[0] | ||
body = "" | ||
else: | ||
summary, body = doc | ||
body = body.strip() | ||
|
||
ast_args = get_attribute_docstrings(command) | ||
args.update(ast_args) | ||
|
||
return cls(summary=summary, body=body, args=args) | ||
|
||
|
||
def get_doc(cls): | ||
"""Lifted from dataclasses source.""" | ||
doc = cls.__doc__ or "" | ||
|
||
# Dataclasses will set the doc attribute to the below value if there was no | ||
# explicit docstring. This is just annoying for us, so we treat that as though | ||
# there wasn't one. | ||
try: | ||
# In some cases fetching a signature is not possible. | ||
# But, we surely should not fail in this case. | ||
text_sig = str(inspect.signature(cls)).replace(" -> None", "") | ||
except (TypeError, ValueError): # pragma: no cover | ||
text_sig = "" | ||
|
||
dataclasses_docstring = cls.__name__ + text_sig | ||
|
||
if doc == dataclasses_docstring: | ||
return "" | ||
return doc | ||
|
||
|
||
def get_attribute_docstrings(command: type) -> dict[str, str]: | ||
result: dict[str, str] = {} | ||
|
||
try: | ||
raw_source = inspect.getsource(command) | ||
except OSError: | ||
return result | ||
|
||
source = textwrap.dedent(raw_source) | ||
module = ast.parse(source) | ||
|
||
cls_node = module.body[0] | ||
assert isinstance(cls_node, ast.ClassDef) | ||
|
||
last_assignment: ast.AnnAssign | None = None | ||
for node in cls_node.body: | ||
if isinstance(node, ast.Expr): | ||
if last_assignment: | ||
name = typing.cast(ast.Name, last_assignment.target).id | ||
value = typing.cast(ast.Constant, node.value).value | ||
result[name] = value | ||
continue | ||
|
||
last_assignment = node if isinstance(node, ast.AnnAssign) else None | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters