From f483ed42406ef6f4cabfe138bcbd1df31602c92e Mon Sep 17 00:00:00 2001 From: konsti Date: Mon, 30 Oct 2023 10:58:33 +0100 Subject: [PATCH] Byte strings aren't docstrings (#8350) We previously incorrectly treated byte strings in docstring position as docstrings because black does so (https://github.com/astral-sh/ruff/pull/8283#discussion_r1375682931, https://github.com/psf/black/issues/4002), even CPython doesn't recognize them: ```console $ python3.12 Python 3.12.0 (main, Oct 6 2023, 17:57:44) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def f(): ... b""" a""" ... >>> print(str(f.__doc__)) None ``` --- .../resources/test/fixtures/ruff/docstring.py | 4 ++++ .../src/statement/suite.rs | 1 - .../tests/snapshots/format@docstring.py.snap | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py index 60c655f686e97..17edab372513d 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py @@ -93,6 +93,10 @@ def docstring_that_ends_with_quote_and_a_line_break3(): """ +class ByteDocstring: + b""" has leading whitespace""" + first_statement = 1 + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index fa50c8ba42df9..a32184f5b7da3 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -554,7 +554,6 @@ impl<'a> DocstringStmt<'a> { match value.as_ref() { Expr::StringLiteral(value) if !value.implicit_concatenated => Some(DocstringStmt(stmt)), - Expr::BytesLiteral(value) if !value.implicit_concatenated => Some(DocstringStmt(stmt)), _ => None, } } diff --git a/crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap index 3b2867715f843..580b55562af07 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap @@ -99,6 +99,10 @@ def docstring_that_ends_with_quote_and_a_line_break3(): """ +class ByteDocstring: + b""" has leading whitespace""" + first_statement = 1 + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting @@ -213,6 +217,11 @@ def docstring_that_ends_with_quote_and_a_line_break3(): """he said "the news of my death have been greatly exaggerated" """ +class ByteDocstring: + b""" has leading whitespace""" + first_statement = 1 + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting @@ -327,6 +336,11 @@ def docstring_that_ends_with_quote_and_a_line_break3(): """he said "the news of my death have been greatly exaggerated" """ +class ByteDocstring: + b""" has leading whitespace""" + first_statement = 1 + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting @@ -441,6 +455,11 @@ def docstring_that_ends_with_quote_and_a_line_break3(): """he said "the news of my death have been greatly exaggerated" """ +class ByteDocstring: + b""" has leading whitespace""" + first_statement = 1 + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting @@ -555,6 +574,11 @@ def docstring_that_ends_with_quote_and_a_line_break3(): """he said "the news of my death have been greatly exaggerated" """ +class ByteDocstring: + b""" has leading whitespace""" + first_statement = 1 + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting