Fix sus returns in strings.py (#4546)

This commit is contained in:
GiGaGon 2025-01-06 12:02:56 -08:00 committed by GitHub
parent fdabd424e2
commit e157ba4de5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 7 deletions

View File

@ -67,7 +67,7 @@
)
from black.numerics import normalize_numeric_literal
from black.strings import (
fix_docstring,
fix_multiline_docstring,
get_string_prefix,
normalize_string_prefix,
normalize_string_quotes,
@ -444,7 +444,7 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
indent = " " * 4 * self.current_line.depth
if is_multiline_string(leaf):
docstring = fix_docstring(docstring, indent)
docstring = fix_multiline_docstring(docstring, indent)
else:
docstring = docstring.strip()

View File

@ -63,10 +63,9 @@ def lines_with_leading_tabs_expanded(s: str) -> list[str]:
return lines
def fix_docstring(docstring: str, prefix: str) -> str:
def fix_multiline_docstring(docstring: str, prefix: str) -> str:
# https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation
if not docstring:
return ""
assert docstring, "INTERNAL ERROR: Multiline docstrings cannot be empty"
lines = lines_with_leading_tabs_expanded(docstring)
# Determine minimum indentation (first line doesn't count):
indent = sys.maxsize
@ -186,8 +185,7 @@ def normalize_string_quotes(s: str) -> str:
orig_quote = "'"
new_quote = '"'
first_quote_pos = s.find(orig_quote)
if first_quote_pos == -1:
return s # There's an internal error
assert first_quote_pos != -1, f"INTERNAL ERROR: Malformed string {s!r}"
prefix = s[:first_quote_pos]
unescaped_new_quote = _cached_compile(rf"(([^\\]|^)(\\\\)*){new_quote}")