Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error message for match #2649

Merged
merged 8 commits into from Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions src/black/parsing.py
Expand Up @@ -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
)
Expand Down
18 changes: 18 additions & 0 deletions tests/data/pattern_matching_invalid.py
@@ -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
9 changes: 9 additions & 0 deletions tests/test_format.py
Expand Up @@ -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")
Expand Down