Skip to content

B013 false positive when using iterable unpacking in except clause #12450

@raqbit

Description

@raqbit

When unpacking an iterable into a tuple as part of an except clause, B013 is triggered even though the unpacking can cause the tuple to contain multiple exceptions.

Example:

exceptions = [ConnectionError, PermissionError]

try:
    _foo()
except (*exceptions,):  # B013: A length-one tuple literal is redundant in exception handlers
    pass

Repro on ruff playground: https://play.ruff.rs/6b9daf6a-96fb-48ab-9be6-db236f86ec21
Ruff version: 0.5.4


As a workaround for my specific use case, the tuple constructor accepts an iterable, making the following possible:

try:
    _foo()
except tuple(exceptions):
    pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomershelp wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions