]> 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:

fix error message for match (#2649)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Wed, 1 Dec 2021 02:39:39 +0000 (18:39 -0800)
committerGitHub <noreply@github.com>
Wed, 1 Dec 2021 02:39:39 +0000 (18:39 -0800)
Fixes #2648.

Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
CHANGES.md
src/black/parsing.py
tests/data/pattern_matching_invalid.py [new file with mode: 0644]
tests/test_format.py

index 7214405c4290f0d28e6f6ef923b99c4478ef3622..c0cf60af98aefdafe7eed9cd9eff15ee0a7996f9 100644 (file)
@@ -15,6 +15,7 @@
   times, like `match re.match()` (#2661)
 - Fix assignment to environment variables in Jupyter Notebooks (#2642)
 - Add `flake8-simplify` and `flake8-comprehensions` plugins (#2653)
+- Fix parser error location on invalid syntax in a `match` statement (#2649)
 
 ## 21.11b1
 
index 32cfa5239f117e00c2ce969baab6ee3118a5b519..e38405637cd88c745cfd965ef7698f68ad471ce7 100644 (file)
@@ -75,8 +75,10 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
         # Python 3.10+
         grammars.append(pygram.python_grammar_soft_keywords)
     # If we have to parse both, try to parse async as a keyword first
-    if not supports_feature(target_versions, Feature.ASYNC_IDENTIFIERS):
-        # Python 3.7+
+    if not supports_feature(
+        target_versions, Feature.ASYNC_IDENTIFIERS
+    ) and not supports_feature(target_versions, Feature.PATTERN_MATCHING):
+        # Python 3.7-3.9
         grammars.append(
             pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords
         )
diff --git a/tests/data/pattern_matching_invalid.py b/tests/data/pattern_matching_invalid.py
new file mode 100644 (file)
index 0000000..22b5b94
--- /dev/null
@@ -0,0 +1,18 @@
+# First match, no errors
+match something:
+    case bla():
+        pass
+
+# Problem on line 10
+match invalid_case:
+    case valid_case:
+        pass
+    case a := b:
+        pass
+    case valid_case:
+        pass
+
+# No problems either
+match something:
+    case bla():
+        pass
index 8f8ffb3610e6951a426049a31beb20bc44e07015..f97d7165b1af353278200aa6610e4782a5df35be 100644 (file)
@@ -200,6 +200,15 @@ def test_python_310(filename: str) -> None:
     assert_format(source, expected, mode, minimum_version=(3, 10))
 
 
+def test_patma_invalid() -> None:
+    source, expected = read_data("pattern_matching_invalid")
+    mode = black.Mode(target_versions={black.TargetVersion.PY310})
+    with pytest.raises(black.parsing.InvalidInput) as exc_info:
+        assert_format(source, expected, mode, minimum_version=(3, 10))
+
+    exc_info.match("Cannot parse: 10:11")
+
+
 def test_docstring_no_string_normalization() -> None:
     """Like test_docstring but with string normalization off."""
     source, expected = read_data("docstring_no_string_normalization")