From 5ea89389ac5772f6d1acc6e5a041e42ca7098c51 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 18 Dec 2022 16:25:36 -0800 Subject: [PATCH 1/3] Add test for crasher #3425 The crash reproes on 22.12.1 but appears to be fixed on main; add a test to make sure. --- tests/data/simple_cases/docstring.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/data/simple_cases/docstring.py b/tests/data/simple_cases/docstring.py index f08bba575fe..aafb74353ab 100644 --- a/tests/data/simple_cases/docstring.py +++ b/tests/data/simple_cases/docstring.py @@ -173,6 +173,10 @@ def multiline_backslash_2(): ''' hey there \ ''' +def multiline_backslash_really_long_dont_crash(): + """ + hey there hello guten tag hi hoow are you ola zdravstvuyte ciao como estas ca va \ """ + def multiline_backslash_3(): ''' @@ -391,6 +395,11 @@ def multiline_backslash_2(): hey there \ """ +def multiline_backslash_really_long_dont_crash(): + """ + hey there hello guten tag hi hoow are you ola zdravstvuyte ciao como estas ca va \ """ + + def multiline_backslash_3(): """ already escaped \\""" From b5d15be22f0cc30eceb808a8e7a10dd2db0330dd Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 18 Dec 2022 18:24:39 -0800 Subject: [PATCH 2/3] fix two crashers --- CHANGES.md | 1 + src/black/linegen.py | 4 ++++ tests/data/miscellaneous/linelength6.py | 5 +++++ tests/data/simple_cases/docstring.py | 2 ++ tests/test_format.py | 7 +++++++ 5 files changed, 19 insertions(+) create mode 100644 tests/data/miscellaneous/linelength6.py diff --git a/CHANGES.md b/CHANGES.md index f786f1a1fed..5e8eb37fd6f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -24,6 +24,7 @@ - Long values in dict literals are now wrapped in parentheses; correspondingly unnecessary parentheses around short values in dict literals are now removed; long string lambda values are now wrapped in parentheses (#3440) +- Fix two crashes in preview style involving edge cases with docstrings (#3451) ### Configuration diff --git a/src/black/linegen.py b/src/black/linegen.py index 2e75bc94506..8d3ed11469d 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -385,6 +385,7 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]: else: docstring = docstring.strip() + has_trailing_backslash = False if docstring: # Add some padding if the docstring starts / ends with a quote mark. if docstring[0] == quote_char: @@ -397,6 +398,7 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]: # Odd number of tailing backslashes, add some padding to # avoid escaping the closing string quote. docstring += " " + has_trailing_backslash = True elif not docstring_started_empty: docstring = " " @@ -419,6 +421,8 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]: if ( len(lines) > 1 and last_line_length + quote_len > self.mode.line_length + and len(indent) + quote_len <= self.mode.line_length + and not has_trailing_backslash ): leaf.value = prefix + quote + docstring + "\n" + indent + quote else: diff --git a/tests/data/miscellaneous/linelength6.py b/tests/data/miscellaneous/linelength6.py new file mode 100644 index 00000000000..4fb342726f5 --- /dev/null +++ b/tests/data/miscellaneous/linelength6.py @@ -0,0 +1,5 @@ +# Regression test for #3427, which reproes only with line length <= 6 +def f(): + """ + x + """ diff --git a/tests/data/simple_cases/docstring.py b/tests/data/simple_cases/docstring.py index aafb74353ab..c31d6a68783 100644 --- a/tests/data/simple_cases/docstring.py +++ b/tests/data/simple_cases/docstring.py @@ -173,6 +173,7 @@ def multiline_backslash_2(): ''' hey there \ ''' +# Regression test for #3425 def multiline_backslash_really_long_dont_crash(): """ hey there hello guten tag hi hoow are you ola zdravstvuyte ciao como estas ca va \ """ @@ -395,6 +396,7 @@ def multiline_backslash_2(): hey there \ """ +# Regression test for #3425 def multiline_backslash_really_long_dont_crash(): """ hey there hello guten tag hi hoow are you ola zdravstvuyte ciao como estas ca va \ """ diff --git a/tests/test_format.py b/tests/test_format.py index 01cd61eef63..4e2a6b2b59b 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -146,6 +146,13 @@ def test_docstring_no_string_normalization() -> None: assert_format(source, expected, mode) +def test_docstring_line_length_6() -> None: + """Like test_docstring but with string normalization off.""" + source, expected = read_data("miscellaneous", "linelength6") + mode = black.Mode(line_length=6) + assert_format(source, expected, mode) + + def test_preview_docstring_no_string_normalization() -> None: """ Like test_docstring but with string normalization off *and* the preview style From c765d8606e075c3e0c286900c6eeba20c1a4bc49 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 19 Dec 2022 18:07:09 -0800 Subject: [PATCH 3/3] Update tests/test_format.py --- tests/test_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_format.py b/tests/test_format.py index 4e2a6b2b59b..0816bbd3692 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -147,7 +147,7 @@ def test_docstring_no_string_normalization() -> None: def test_docstring_line_length_6() -> None: - """Like test_docstring but with string normalization off.""" + """Like test_docstring but with line length set to 6.""" source, expected = read_data("miscellaneous", "linelength6") mode = black.Mode(line_length=6) assert_format(source, expected, mode)