Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat raw strings like other docstrings #3947

Merged
merged 3 commits into from Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -21,6 +21,7 @@
multiple lines (#3899)
- Magic trailing commas are now respected in return types. (#3916)
- Require one empty line after module-level docstrings. (#3932)
- Treat raw triple-quoted strings as docstrings (#3947)

### Configuration

Expand Down
15 changes: 10 additions & 5 deletions src/black/lines.py
Expand Up @@ -193,11 +193,16 @@ def is_class_paren_empty(self) -> bool:
@property
def is_triple_quoted_string(self) -> bool:
"""Is the line a triple quoted string?"""
return (
bool(self)
and self.leaves[0].type == token.STRING
and self.leaves[0].value.startswith(('"""', "'''"))
)
if not self or self.leaves[0].type != token.STRING:
return False
value = self.leaves[0].value
if value.startswith(('"""', "'''")):
return True
if Preview.accept_raw_docstrings in self.mode and value.startswith(
("r'''", 'r"""', "R'''", 'R"""')
):
return True
return False

@property
def opens_block(self) -> bool:
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Expand Up @@ -188,6 +188,7 @@ class Preview(Enum):
dummy_implementations = auto()
walrus_subscript = auto()
module_docstring_newlines = auto()
accept_raw_docstrings = auto()
fix_power_op_line_length = auto()


Expand Down
32 changes: 32 additions & 0 deletions tests/data/raw_docstring.py
@@ -0,0 +1,32 @@
# flags: --preview --skip-string-normalization
class C:

r"""Raw"""

def f():

r"""Raw"""

class SingleQuotes:


r'''Raw'''

class UpperCaseR:
R"""Raw"""

# output
class C:
r"""Raw"""


def f():
r"""Raw"""


class SingleQuotes:
r'''Raw'''


class UpperCaseR:
R"""Raw"""