Skip to content

Commit

Permalink
Avoid removing whitespace for walrus operators within subscripts (#3823)
Browse files Browse the repository at this point in the history
Co-authored-by: hauntsaninja <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
  • Loading branch information
3 people authored Sep 8, 2023
1 parent 74d3009 commit a20338c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
(#3740)
- Fix error in AST validation when _Black_ removes trailing whitespace in a type comment
(#3773)
- Fix a bug whereby spaces were removed from walrus operators within subscript (#3823)

### Preview style

Expand Down
4 changes: 3 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def append(
# Note: at this point leaf.prefix should be empty except for
# imports, for which we only preserve newlines.
leaf.prefix += whitespace(
leaf, complex_subscript=self.is_complex_subscript(leaf)
leaf,
complex_subscript=self.is_complex_subscript(leaf),
mode=self.mode,
)
if self.inside_brackets or not preformatted or track_bracket:
self.bracket_tracker.mark(leaf)
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class Preview(Enum):
wrap_long_dict_values_in_parens = auto()
wrap_multiple_context_managers_in_parens = auto()
dummy_implementations = auto()
walrus_subscript = auto()


class Deprecated(UserWarning):
Expand Down
8 changes: 7 additions & 1 deletion src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from mypy_extensions import mypyc_attr

from black.cache import CACHE_DIR
from black.mode import Mode, Preview
from black.strings import has_triple_quotes
from blib2to3 import pygram
from blib2to3.pgen2 import token
Expand Down Expand Up @@ -171,7 +172,7 @@ def visit_default(self, node: LN) -> Iterator[T]:
yield from self.visit(child)


def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str: # noqa: C901
def whitespace(leaf: Leaf, *, complex_subscript: bool, mode: Mode) -> str: # noqa: C901
"""Return whitespace prefix if needed for the given `leaf`.
`complex_subscript` signals whether the given leaf is part of a subscription
Expand Down Expand Up @@ -345,6 +346,11 @@ def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str: # noqa: C901

return NO

elif Preview.walrus_subscript in mode and (
t == token.COLONEQUAL or prev.type == token.COLONEQUAL
):
return SPACE

elif not complex_subscript:
return NO

Expand Down
6 changes: 6 additions & 0 deletions tests/data/preview/pep_572.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
x[(a:=0):]
x[:(a:=0)]

# output
x[(a := 0):]
x[:(a := 0)]
12 changes: 12 additions & 0 deletions tests/data/preview_py_310/pep_572.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
x[a:=0]
x[a := 0]
x[a := 0, b := 1]
x[5, b := 0]
x[a:=0,b:=1]

# output
x[a := 0]
x[a := 0]
x[a := 0, b := 1]
x[5, b := 0]
x[a := 0, b := 1]
7 changes: 7 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def test_preview_context_managers_targeting_py39() -> None:
assert_format(source, expected, mode, minimum_version=(3, 9))


@pytest.mark.parametrize("filename", all_data_cases("preview_py_310"))
def test_preview_python_310(filename: str) -> None:
source, expected = read_data("preview_py_310", filename)
mode = black.Mode(target_versions={black.TargetVersion.PY310}, preview=True)
assert_format(source, expected, mode, minimum_version=(3, 10))


@pytest.mark.parametrize(
"filename", all_data_cases("preview_context_managers/auto_detect")
)
Expand Down

0 comments on commit a20338c

Please sign in to comment.