Skip to content

Commit

Permalink
Mention style changes that are likely to be part of Black 2025
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jan 3, 2025
1 parent 790ba1c commit d784d65
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions docs/formatter/black.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ c = "\U0001f977"
d = "\N{CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I}"
```

TODO: Is this part of Black's 2025 style guide?
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).

### Module docstrings

Expand All @@ -236,9 +236,9 @@ Ruff formats module docstrings similar to class or function docstrings, whereas
"""Module docstring"""

```
TODO: Is this part of Black's 2025 style guide?
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).

### F-string formatting
### F-strings

Ruff formats expression parts in f-strings whereas Black does not:

Expand All @@ -253,7 +253,7 @@ f'test{inner + "nested_string"} including math {5 ** 3 + 10}'
f"test{inner + 'nested_string'} including math {5**3 + 10}"
```

### Implicit concatenated string formatting
### Implicit concatenated strings

Ruff merges implicitly concatenated strings if the entire string fits on a single line:

Expand Down Expand Up @@ -299,9 +299,9 @@ a = (
)
```

This makes the formatter compatible with `ISC001` ([#8272](https://github.com/astral-sh/ruff/issues/8272)).
This ensures compatibility with `ISC001` ([#8272](https://github.com/astral-sh/ruff/issues/8272)).

### `assert` formatting
### `assert` statements

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:

Expand All @@ -323,6 +323,47 @@ assert len(policy_types) >= priority + num_duplicates, (
)
```

### Parentheses around `if`-guards in `match` statements
Ruff automatically parenthesizes overlong `if` guards and it also removes parentheses if they're no longer required.

```python
# Input
match some_variable:
case "short-guard" if (
other_condition
):
pass

case "long-guard" if other_condition or some_long_call_expression(with_may, arguments) or last_condition:
pass


# Black
match some_variable:
case "short-guard" if (other_condition):
pass

case "long-guard" if other_condition or some_long_call_expression(
with_may, arguments
) or last_condition:
pass


# Ruff
match some_variable:
case "short-guard" if other_condition:
pass

case "long-guard" if (
other_condition
or some_long_call_expression(with_may, arguments)
or last_condition
):
pass
```

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).

### `global` and `nonlocal` names are broken across multiple lines by continuations

If a `global` or `nonlocal` statement includes multiple names, and exceeds the configured line
Expand Down Expand Up @@ -371,7 +412,7 @@ class IntFromGeom(GEOSFuncFactory):
errcheck = staticmethod(check_minus_one)
```

TODO: Is this part of Black's 2025 style guide?
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).

### Trailing own-line comments on imports are not moved to the next line

Expand Down Expand Up @@ -590,7 +631,7 @@ def func(

Ruff will instead insert a trailing comma in all such cases for consistency.

TODO: Is this part of Black's 2025 style guide?
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).

### Parentheses around call-chain assignment values are not preserved

Expand Down

0 comments on commit d784d65

Please sign in to comment.