Skip to content

Commit c4f00a7

Browse files
committed
doc: Improve docstring building
1 parent 1453426 commit c4f00a7

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

tools/schemacode/src/bidsschematools/types/_generator.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
TYPE_CHECKING = False
2020
if TYPE_CHECKING:
21-
from typing import Any, Protocol
21+
from typing import Any, Callable, Protocol
2222

2323
class Spec(Protocol):
2424
prelude: str
@@ -27,8 +27,8 @@ class Spec(Protocol):
2727
proto_prefix: str
2828

2929

30-
def with_indent(spaces: int, /):
31-
def decorator(attr):
30+
def with_indent(spaces: int, /) -> Callable[[str], str]:
31+
def decorator(attr: str) -> str:
3232
return indent(dedent(attr), " " * spaces)
3333

3434
return decorator
@@ -146,12 +146,13 @@ def create_protocol_source(
146146
classes: dict[str, str],
147147
) -> str:
148148
class_name = snake_to_pascal(class_name)
149-
docstring = f"""\
150-
{metadata.get("description", "").strip()}
149+
docstring = metadata.get("description", "").strip()
150+
if "@property" not in template.attr_def:
151+
docstring += with_indent(4)("""
151152
152-
Attributes
153-
----------
154-
"""
153+
Attributes
154+
----------
155+
""")
155156

156157
required = metadata.get("required", {})
157158
optional = [prop for prop in properties if prop not in required]
@@ -172,7 +173,13 @@ def create_protocol_source(
172173
name=prop_name, type=type_, docstring=description, default=default
173174
)
174175
)
175-
docstring += f"{prop_name}: {type_}\n\t{description}\n"
176+
# Avoid double-documenting properties
177+
if "@property" not in template.attr_def:
178+
docstring += with_indent(4)(f"""\
179+
{prop_name}: {type_}
180+
{description}
181+
182+
""")
176183

177184
lines.insert(0, template.class_def.format(name=class_name, docstring=docstring))
178185

0 commit comments

Comments
 (0)