Skip to content

Comments

Fix exception handler parenthesis removal for Python 3.14+#23126

Merged
ntBre merged 3 commits intomainfrom
brent/fix-tuple-formatting
Feb 6, 2026
Merged

Fix exception handler parenthesis removal for Python 3.14+#23126
ntBre merged 3 commits intomainfrom
brent/fix-tuple-formatting

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Feb 6, 2026

Summary

Fixes #23125. We were missing handling for starred elements within a tuple of
exceptions. This produces valid syntax but changes the AST from a
non-starred except handler to a starred except handler (which can be invalid
if there are other non-starred handlers):

 try: ...
-except (*exceptions, ValueError): ...
+except *exceptions, ValueError: ...

Starred expressions in other positions are outright invalid:

>>> try: ...
... except Exception, *exceptions: ...
...
  File "<python-input-8>", line 2
    except Exception, *exceptions: ...
                      ^
SyntaxError: invalid syntax

We now preserve parentheses in these cases as well.

Test Plan

New tests derived from the issue

ntBre added 3 commits February 6, 2026 14:56
Summary
--

Fixes #23125. We were missing handling for starred elements within a tuple of
exceptions. This produces valid syntax but changes the AST from a
non-starred `except` handler to a starred `except` handler (which can be invalid
if there are other non-starred handlers):

```py
try: ...
except (*exceptions, ValueError): ...

try: ...
except *exceptions, ValueError: ...
```

Starred expressions in other positions are outright invalid:

```pycon
>>> try: ...
... except Exception, *exceptions: ...
...
  File "<python-input-8>", line 2
    except Exception, *exceptions: ...
                      ^
SyntaxError: invalid syntax
```

We now preserve parentheses in these cases as well.

Test Plan
--

New tests derived from the issue
@ntBre ntBre requested a review from MichaReiser as a code owner February 6, 2026 19:58
@ntBre ntBre added bug Something isn't working formatter Related to the formatter labels Feb 6, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 6, 2026

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre requested a review from dylwil3 February 6, 2026 20:37
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for fixing this!

@ntBre ntBre merged commit f14fd5d into main Feb 6, 2026
44 checks passed
@ntBre ntBre deleted the brent/fix-tuple-formatting branch February 6, 2026 21:48
ichoosetoaccept added a commit to detailobsessed/surfmon that referenced this pull request Feb 13, 2026
## Summary
Upgrade ruff from 0.15.0 to 0.15.1 and fix the except clause formatting bug that ruff 0.15.1 addresses.

## Problem
Ruff 0.15.0 had a bug where it incorrectly removed parentheses from multi-exception `except` clauses on Python 3.14+ (PEP 758). Ruff 0.15.1 fixes this (astral-sh/ruff#23126).

## Solution
- Bump ruff to 0.15.1 in pyproject.toml and uv.lock
- Update prek.toml hook rev to match

## Changes
- `pyproject.toml` — ruff version bump
- `prek.toml` — ruff hook rev update
- `uv.lock` — lockfile refresh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working formatter Related to the formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

except* formatting broken for exception unpacking with tuples

3 participants