From e157ba4de547acd8f0df2f2111c15e6f6e70bb7c Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Mon, 6 Jan 2025 12:02:56 -0800 Subject: [PATCH] Fix sus returns in strings.py (#4546) --- src/black/linegen.py | 4 ++-- src/black/strings.py | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 6bd2e5cf171..ca8b30665c2 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -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() diff --git a/src/black/strings.py b/src/black/strings.py index 8fa7e064074..a3018990ee8 100644 --- a/src/black/strings.py +++ b/src/black/strings.py @@ -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}")