Skip to content

Allow unary plus in match patterns on Python 3.15 - #28231

Merged
ntBre merged 1 commit into
mainfrom
brent/315-match
Sep 2, 2026
Merged

ntBre merged 1 commit into
mainfrom
brent/315-match

Conversation

@ntBre

@ntBre ntBre commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

As added in python/cpython#148566, CPython now allows unary plus in match
patterns, mirroring the support for unary minus. We already had a special case for this for improved
error reporting and recovery, so I just swapped out the add_error call for
add_unsupported_syntax_error with a new error kind.

Part of #28206

Test Plan

New inline parser tests

@ntBre ntBre added parser Related to the parser python315 Related to Python 3.15 labels Sep 1, 2026
@astral-sh-bot

astral-sh-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@astral-sh-bot

astral-sh-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Summary
--

As added in python/cpython#148566, CPython now allows unary plus in match
patterns, mirroring the support for unary minus. We already had a special case for this for improved
error reporting and recovery, so I just swapped out the `add_error` call for
`add_unsupported_syntax_error` with a new error kind.

Test Plan
--

New inline parser tests
ntBre added a commit that referenced this pull request Sep 1, 2026
Summary
--

While reviewing #28231, Codex found a preexisting bug shared with our unary minus handling. Namely,
CPython emits an error for code like this, where the unary expression is part of a longer
expression:

```pycon
>>> match foo:
...     case -1**2: ...
...
  File "<python-input-0>", line 2
    case -1**2: ...
           ^^
SyntaxError: invalid syntax
```

but Ruff does not:

```console
$ ruff check --ignore ALL - <<EOF
match foo:
    case -1**2: ...
EOF
All checks passed!
```

We now verify that the parsed unary expression is a number literal and emit an error if not.

Test Plan
--

New inline snapshots
@ntBre ntBre mentioned this pull request Sep 1, 2026
27 of 32 tasks
ntBre added a commit that referenced this pull request Sep 1, 2026
Summary
--

While reviewing #28231, Codex found a preexisting bug shared with our unary minus handling. Namely,
CPython emits an error for code like this, where the unary expression is part of a longer
expression:

```pycon
>>> match foo:
...     case -1**2: ...
...
  File "<python-input-0>", line 2
    case -1**2: ...
           ^^
SyntaxError: invalid syntax
```

but Ruff does not:

```console
$ ruff check --ignore ALL - <<EOF
match foo:
    case -1**2: ...
EOF
All checks passed!
```

We now verify that the parsed unary expression is a number literal and emit an error if not.

Test Plan
--

New inline snapshots
@ntBre
ntBre marked this pull request as ready for review September 1, 2026 22:34
@ntBre
ntBre merged commit eff1c88 into main Sep 2, 2026
108 of 109 checks passed
@ntBre
ntBre deleted the brent/315-match branch September 2, 2026 13:16
ntBre added a commit that referenced this pull request Sep 2, 2026
Summary
--

While reviewing #28231, Codex found a preexisting bug shared with our unary minus handling. Namely,
CPython emits an error for code like this, where the unary expression is part of a longer
expression:

```pycon
>>> match foo:
...     case -1**2: ...
...
  File "<python-input-0>", line 2
    case -1**2: ...
           ^^
SyntaxError: invalid syntax
```

but Ruff does not:

```console
$ ruff check --ignore ALL - <<EOF
match foo:
    case -1**2: ...
EOF
All checks passed!
```

We now verify that the parsed unary expression is a number literal and emit an error if not.

Test Plan
--

New inline snapshots
ntBre added a commit that referenced this pull request Sep 2, 2026
Summary
--

While reviewing #28231, Codex found a preexisting bug shared with our
unary minus handling. Namely, CPython emits an error for code like this,
where the unary expression is part of a longer expression:

```pycon
>>> match foo:
...     case -1**2: ...
...
  File "<python-input-0>", line 2
    case -1**2: ...
           ^^
SyntaxError: invalid syntax
```

but Ruff does not:

```console
$ ruff check --ignore ALL - <<EOF
match foo:
    case -1**2: ...
EOF
All checks passed!
```

We now verify that the parsed unary expression is a number literal and
emit an error if not.

Test Plan
--

New inline snapshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser python315 Related to Python 3.15

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants