Skip to content

Commit

Permalink
Fix sus returns in strings.py (#4546)
Browse files Browse the repository at this point in the history
  • Loading branch information
MeGaGiGaGon authored Jan 6, 2025
1 parent fdabd42 commit e157ba4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down
8 changes: 3 additions & 5 deletions src/black/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
Expand Down

0 comments on commit e157ba4

Please sign in to comment.