mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-03 00:57:46 +00:00

**Summary** Implement docstring formatting **Test Plan** Matches black's `docstring.py` fixture exactly, added some new cases for what is hard to debug with black and with what black doesn't cover. similarity index: main: zulip: 0.99702 django: 0.99784 warehouse: 0.99585 build: 0.75623 transformers: 0.99469 cpython: 0.75989 typeshed: 0.74853 this branch: zulip: 0.99702 django: 0.99784 warehouse: 0.99585 build: 0.75623 transformers: 0.99464 cpython: 0.75517 typeshed: 0.74853 The regression in transformers is actually an improvement in a file they don't format with black (they run `black examples tests src utils setup.py conftest.py`, the difference is in hubconf.py). cpython doesn't use black. Closes #6196
448 lines
8.4 KiB
Text
448 lines
8.4 KiB
Text
---
|
|
source: crates/ruff_python_formatter/tests/fixtures.rs
|
|
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py
|
|
---
|
|
## Input
|
|
```py
|
|
def single_line_backslashes1():
|
|
""" content\ """
|
|
return
|
|
|
|
|
|
def single_line_backslashes2():
|
|
""" content\\ """
|
|
return
|
|
|
|
|
|
def single_line_backslashes3():
|
|
""" content\\\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes1():
|
|
"""This is a docstring with
|
|
some lines of text\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes2():
|
|
"""This is a docstring with
|
|
some lines of text\\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes3():
|
|
"""This is a docstring with
|
|
some lines of text\\\ """
|
|
return
|
|
|
|
|
|
def multiple_negatively_indented_docstring_lines():
|
|
"""a
|
|
b
|
|
c
|
|
d
|
|
e
|
|
"""
|
|
|
|
|
|
def overindentend_docstring():
|
|
"""a
|
|
over-indented
|
|
"""
|
|
|
|
|
|
def comment_before_docstring():
|
|
# don't lose this function comment ...
|
|
"""Does nothing.
|
|
|
|
But it has comments
|
|
""" # ... neither lose this function comment
|
|
|
|
|
|
class CommentBeforeDocstring():
|
|
# don't lose this class comment ...
|
|
"""Empty class.
|
|
|
|
But it has comments
|
|
""" # ... neither lose this class comment
|
|
|
|
|
|
class IndentMeSome:
|
|
def doc_string_without_linebreak_after_colon(self): """ This is somewhat strange
|
|
a
|
|
b
|
|
We format this a is the docstring had started properly indented on the next
|
|
line if the target indentation. This may we incorrect since source and target
|
|
indentation can be incorrect, but this is also an edge case.
|
|
"""
|
|
|
|
|
|
class IgnoreImplicitlyConcatenatedStrings:
|
|
"""""" ""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break1():
|
|
"""
|
|
he said "the news of my death have been greatly exaggerated"
|
|
"""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break2():
|
|
"""he said "the news of my death have been greatly exaggerated"
|
|
"""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break3():
|
|
"""he said "the news of my death have been greatly exaggerated"
|
|
|
|
"""
|
|
|
|
|
|
class TabbedIndent:
|
|
def tabbed_indent(self):
|
|
"""check for correct tabbed formatting
|
|
^^^^^^^^^^
|
|
Normal indented line
|
|
- autor
|
|
"""
|
|
```
|
|
|
|
## Outputs
|
|
### Output 1
|
|
```
|
|
indent-style = Spaces, size: 4
|
|
line-width = 88
|
|
quote-style = Double
|
|
magic-trailing-comma = Respect
|
|
```
|
|
|
|
```py
|
|
def single_line_backslashes1():
|
|
"""content\ """
|
|
return
|
|
|
|
|
|
def single_line_backslashes2():
|
|
"""content\\"""
|
|
return
|
|
|
|
|
|
def single_line_backslashes3():
|
|
"""content\\\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes1():
|
|
"""This is a docstring with
|
|
some lines of text\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes2():
|
|
"""This is a docstring with
|
|
some lines of text\\"""
|
|
return
|
|
|
|
|
|
def multiline_backslashes3():
|
|
"""This is a docstring with
|
|
some lines of text\\\ """
|
|
return
|
|
|
|
|
|
def multiple_negatively_indented_docstring_lines():
|
|
"""a
|
|
b
|
|
c
|
|
d
|
|
e
|
|
"""
|
|
|
|
|
|
def overindentend_docstring():
|
|
"""a
|
|
over-indented
|
|
"""
|
|
|
|
|
|
def comment_before_docstring():
|
|
# don't lose this function comment ...
|
|
"""Does nothing.
|
|
|
|
But it has comments
|
|
""" # ... neither lose this function comment
|
|
|
|
|
|
class CommentBeforeDocstring:
|
|
# don't lose this class comment ...
|
|
"""Empty class.
|
|
|
|
But it has comments
|
|
""" # ... neither lose this class comment
|
|
|
|
|
|
class IndentMeSome:
|
|
def doc_string_without_linebreak_after_colon(self):
|
|
"""This is somewhat strange
|
|
a
|
|
b
|
|
We format this a is the docstring had started properly indented on the next
|
|
line if the target indentation. This may we incorrect since source and target
|
|
indentation can be incorrect, but this is also an edge case.
|
|
"""
|
|
|
|
|
|
class IgnoreImplicitlyConcatenatedStrings:
|
|
"""""" ""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break1():
|
|
"""
|
|
he said "the news of my death have been greatly exaggerated"
|
|
"""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break2():
|
|
"""he said "the news of my death have been greatly exaggerated" """
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break3():
|
|
"""he said "the news of my death have been greatly exaggerated" """
|
|
|
|
|
|
class TabbedIndent:
|
|
def tabbed_indent(self):
|
|
"""check for correct tabbed formatting
|
|
^^^^^^^^^^
|
|
Normal indented line
|
|
- autor
|
|
"""
|
|
```
|
|
|
|
|
|
### Output 2
|
|
```
|
|
indent-style = Spaces, size: 2
|
|
line-width = 88
|
|
quote-style = Double
|
|
magic-trailing-comma = Respect
|
|
```
|
|
|
|
```py
|
|
def single_line_backslashes1():
|
|
"""content\ """
|
|
return
|
|
|
|
|
|
def single_line_backslashes2():
|
|
"""content\\"""
|
|
return
|
|
|
|
|
|
def single_line_backslashes3():
|
|
"""content\\\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes1():
|
|
"""This is a docstring with
|
|
some lines of text\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes2():
|
|
"""This is a docstring with
|
|
some lines of text\\"""
|
|
return
|
|
|
|
|
|
def multiline_backslashes3():
|
|
"""This is a docstring with
|
|
some lines of text\\\ """
|
|
return
|
|
|
|
|
|
def multiple_negatively_indented_docstring_lines():
|
|
"""a
|
|
b
|
|
c
|
|
d
|
|
e
|
|
"""
|
|
|
|
|
|
def overindentend_docstring():
|
|
"""a
|
|
over-indented
|
|
"""
|
|
|
|
|
|
def comment_before_docstring():
|
|
# don't lose this function comment ...
|
|
"""Does nothing.
|
|
|
|
But it has comments
|
|
""" # ... neither lose this function comment
|
|
|
|
|
|
class CommentBeforeDocstring:
|
|
# don't lose this class comment ...
|
|
"""Empty class.
|
|
|
|
But it has comments
|
|
""" # ... neither lose this class comment
|
|
|
|
|
|
class IndentMeSome:
|
|
def doc_string_without_linebreak_after_colon(self):
|
|
"""This is somewhat strange
|
|
a
|
|
b
|
|
We format this a is the docstring had started properly indented on the next
|
|
line if the target indentation. This may we incorrect since source and target
|
|
indentation can be incorrect, but this is also an edge case.
|
|
"""
|
|
|
|
|
|
class IgnoreImplicitlyConcatenatedStrings:
|
|
"""""" ""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break1():
|
|
"""
|
|
he said "the news of my death have been greatly exaggerated"
|
|
"""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break2():
|
|
"""he said "the news of my death have been greatly exaggerated" """
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break3():
|
|
"""he said "the news of my death have been greatly exaggerated" """
|
|
|
|
|
|
class TabbedIndent:
|
|
def tabbed_indent(self):
|
|
"""check for correct tabbed formatting
|
|
^^^^^^^^^^
|
|
Normal indented line
|
|
- autor
|
|
"""
|
|
```
|
|
|
|
|
|
### Output 3
|
|
```
|
|
indent-style = Tab
|
|
line-width = 88
|
|
quote-style = Double
|
|
magic-trailing-comma = Respect
|
|
```
|
|
|
|
```py
|
|
def single_line_backslashes1():
|
|
"""content\ """
|
|
return
|
|
|
|
|
|
def single_line_backslashes2():
|
|
"""content\\"""
|
|
return
|
|
|
|
|
|
def single_line_backslashes3():
|
|
"""content\\\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes1():
|
|
"""This is a docstring with
|
|
some lines of text\ """
|
|
return
|
|
|
|
|
|
def multiline_backslashes2():
|
|
"""This is a docstring with
|
|
some lines of text\\"""
|
|
return
|
|
|
|
|
|
def multiline_backslashes3():
|
|
"""This is a docstring with
|
|
some lines of text\\\ """
|
|
return
|
|
|
|
|
|
def multiple_negatively_indented_docstring_lines():
|
|
"""a
|
|
b
|
|
c
|
|
d
|
|
e
|
|
"""
|
|
|
|
|
|
def overindentend_docstring():
|
|
"""a
|
|
over-indented
|
|
"""
|
|
|
|
|
|
def comment_before_docstring():
|
|
# don't lose this function comment ...
|
|
"""Does nothing.
|
|
|
|
But it has comments
|
|
""" # ... neither lose this function comment
|
|
|
|
|
|
class CommentBeforeDocstring:
|
|
# don't lose this class comment ...
|
|
"""Empty class.
|
|
|
|
But it has comments
|
|
""" # ... neither lose this class comment
|
|
|
|
|
|
class IndentMeSome:
|
|
def doc_string_without_linebreak_after_colon(self):
|
|
"""This is somewhat strange
|
|
a
|
|
b
|
|
We format this a is the docstring had started properly indented on the next
|
|
line if the target indentation. This may we incorrect since source and target
|
|
indentation can be incorrect, but this is also an edge case.
|
|
"""
|
|
|
|
|
|
class IgnoreImplicitlyConcatenatedStrings:
|
|
"""""" ""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break1():
|
|
"""
|
|
he said "the news of my death have been greatly exaggerated"
|
|
"""
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break2():
|
|
"""he said "the news of my death have been greatly exaggerated" """
|
|
|
|
|
|
def docstring_that_ends_with_quote_and_a_line_break3():
|
|
"""he said "the news of my death have been greatly exaggerated" """
|
|
|
|
|
|
class TabbedIndent:
|
|
def tabbed_indent(self):
|
|
"""check for correct tabbed formatting
|
|
^^^^^^^^^^
|
|
Normal indented line
|
|
- autor
|
|
"""
|
|
```
|
|
|
|
|
|
|