You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
d ="\N{CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I}"
216
216
```
217
217
218
+
TODO: Is this part of Black's 2025 style guide?
219
+
218
220
### Module docstrings
219
221
220
222
Ruff formats module docstrings similar to class or function docstrings, whereas Black does not.
@@ -234,32 +236,92 @@ Ruff formats module docstrings similar to class or function docstrings, whereas
234
236
"""Module docstring"""
235
237
236
238
```
239
+
TODO: Is this part of Black's 2025 style guide?
237
240
241
+
### F-string formatting
238
242
239
-
### Walruses in slice expressions
243
+
Ruff formats expression parts in f-strings whereas Black does not:
240
244
241
-
Black avoids inserting space around `:=` operators within slices. For example, the following adheres
242
-
to Black stable style:
245
+
```python
246
+
# Input
247
+
f'test{inner +"nested_string"} including math {5**3+10}'
248
+
249
+
# Black
250
+
f'test{inner +"nested_string"} including math {5**3+10}'
251
+
252
+
# Ruff
253
+
f"test{inner +'nested_string'} including math {5**3+10}"
254
+
```
255
+
256
+
### Implicit concatenated string formatting
257
+
258
+
Ruff merges implicitly concatenated strings if the entire string fits on a single line:
243
259
244
260
```python
245
261
# Input
246
-
x[y:=1]
262
+
deftest(max_history):
263
+
raise argparse.ArgumentTypeError(
264
+
f"The value of `--max-history {max_history}` "f"is not a positive integer."
265
+
)
247
266
248
267
# Black
249
-
x[y:=1]
268
+
deftest(max_history):
269
+
raise argparse.ArgumentTypeError(
270
+
f"The value of `--max-history {max_history}` "f"is not a positive integer."
271
+
)
272
+
273
+
# Ruff
274
+
deftest(max_history):
275
+
raise argparse.ArgumentTypeError(
276
+
f"The value of `--max-history {max_history}` is not a positive integer."
277
+
)
250
278
```
251
279
252
-
Ruff will instead add space around the `:=` operator:
280
+
Black's unstable style applies the same formatting.
281
+
282
+
There are few rare cases where Ruff can't merge the implicitly concatenated strings automatically.
283
+
In those cases, Ruff preserves if the implicit concatenated strings are formatted over multiple lines:
253
284
254
285
```python
255
286
# Input
256
-
x[y:=1]
287
+
a = (
288
+
r"aaaaaaa"
289
+
"bbbbbbbbbbbb"
290
+
)
291
+
292
+
# Black
293
+
a =r"aaaaaaa""bbbbbbbbbbbb"
257
294
258
295
# Ruff
259
-
x[y := 1]
296
+
a = (
297
+
r"aaaaaaa"
298
+
"bbbbbbbbbbbb"
299
+
)
260
300
```
261
301
262
-
This will likely be incorporated into Black's preview style ([#3823](https://github.com/psf/black/pull/3823)).
302
+
This makes the formatter compatible with `ISC001` ([#8272](https://github.com/astral-sh/ruff/issues/8272)).
303
+
304
+
### `assert` formatting
305
+
306
+
Unlike Black, Ruff prefers breaking the message over breaking the assertion, similar to how both Ruff and Black prefer breaking the assignment value over breaking the assignment target:
307
+
308
+
```python
309
+
# Input
310
+
assert (
311
+
len(policy_types) >= priority + num_duplicates
312
+
), f"This tests needs at least {priority+num_duplicates} many types."
313
+
314
+
315
+
# Black
316
+
assert (
317
+
len(policy_types) >= priority + num_duplicates
318
+
), f"This tests needs at least {priority+num_duplicates} many types."
### Single `with` item targeting Python 3.8 or older
719
+
720
+
Unlike Black, Ruff uses the same layout for `with` statements with a single context manager as it does for `while`, `if` and other compound statements:
0 commit comments