Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
raqbit opened this issue Jul 22, 2024 · 4 comments · Fixed by #12484
Closed

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

raqbit opened this issue Jul 22, 2024 · 4 comments · Fixed by #12484
Labels
bug Something isn't working good first issue Good for newcomers help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@raqbit
Copy link

raqbit commented Jul 22, 2024

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
@charliermarsh
Copy link
Member

Thanks!

@charliermarsh charliermarsh added bug Something isn't working rule Implementing or modifying a lint rule labels Jul 22, 2024
@raqbit
Copy link
Author

raqbit commented Jul 22, 2024

Side note: likely as a side-effect of the incorrect detection, the auto-fix for this is also incorrect, as Ruff will turn the except clause into except <iterable>, whereas Python only supports tuples in except clauses (which is why the unpacking/tuple constructor is required)1.

try:
    _foo()
except exceptions:
    pass

Footnotes

  1. https://docs.python.org/3/reference/compound_stmts.html#except-clause:~:text=the%20expression%20must%20evaluate

@charliermarsh charliermarsh added good first issue Good for newcomers help wanted Contributions especially welcome labels Jul 22, 2024
@charliermarsh
Copy link
Member

Ah ok, so this is fine with exceptions = (ConnectionError, PermissionError), but not for non-tuple iterables. Got it.

@charliermarsh
Copy link
Member

I think we should just always allow (*exceptions,) though. Even if we can detect a tuple, it's a footgun to change to except exceptions: since that code will silently break if you change the tuple to a list or other collection later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Contributions especially welcome rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants