Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f7cf91

Browse files
fix error message for match (psf#2649)
Fixes psf#2648. Co-authored-by: Batuhan Taskaya <[email protected]>
1 parent 5e2bb52 commit 0f7cf91

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
times, like `match re.match()` (#2661)
1616
- Fix assignment to environment variables in Jupyter Notebooks (#2642)
1717
- Add `flake8-simplify` and `flake8-comprehensions` plugins (#2653)
18+
- Fix parser error location on invalid syntax in a `match` statement (#2649)
1819

1920
## 21.11b1
2021

src/black/parsing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
7575
# Python 3.10+
7676
grammars.append(pygram.python_grammar_soft_keywords)
7777
# If we have to parse both, try to parse async as a keyword first
78-
if not supports_feature(target_versions, Feature.ASYNC_IDENTIFIERS):
79-
# Python 3.7+
78+
if not supports_feature(
79+
target_versions, Feature.ASYNC_IDENTIFIERS
80+
) and not supports_feature(target_versions, Feature.PATTERN_MATCHING):
81+
# Python 3.7-3.9
8082
grammars.append(
8183
pygram.python_grammar_no_print_statement_no_exec_statement_async_keywords
8284
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# First match, no errors
2+
match something:
3+
case bla():
4+
pass
5+
6+
# Problem on line 10
7+
match invalid_case:
8+
case valid_case:
9+
pass
10+
case a := b:
11+
pass
12+
case valid_case:
13+
pass
14+
15+
# No problems either
16+
match something:
17+
case bla():
18+
pass

tests/test_format.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ def test_python_310(filename: str) -> None:
200200
assert_format(source, expected, mode, minimum_version=(3, 10))
201201

202202

203+
def test_patma_invalid() -> None:
204+
source, expected = read_data("pattern_matching_invalid")
205+
mode = black.Mode(target_versions={black.TargetVersion.PY310})
206+
with pytest.raises(black.parsing.InvalidInput) as exc_info:
207+
assert_format(source, expected, mode, minimum_version=(3, 10))
208+
209+
exc_info.match("Cannot parse: 10:11")
210+
211+
203212
def test_docstring_no_string_normalization() -> None:
204213
"""Like test_docstring but with string normalization off."""
205214
source, expected = read_data("docstring_no_string_normalization")

0 commit comments

Comments
 (0)