Skip to content

Accept unparenthesized exception groups in except clauses (PEP 758) - #142

Open
karpovantonme wants to merge 1 commit into
RustPython:mainfrom
karpovantonme:pep758-unparenthesized-except
Open

Accept unparenthesized exception groups in except clauses (PEP 758)#142
karpovantonme wants to merge 1 commit into
RustPython:mainfrom
karpovantonme:pep758-unparenthesized-except

Conversation

@karpovantonme

@karpovantonme karpovantonme commented Aug 27, 2026

Copy link
Copy Markdown

What this is

PEP 758 landed in Python 3.14 and allows except A, B: and except* A, B: without parentheses, as long as there is no as clause. The parser rejects both today, so a file using the new form fails to parse rather than producing a tree.

I ran into it scanning Django projects: on one of them 6 files out of 496 failed to parse, all of them on except ValueError, OSError:.

The change

Two productions, one per clause. GenericList already collapses a single element to itself and wraps the rest in a tuple, which is the shape CPython produces here:

-    <location:@L> "except" <typ:Test<"all">?> ":" <body:Suite> => {
+    <location:@L> "except" <typ:GenericList<Test<"all">>?> ":" <body:Suite> => {

The as form is untouched. PEP 758 keeps parentheses required there, and so does the grammar after this.

No new LALR conflicts: lalrpop generates without complaint.

What I checked

except A, B      parses    (new)
except* A, B     parses    (new)
except A         parses
except A as e    parses
except (A, B)    parses
except           parses

cargo test is green, 183 tests. The new snapshot shows ExprTuple for both clauses, matching CPython.

About the diff size

python.rs is regenerated with lalrpop 0.20.0, the version that produced the file in tree, so the change is not mixed with a generator upgrade. It is still ~9.7k lines because the state table renumbers whenever the grammar changes. The hand-written part of this PR is the two productions above, a test and its snapshot.

Summary by CodeRabbit

  • New Features
    • Added support for multiple comma-separated exception types without parentheses in except and except* clauses.
    • Supports the updated Python syntax while preserving existing single-type exception handling.

PEP 758, accepted for Python 3.14, allows `except A, B:` and
`except* A, B:` without parentheses when there is no `as` clause. The
parser rejects both today, so a file using the new form fails to parse
rather than producing a tree.

`GenericList` already collapses a single element to itself and wraps the
rest in a tuple, which is the shape CPython produces here, so this is one
production per clause. The `as` form is untouched: PEP 758 keeps
parentheses required there.

python.rs is regenerated with lalrpop 0.20.0, the version that produced
the file in tree. The diff is large because the state table renumbers.
@coderabbitai

coderabbitai Bot commented Aug 27, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c697734a-0233-4598-b995-a48c370ff4be

📥 Commits

Reviewing files that changed from the base of the PR and between 1834307 and 9b20906.

⛔ Files ignored due to path filters (1)
  • parser/src/snapshots/rustpython_parser__parser__tests__try_unparenthesized_except_group.snap is excluded by !**/*.snap
📒 Files selected for processing (3)
  • parser/src/parser.rs
  • parser/src/python.lalrpop
  • parser/src/python.rs

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

The grammar now accepts multiple comma-separated exception types without parentheses in except and except* clauses. A parser snapshot test validates both forms.

Changes

Unparenthesized exception group parsing

Layer / File(s) Summary
Grammar support
parser/src/python.lalrpop
except and except* clauses now parse multiple unparenthesized exception types through GenericList<Test<"all">>.
Parser snapshot validation
parser/src/parser.rs
A snapshot test covers comma-separated exception types in regular and starred exception handlers.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 9b209

This localized parser change adds support for unparenthesized exception groups while preserving existing forms; no actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main parser change and identifies the relevant PEP 758 behavior.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. (1 skipped: 1 …
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. (1 skipped: 1 unsupported.)

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@fanninpm

Copy link
Copy Markdown
Contributor

Did you check https://github.com/astral-sh/ruff ? RustPython now uses Ruff's parser.

@youknowone

Copy link
Copy Markdown
Member

Thank you for contributing. I will merge it as long as it pass test, but it is not related to RustPython anymore as @fanninpm said

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants