]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Avoid removing whitespace for walrus operators within subscripts (#3823)
authorCharlie Marsh <crmarsh416@gmail.com>
Fri, 8 Sep 2023 14:37:13 +0000 (16:37 +0200)
committerGitHub <noreply@github.com>
Fri, 8 Sep 2023 14:37:13 +0000 (07:37 -0700)
Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
CHANGES.md
src/black/lines.py
src/black/mode.py
src/black/nodes.py
tests/data/preview/pep_572.py [new file with mode: 0644]
tests/data/preview_py_310/pep_572.py [new file with mode: 0644]
tests/test_format.py

index af9fc490acf75359ebb2165e9b567e523c03c9ed..4aa3123fab63ba58b7abc0cc49881134fcc674a7 100644 (file)
@@ -80,6 +80,7 @@
   (#3740)
 - Fix error in AST validation when _Black_ removes trailing whitespace in a type comment
   (#3773)
   (#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
 
 
 ### Preview style
 
index 0a307b45efff745ab07b744f1f99068c1b81c37c..f3044ce47b89970c8c505b88ad9ba0d9d6f81909 100644 (file)
@@ -81,7 +81,9 @@ class Line:
             # Note: at this point leaf.prefix should be empty except for
             # imports, for which we only preserve newlines.
             leaf.prefix += whitespace(
             # 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)
             )
         if self.inside_brackets or not preformatted or track_bracket:
             self.bracket_tracker.mark(leaf)
index 282c1669da74f5275b818befeed14634eb186a51..06d20b7a7d668a16e5d846746c8c5da2f18a9c87 100644 (file)
@@ -183,6 +183,7 @@ class Preview(Enum):
     wrap_long_dict_values_in_parens = auto()
     wrap_multiple_context_managers_in_parens = auto()
     dummy_implementations = auto()
     wrap_long_dict_values_in_parens = auto()
     wrap_multiple_context_managers_in_parens = auto()
     dummy_implementations = auto()
+    walrus_subscript = auto()
 
 
 class Deprecated(UserWarning):
 
 
 class Deprecated(UserWarning):
index 45423b2596be6946b203151c9a93200e25eb92df..edd201a21e9f2311d3482c1183f1d100e1f073ef 100644 (file)
@@ -13,6 +13,7 @@ else:
 from mypy_extensions import mypyc_attr
 
 from black.cache import CACHE_DIR
 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
 from black.strings import has_triple_quotes
 from blib2to3 import pygram
 from blib2to3.pgen2 import token
@@ -171,7 +172,7 @@ class Visitor(Generic[T]):
                 yield from self.visit(child)
 
 
                 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
     """Return whitespace prefix if needed for the given `leaf`.
 
     `complex_subscript` signals whether the given leaf is part of a subscription
@@ -345,6 +346,11 @@ def whitespace(leaf: Leaf, *, complex_subscript: bool) -> str:  # noqa: C901
 
             return NO
 
 
             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
 
         elif not complex_subscript:
             return NO
 
diff --git a/tests/data/preview/pep_572.py b/tests/data/preview/pep_572.py
new file mode 100644 (file)
index 0000000..a50e130
--- /dev/null
@@ -0,0 +1,6 @@
+x[(a:=0):]
+x[:(a:=0)]
+
+# output
+x[(a := 0):]
+x[:(a := 0)]
diff --git a/tests/data/preview_py_310/pep_572.py b/tests/data/preview_py_310/pep_572.py
new file mode 100644 (file)
index 0000000..78d4e9e
--- /dev/null
@@ -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]
index fb4d8eb4346aaedc752743b10a21619797274b76..0650a2d6e53aa710c91f6395810acea43c9e0bac 100644 (file)
@@ -56,6 +56,13 @@ def test_preview_context_managers_targeting_py39() -> None:
     assert_format(source, expected, mode, minimum_version=(3, 9))
 
 
     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")
 )
 @pytest.mark.parametrize(
     "filename", all_data_cases("preview_context_managers/auto_detect")
 )