From 8163f2dced27bc91cd756b1af8a958d714e1deff Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 30 Oct 2023 07:37:20 -0700 Subject: [PATCH 1/2] Fix bytes strings being treated as docstrings Fixes #4002 I think this only fixes crashes so it doesn't need to go through the preview style, but not 100% sure. --- CHANGES.md | 2 +- src/black/nodes.py | 9 ++++++- tests/data/cases/bytes_docstring.py | 34 +++++++++++++++++++++++++ tests/data/{ => cases}/raw_docstring.py | 0 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/data/cases/bytes_docstring.py rename tests/data/{ => cases}/raw_docstring.py (100%) diff --git a/CHANGES.md b/CHANGES.md index 60231468bdf..4d5b30f56b9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,7 +8,7 @@ ### Stable style - +- Fix crash on formatting bytes strings that look like docstrings (#4003) ### Preview style diff --git a/src/black/nodes.py b/src/black/nodes.py index b2e96cb9edf..5f6b280c035 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -14,7 +14,7 @@ from black.cache import CACHE_DIR from black.mode import Mode, Preview -from black.strings import has_triple_quotes +from black.strings import get_string_prefix, has_triple_quotes from blib2to3 import pygram from blib2to3.pgen2 import token from blib2to3.pytree import NL, Leaf, Node, type_repr @@ -525,6 +525,13 @@ def is_arith_like(node: LN) -> bool: def is_docstring(leaf: Leaf) -> bool: + if leaf.type != token.STRING: + return False + + prefix = get_string_prefix(leaf.value) + if "b" in prefix or "B" in prefix: + return False + if prev_siblings_are( leaf.parent, [None, token.NEWLINE, token.INDENT, syms.simple_stmt] ): diff --git a/tests/data/cases/bytes_docstring.py b/tests/data/cases/bytes_docstring.py new file mode 100644 index 00000000000..2326e95293a --- /dev/null +++ b/tests/data/cases/bytes_docstring.py @@ -0,0 +1,34 @@ +def bitey(): + b" not a docstring" + +def bitey2(): + b' also not a docstring' + +def triple_quoted_bytes(): + b""" not a docstring""" + +def triple_quoted_bytes2(): + b''' also not a docstring''' + +def capitalized_bytes(): + B" NOT A DOCSTRING" + +# output +def bitey(): + b" not a docstring" + + +def bitey2(): + b" also not a docstring" + + +def triple_quoted_bytes(): + b""" not a docstring""" + + +def triple_quoted_bytes2(): + b""" also not a docstring""" + + +def capitalized_bytes(): + b" NOT A DOCSTRING" \ No newline at end of file diff --git a/tests/data/raw_docstring.py b/tests/data/cases/raw_docstring.py similarity index 100% rename from tests/data/raw_docstring.py rename to tests/data/cases/raw_docstring.py From d5fd14d4903805c4cf823468f9daee06fca1494f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:56:44 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGES.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 269a32b1bb4..f365f1c239b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,8 +9,7 @@ ### Stable style - Fix crash on formatting bytes strings that look like docstrings (#4003) -- Fix crash when whitespace followed a backslash before newline in a docstring - (#4008) +- Fix crash when whitespace followed a backslash before newline in a docstring (#4008) ### Preview style