From 3208b767d78d455aa0e464864be3ab77d3630c0e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 16 Oct 2023 05:38:58 -0700 Subject: [PATCH 1/2] Treat raw strings like other docstrings Fixes #3944 --- CHANGES.md | 1 + src/black/lines.py | 15 ++++++++++----- src/black/mode.py | 1 + tests/data/raw_docstring.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/data/raw_docstring.py diff --git a/CHANGES.md b/CHANGES.md index a608551815f..1cfc63d7b6e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,6 +20,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 (#3941) ### Configuration diff --git a/src/black/lines.py b/src/black/lines.py index 14754d7532f..48fde888208 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -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: diff --git a/src/black/mode.py b/src/black/mode.py index baf886abb7f..16513ead9c9 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -188,6 +188,7 @@ class Preview(Enum): dummy_implementations = auto() walrus_subscript = auto() module_docstring_newlines = auto() + accept_raw_docstrings = auto() class Deprecated(UserWarning): diff --git a/tests/data/raw_docstring.py b/tests/data/raw_docstring.py new file mode 100644 index 00000000000..751fd3201df --- /dev/null +++ b/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""" From 1f1627772fb1b3c62b0753ad129838a1dd0df7fe Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 16 Oct 2023 05:40:57 -0700 Subject: [PATCH 2/2] fix PR number --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 1cfc63d7b6e..8da8dd44a11 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,7 +20,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 (#3941) +- Treat raw triple-quoted strings as docstrings (#3947) ### Configuration