Skip to content

except* formatting broken for exception unpacking with tuples #23125

@Thomas55555

Description

@Thomas55555

Summary

Describe the bug
When using a tuple of exceptions in an except block with the unpacking syntax, Ruff’s formatter produces invalid Python code. The formatting chain progressively corrupts the syntax.

Steps to reproduce

  1. Start with this code:
import orjson

JSON_DECODE_EXCEPTIONS = (orjson.JSONDecodeError,)

def fibonacci(n):
    """Compute the nth number in the Fibonacci sequence."""
    try:
        return 1
    except (*JSON_DECODE_EXCEPTIONS, ValueError):
        return []
  1. Apply Ruff formatting. It first produces:
import orjson

JSON_DECODE_EXCEPTIONS = (orjson.JSONDecodeError,)


def fibonacci(n):
    """Compute the nth number in the Fibonacci sequence."""
    try:
        return 1
    except *JSON_DECODE_EXCEPTIONS, ValueError:
        return []
  1. Applying Ruff again, it produces invalid syntax:
import orjson

JSON_DECODE_EXCEPTIONS = (orjson.JSONDecode_EXCEPTIONS,)


def fibonacci(n):
    """Compute the nth number in the Fibonacci sequence."""
    try:
        return 1
    except* JSON_DECODE_EXCEPTIONS, ValueError:
        return []

Playground links

Initial formatting: https://play.ruff.rs/671ad30c-c884-4fa6-ab21-8c8f1a5f494f
Second formatting: https://play.ruff.rs/e1fe4e28-8ecb-4af9-9792-22753c51ddcb

See: home-assistant/core#162356

Version

ruff 0.15.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingformatterRelated to the formatter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions