Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/docformatter/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,35 @@ def is_newline_continuation(
"""
if (
token.type in (tokenize.NEWLINE, tokenize.NL)
and token.line.strip()
and not _is_blank_line(token.line)
and token.line.strip() in prev_token.line.strip()
):
return True

return False


def _is_blank_line(line: str) -> bool:
"""Determine if a physical line is blank.

A blank line is a non-empty line that holds nothing but whitespace, e.g.
"\\n" or " \\n". The empty string is *not* a blank line; tokenize uses
it as the line of the NEWLINE token it synthesizes for source that has no
trailing newline, as well as for DEDENT and ENDMARKER tokens.

Parameters
----------
line : str
The physical line the token was read from; tokenize.TokenInfo.line.

Returns
-------
bool
True if the line holds only whitespace and is not empty.
"""
return bool(line) and not line.strip()


def is_string_variable(
token: tokenize.TokenInfo,
prev_token: tokenize.TokenInfo,
Expand Down
9 changes: 9 additions & 0 deletions tests/_data/string_files/do_format_code.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1251,3 +1251,12 @@ pass
[issue_355]
source="def foo():\n \"\"\"Summary.\"\"\"\n x = 1\n # next line has 4 spaces of trailing whitespace\n \n return x\n"
expected="def foo():\n \"\"\"Summary.\"\"\"\n x = 1\n # next line has 4 spaces of trailing whitespace\n \n return x\n"

# Source that does not end with a newline. tokenize synthesizes a NEWLINE
# token for it whose line attribute is the empty string (CPython < 3.12); that
# token must still be treated as part of the preceding line, otherwise
# untokenize bridges the row gap with a backslash continuation and the
# formatted file no longer parses.
[issue_360_no_trailing_newline]
source="def foo():\n \"\"\"\n Hello foo.\n \"\"\"\n x = 1"
expected="def foo():\n \"\"\"Hello foo.\"\"\"\n x = 1"
1 change: 1 addition & 0 deletions tests/formatter/test_do_format_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
("do_not_break_f_string_single_quotes", NO_ARGS),
("issue_331_black_module_docstring", ["--black", ""]),
("issue_355", NO_ARGS),
("issue_360_no_trailing_newline", NO_ARGS),
],
)
def test_do_format_code(test_key, test_args, args):
Expand Down
Loading