Skip to content

Validate unary expressions when parsing - #28233

Merged
ntBre merged 1 commit into
mainfrom
brent/signed-literal-parsing
Sep 2, 2026
Merged

ntBre merged 1 commit into
mainfrom
brent/signed-literal-parsing

Conversation

@ntBre

@ntBre ntBre commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

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:

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

but Ruff does not:

$ 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 added bug An issue describing something that isn't working, or a PR that fixes a bug parser Related to the parser 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)

@ntBre
ntBre force-pushed the brent/signed-literal-parsing branch from d4f3c64 to 9cdbc1c Compare September 1, 2026 22:03
@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.

@ntBre
ntBre force-pushed the brent/signed-literal-parsing branch from 9cdbc1c to 083ddcc Compare September 1, 2026 22:33
@ntBre
ntBre marked this pull request as ready for review September 1, 2026 22:34
Base automatically changed from brent/315-match to main September 2, 2026 13:16
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 force-pushed the brent/signed-literal-parsing branch from 083ddcc to 7d77176 Compare September 2, 2026 13:16
@ntBre
ntBre merged commit 0ecff87 into main Sep 2, 2026
67 checks passed
@ntBre
ntBre deleted the brent/signed-literal-parsing branch September 2, 2026 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants