Skip to content

Commit

Permalink
Enforce double quotes for all docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Dec 6, 2023
1 parent 42c6891 commit 6286396
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def g():
# hi
...

# FIXME(#8905): Uncomment, leads to unstable formatting
# def h():
# ...
# # bye
def h():
...
# bye
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ class MyClass:
# fmt: on
def method():
print ( "str" )

@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flags: --line-ranges=12-12
# flags: --line-ranges=12-12 --line-ranges=21-21
# NOTE: If you need to modify this file, pay special attention to the --line-ranges=
# flag above as it's formatting specifically these lines.

Expand All @@ -10,3 +10,12 @@ class MyClass:
# fmt: on
def method():
print("str")

@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
{
"indent_style": "tab",
"indent_width": 4
},
{
"quote_style": "single"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,8 @@ def tabbed_indent(self):
Normal indented line
- autor
"""


def single_quoted():
' content\ '
return
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ impl<'a> Format<PyFormatContext<'_>> for FormatString<'a> {
let normalized = string_part.normalize(
Quoting::CanChange,
&locator,
f.options().quote_style(),
// Per PEP 8 and PEP 257, always prefer double quotes for docstrings
QuoteStyle::Double,
parent_docstring_quote_style,
);
docstring::format(&normalized, f)
Expand Down Expand Up @@ -323,9 +324,7 @@ impl StringPart {
configured_style: QuoteStyle,
parent_docstring_quote_style: Option<QuoteStyle>,
) -> NormalizedString<'a> {
// Per PEP 8 and PEP 257, always prefer double quotes for docstrings
// and triple-quoted strings. (We assume docstrings are always
// triple-quoted.)
// Per PEP 8, always prefer double quotes for triple-quoted strings.
let preferred_style = if self.quotes.triple {
// ... unless we're formatting a code snippet inside a docstring,
// then we specifically want to invert our quote style to avoid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ def g():
# hi
...
# FIXME(#8905): Uncomment, leads to unstable formatting
# def h():
# ...
# # bye
def h():
...
# bye
```

## Black Differences
Expand All @@ -41,17 +40,6 @@ def g():
class y: ... # comment
# whitespace doesn't matter (note the next line has a trailing space and tab)
@@ -13,6 +12,7 @@
# hi
...
-def h():
- ...
- # bye
+# FIXME(#8905): Uncomment, leads to unstable formatting
+# def h():
+# ...
+# # bye
```

## Ruff Output
Expand All @@ -71,10 +59,9 @@ def g():
# hi
...
# FIXME(#8905): Uncomment, leads to unstable formatting
# def h():
# ...
# # bye
def h():
...
# bye
```

## Black Output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ class MyClass:
# fmt: on
def method():
print ( "str" )
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
```

## Black Differences

```diff
--- Black
+++ Ruff
@@ -1,12 +1,10 @@
-# flags: --line-ranges=12-12
@@ -1,15 +1,13 @@
-# flags: --line-ranges=12-12 --line-ranges=21-21
# NOTE: If you need to modify this file, pay special attention to the --line-ranges=
# flag above as it's formatting specifically these lines.
Expand All @@ -37,6 +46,15 @@ class MyClass:
def method():
- print("str")
+ print ( "str" )
@decor(
a=1,
@@ -18,4 +16,4 @@
# fmt: on
)
def func():
- pass
+ pass
```

## Ruff Output
Expand All @@ -52,12 +70,21 @@ class MyClass:
# fmt: on
def method():
print ( "str" )
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
```

## Black Output

```python
# flags: --line-ranges=12-12
# flags: --line-ranges=12-12 --line-ranges=21-21
# NOTE: If you need to modify this file, pay special attention to the --line-ranges=
# flag above as it's formatting specifically these lines.
Expand All @@ -69,6 +96,15 @@ class MyClass:
# fmt: on
def method():
print("str")
@decor(
a=1,
# fmt: off
b=(2, 3),
# fmt: on
)
def func():
pass
```


Loading

0 comments on commit 6286396

Please sign in to comment.