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
+
Ruff's formatting matches Black's preview formatting, and we expect it to be part of [Black's 2025 style guide](https://github.com/psf/black/issues/4522).
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,133 @@ Ruff formats module docstrings similar to class or function docstrings, whereas
234
236
"""Module docstring"""
235
237
236
238
```
239
+
Ruff's formatting matches Black's preview formatting, and we expect it to be part of [Black's 2025 style guide](https://github.com/psf/black/issues/4522).
240
+
241
+
### F-strings
242
+
243
+
Ruff formats expression parts in f-strings whereas Black does not:
244
+
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}'
237
251
252
+
# Ruff
253
+
f"test{inner +'nested_string'} including math {5**3+10}"
254
+
```
238
255
239
-
### Walruses in slice expressions
256
+
### Implicit concatenated strings
240
257
241
-
Black avoids inserting space around `:=` operators within slices. For example, the following adheres
242
-
to Black stable style:
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
+
)
266
+
267
+
# Black
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
+
)
278
+
```
279
+
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:
284
+
285
+
```python
286
+
# Input
287
+
a = (
288
+
r"aaaaaaa"
289
+
"bbbbbbbbbbbb"
290
+
)
291
+
292
+
# Black
293
+
a =r"aaaaaaa""bbbbbbbbbbbb"
294
+
295
+
# Ruff
296
+
a = (
297
+
r"aaaaaaa"
298
+
"bbbbbbbbbbbb"
299
+
)
300
+
```
301
+
302
+
This ensures compatibility with `ISC001` ([#8272](https://github.com/astral-sh/ruff/issues/8272)).
303
+
304
+
### `assert` statements
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
+
247
314
248
315
# Black
249
-
x[y:=1]
316
+
assert (
317
+
len(policy_types) >= priority + num_duplicates
318
+
), f"This tests needs at least {priority+num_duplicates} many types."
f"This tests needs at least {priority + num_duplicates} many types."
323
+
)
250
324
```
251
325
252
-
Ruff will instead add space around the `:=` operator:
326
+
### Parentheses around `if`-guards in `match` statements
327
+
Ruff automatically parenthesizes overlong `if` guards and it also removes parentheses if they're no longer required.
253
328
254
329
```python
255
330
# Input
256
-
x[y:=1]
331
+
match some_variable:
332
+
case"short-guard"if (
333
+
other_condition
334
+
):
335
+
pass
257
336
337
+
case"long-guard"if other_condition or some_long_call_expression(with_may, arguments) or last_condition:
338
+
pass
339
+
340
+
341
+
# Black
342
+
match some_variable:
343
+
case"short-guard"if (other_condition):
344
+
pass
345
+
346
+
case"long-guard"if other_condition or some_long_call_expression(
347
+
with_may, arguments
348
+
) or last_condition:
349
+
pass
350
+
351
+
258
352
# Ruff
259
-
x[y := 1]
353
+
match some_variable:
354
+
case"short-guard"if other_condition:
355
+
pass
356
+
357
+
case"long-guard"if (
358
+
other_condition
359
+
or some_long_call_expression(with_may, arguments)
360
+
or last_condition
361
+
):
362
+
pass
260
363
```
261
364
262
-
This will likely be incorporated into Black's preview style ([#3823](https://github.com/psf/black/pull/3823)).
365
+
Ruff's formatting matches Black's preview formatting, and we expect it to be part of [Black's 2025 style guide](https://github.com/psf/black/issues/4522).
263
366
264
367
### `global` and `nonlocal` names are broken across multiple lines by continuations
265
368
@@ -309,6 +412,8 @@ class IntFromGeom(GEOSFuncFactory):
309
412
errcheck =staticmethod(check_minus_one)
310
413
```
311
414
415
+
Ruff's formatting matches Black's preview formatting, and we expect it to be part of [Black's 2025 style guide](https://github.com/psf/black/issues/4522).
416
+
312
417
### Trailing own-line comments on imports are not moved to the next line
313
418
314
419
Black enforces a single empty line between an import and a trailing own-line comment. Ruff leaves
@@ -385,9 +490,7 @@ would instead format the above as:
Ruff will instead insert a trailing comma in all such cases for consistency.
530
633
634
+
Ruff's formatting matches Black's preview formatting, and we expect it to be part of [Black's 2025 style guide](https://github.com/psf/black/issues/4522).
635
+
531
636
### Parentheses around call-chain assignment values are not preserved
### Single `with` item targeting Python 3.8 or older
760
+
761
+
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