Fix two docstring crashes (#3451)
This commit is contained in:
parent
d4ff985853
commit
60a2e8e2c2
@ -32,6 +32,7 @@
|
|||||||
- Long values in dict literals are now wrapped in parentheses; correspondingly
|
- Long values in dict literals are now wrapped in parentheses; correspondingly
|
||||||
unnecessary parentheses around short values in dict literals are now removed; long
|
unnecessary parentheses around short values in dict literals are now removed; long
|
||||||
string lambda values are now wrapped in parentheses (#3440)
|
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
|
- Exclude string type annotations from improved string processing; fix crash when the
|
||||||
return type annotation is stringified and spans across multiple lines (#3462)
|
return type annotation is stringified and spans across multiple lines (#3462)
|
||||||
|
|
||||||
|
@ -401,6 +401,7 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
|
|||||||
else:
|
else:
|
||||||
docstring = docstring.strip()
|
docstring = docstring.strip()
|
||||||
|
|
||||||
|
has_trailing_backslash = False
|
||||||
if docstring:
|
if docstring:
|
||||||
# Add some padding if the docstring starts / ends with a quote mark.
|
# Add some padding if the docstring starts / ends with a quote mark.
|
||||||
if docstring[0] == quote_char:
|
if docstring[0] == quote_char:
|
||||||
@ -413,6 +414,7 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
|
|||||||
# Odd number of tailing backslashes, add some padding to
|
# Odd number of tailing backslashes, add some padding to
|
||||||
# avoid escaping the closing string quote.
|
# avoid escaping the closing string quote.
|
||||||
docstring += " "
|
docstring += " "
|
||||||
|
has_trailing_backslash = True
|
||||||
elif not docstring_started_empty:
|
elif not docstring_started_empty:
|
||||||
docstring = " "
|
docstring = " "
|
||||||
|
|
||||||
@ -435,6 +437,8 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
|
|||||||
if (
|
if (
|
||||||
len(lines) > 1
|
len(lines) > 1
|
||||||
and last_line_length + quote_len > self.mode.line_length
|
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
|
leaf.value = prefix + quote + docstring + "\n" + indent + quote
|
||||||
else:
|
else:
|
||||||
|
5
tests/data/miscellaneous/linelength6.py
Normal file
5
tests/data/miscellaneous/linelength6.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Regression test for #3427, which reproes only with line length <= 6
|
||||||
|
def f():
|
||||||
|
"""
|
||||||
|
x
|
||||||
|
"""
|
@ -173,6 +173,11 @@ def multiline_backslash_2():
|
|||||||
'''
|
'''
|
||||||
hey there \ '''
|
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():
|
def multiline_backslash_3():
|
||||||
'''
|
'''
|
||||||
@ -391,6 +396,12 @@ def multiline_backslash_2():
|
|||||||
hey there \ """
|
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():
|
def multiline_backslash_3():
|
||||||
"""
|
"""
|
||||||
already escaped \\"""
|
already escaped \\"""
|
||||||
|
@ -146,6 +146,13 @@ def test_docstring_no_string_normalization() -> None:
|
|||||||
assert_format(source, expected, mode)
|
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:
|
def test_preview_docstring_no_string_normalization() -> None:
|
||||||
"""
|
"""
|
||||||
Like test_docstring but with string normalization off *and* the preview style
|
Like test_docstring but with string normalization off *and* the preview style
|
||||||
|
Loading…
Reference in New Issue
Block a user