Skip to content

Commit 0326005

Browse files
committed
fix: Fix dedent for attributes docstrings
Issue mkdocstrings#225: mkdocstrings/mkdocstrings#225
1 parent a298616 commit 0326005

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Diff for: src/pytkdocs/parsers/attributes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def get_pairs(nodes):
8888
def get_module_or_class_attributes(nodes):
8989
result = {}
9090
for assignment, string_node in get_pairs(nodes):
91-
string = dedent(string_node.s) if string_node else None
91+
string = inspect.cleandoc(string_node.s) if string_node else None
9292
if isinstance(assignment, ast.Assign):
9393
names = []
9494
for target in assignment.targets:
@@ -164,7 +164,7 @@ def get_instance_attributes(func):
164164
if not names or (string is None and annotation is None):
165165
continue
166166

167-
docstring = dedent(string.s) if string else None
167+
docstring = inspect.cleandoc(string.s) if string else None
168168
for name in names:
169169
result[name] = {"annotation": annotation, "docstring": docstring}
170170

Diff for: tests/fixtures/parsing/attributes.py

+13
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ class E:
166166

167167
both_class_and_instance_attribute: Optional[bool] = None
168168

169+
DEDENT = 0
170+
"""This docstring starts immediately (no blank line).
171+
Use `inspect.cleandoc` instead of `textwrap.dedent`."""
172+
169173
def __init__(self):
170174
"""
171175
Init doctring.
@@ -216,6 +220,10 @@ def __init__(self):
216220
self.c.non_self_attribute2: int = 0
217221
"""Non self attribute 2."""
218222

223+
self.dedent = 0
224+
"""This docstring starts immediately (no blank line).
225+
Use `inspect.cleandoc` instead of `textwrap.dedent`."""
226+
219227

220228
if True:
221229
IN_IF: bytes = b""
@@ -258,3 +266,8 @@ class MarshmallowSchema(Schema):
258266

259267

260268
OK, WARNING, CRITICAL, UNKNOWN = 0, 0, 0, 0
269+
270+
if True:
271+
DEDENT = 0
272+
"""This docstring starts immediately (no blank line).
273+
Use `inspect.cleandoc` instead of `textwrap.dedent`."""

Diff for: tests/test_parsers/test_attributes.py

+9
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def test_pick_up_attribute_in_try_except(self):
7575
assert "IN_FINALLY" in self.attributes
7676
assert self.attributes["IN_FINALLY"]["docstring"] == "In finally."
7777

78+
def test_docstring_is_correctly_dedented(self):
79+
assert "\n " not in self.attributes["DEDENT"]["docstring"]
80+
7881

7982
class TestClassAttributes:
8083
"""Test the parser for module attributes."""
@@ -88,6 +91,9 @@ def test_pick_up_attribute_in_class(self):
8891
assert "IN_CLASS" in self.attributes
8992
assert self.attributes["IN_CLASS"]["docstring"] == "In class."
9093

94+
def test_docstring_is_correctly_dedented(self):
95+
assert "\n " not in self.attributes["DEDENT"]["docstring"]
96+
9197

9298
class TestInstanceAttributes:
9399
"""Test the parser for module attributes."""
@@ -114,6 +120,9 @@ def test_do_not_pick_up_subscript_attribute(self):
114120
assert "d.subscript" not in self.attributes
115121
assert "subscript" not in self.attributes
116122

123+
def test_docstring_is_correctly_dedented(self):
124+
assert "\n " not in self.attributes["dedent"]["docstring"]
125+
117126

118127
class TestPydanticFields:
119128
"""Test the parser for module attributes."""

0 commit comments

Comments
 (0)