Skip to content

Commit 1cf5049

Browse files
committed
Byte strings aren't docstrings
We previously incorrectly treated byte strings in docstring position as docstrings because black does so (#8283 (comment), psf/black#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 ```
1 parent 98b3d71 commit 1cf5049

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def docstring_that_ends_with_quote_and_a_line_break3():
9393
"""
9494

9595

96+
class ByteDocstring:
97+
b""" has leading whitespace"""
98+
first_statement = 1
99+
96100
class TabbedIndent:
97101
def tabbed_indent(self):
98102
"""check for correct tabbed formatting

crates/ruff_python_formatter/src/statement/suite.rs

-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ impl<'a> DocstringStmt<'a> {
554554

555555
match value.as_ref() {
556556
Expr::StringLiteral(value) if !value.implicit_concatenated => Some(DocstringStmt(stmt)),
557-
Expr::BytesLiteral(value) if !value.implicit_concatenated => Some(DocstringStmt(stmt)),
558557
_ => None,
559558
}
560559
}

crates/ruff_python_formatter/tests/snapshots/[email protected]

+24
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ def docstring_that_ends_with_quote_and_a_line_break3():
9999
"""
100100
101101
102+
class ByteDocstring:
103+
b""" has leading whitespace"""
104+
first_statement = 1
105+
102106
class TabbedIndent:
103107
def tabbed_indent(self):
104108
"""check for correct tabbed formatting
@@ -213,6 +217,11 @@ def docstring_that_ends_with_quote_and_a_line_break3():
213217
"""he said "the news of my death have been greatly exaggerated" """
214218
215219
220+
class ByteDocstring:
221+
b""" has leading whitespace"""
222+
first_statement = 1
223+
224+
216225
class TabbedIndent:
217226
def tabbed_indent(self):
218227
"""check for correct tabbed formatting
@@ -327,6 +336,11 @@ def docstring_that_ends_with_quote_and_a_line_break3():
327336
"""he said "the news of my death have been greatly exaggerated" """
328337
329338
339+
class ByteDocstring:
340+
b""" has leading whitespace"""
341+
first_statement = 1
342+
343+
330344
class TabbedIndent:
331345
def tabbed_indent(self):
332346
"""check for correct tabbed formatting
@@ -441,6 +455,11 @@ def docstring_that_ends_with_quote_and_a_line_break3():
441455
"""he said "the news of my death have been greatly exaggerated" """
442456
443457
458+
class ByteDocstring:
459+
b""" has leading whitespace"""
460+
first_statement = 1
461+
462+
444463
class TabbedIndent:
445464
def tabbed_indent(self):
446465
"""check for correct tabbed formatting
@@ -555,6 +574,11 @@ def docstring_that_ends_with_quote_and_a_line_break3():
555574
"""he said "the news of my death have been greatly exaggerated" """
556575
557576
577+
class ByteDocstring:
578+
b""" has leading whitespace"""
579+
first_statement = 1
580+
581+
558582
class TabbedIndent:
559583
def tabbed_indent(self):
560584
"""check for correct tabbed formatting

0 commit comments

Comments
 (0)