Skip to content

Commit df7ed6a

Browse files
authored
Merge pull request #169 from snejus/fix-prompt-continuation-lines
Fix formatting Python code blocks with continuation lines
2 parents ce2dca8 + 0c6812f commit df7ed6a

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- Image and Unicode substitution definitions were not being formatted correctly.
3434
- Files specified in pyproject are now parsed correctly.
3535
- Fixed issue when loading cache in certain environments.
36+
- Fixed formatting of Python code blocks with continuation lines in docstrings.
3637

3738
**Removed**
3839

docstrfmt/docstrfmt.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,20 +1075,10 @@ def doctest_block(
10751075
f"Invalid doctest block: {e}",
10761076
) from None
10771077
doctest_blocks = []
1078-
current_block = ""
1079-
while parsed:
1080-
example = parsed.pop(0)
1081-
if example.want:
1082-
current_block += example.source.strip()
1083-
formatted = CodeFormatters(current_block, context).python()
1084-
doctest_blocks.append((formatted, example.want.strip()))
1085-
current_block = ""
1086-
else: # pragma: no cover
1087-
current_block += example.source.strip() + "\n"
1088-
if current_block:
1089-
# If there's any remaining code, format it as well.
1090-
formatted = CodeFormatters(current_block, context).python()
1091-
doctest_blocks.append((formatted, ""))
1078+
for example in parsed:
1079+
formatted = CodeFormatters(example.source.strip(), context).python()
1080+
doctest_blocks.append((formatted, example.want.strip()))
1081+
10921082
if not doctest_blocks:
10931083
raise InvalidRstError(
10941084
context.current_file,
@@ -1097,11 +1087,10 @@ def doctest_block(
10971087
"Empty doctest block.",
10981088
)
10991089
for formatted, want in doctest_blocks:
1100-
for line in formatted.splitlines():
1101-
if line.startswith(" "):
1102-
yield f"... {line}"
1103-
elif line:
1104-
yield f">>> {line}"
1090+
first_line, *other_lines = formatted.splitlines()
1091+
yield f">>> {first_line}"
1092+
for line in other_lines:
1093+
yield f"... {line}" if line else "..."
11051094
if want:
11061095
yield from want.splitlines()
11071096

tests/test_files/py_file.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ def method2(self, attr1, attr2):
122122
4
123123
>>> import this
124124
125+
>>> try:
126+
... value = {1: 2}[3]
127+
... except KeyError:
128+
... value = 4
129+
...
130+
... print(
131+
... {"some long long long long long long long long long key to force line wrap": value}
132+
... )
133+
{"some long long long long long long long long long key to force line wrap": value}
134+
125135
"""
126136
my_position, im_active = 1, True
127137
match my_position, im_active:

0 commit comments

Comments
 (0)