]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Fix two docstring crashes (#3451)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Mon, 16 Jan 2023 20:26:03 +0000 (12:26 -0800)
committerGitHub <noreply@github.com>
Mon, 16 Jan 2023 20:26:03 +0000 (12:26 -0800)
CHANGES.md
src/black/linegen.py
tests/data/miscellaneous/linelength6.py [new file with mode: 0644]
tests/data/simple_cases/docstring.py
tests/test_format.py

index 2da0fb4720c1bc12cc2a01c75f117f700a47aa9d..17dc0d686dfa19d93346e4d6a5b5c2d8360432a6 100644 (file)
@@ -32,6 +32,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)
 - Exclude string type annotations from improved string processing; fix crash when the
   return type annotation is stringified and spans across multiple lines (#3462)
 
index 4da75b28235c96b2d96204d532d7cd6552a0e9d4..da41886f80d616845db26eadc5eac6a481cce7e1 100644 (file)
@@ -401,6 +401,7 @@ class LineGenerator(Visitor[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:
@@ -413,6 +414,7 @@ class LineGenerator(Visitor[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 = " "
 
@@ -435,6 +437,8 @@ class LineGenerator(Visitor[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 (file)
index 0000000..4fb3427
--- /dev/null
@@ -0,0 +1,5 @@
+# Regression test for #3427, which reproes only with line length <= 6
+def f():
+    """
+    x
+    """
index f08bba575fea03bc734e1fbe6457d9c58c2bbc0c..c31d6a68783e3d904186c292c3e933986f471c8d 100644 (file)
@@ -173,6 +173,11 @@ 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 \ """
+
 
 def multiline_backslash_3():
   '''
@@ -391,6 +396,12 @@ 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 \ """
+
+
 def multiline_backslash_3():
     """
     already escaped \\"""
index 01cd61eef63dee3272e156d5e5700ad9f4152eda..0816bbd36925d313f0ff37970f57b15fc09d47b8 100644 (file)
@@ -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 line length set to 6."""
+    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