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

Bug in B030: False positive when adding tuples of exception classes #10426

Closed
autinerd opened this issue Mar 15, 2024 · 2 comments · Fixed by #10437
Closed

Bug in B030: False positive when adding tuples of exception classes #10426

autinerd opened this issue Mar 15, 2024 · 2 comments · Fixed by #10437
Labels
bug Something isn't working

Comments

@autinerd
Copy link
Contributor

Hello 🙂

When enabling rule B030, these cases are being flagged as violated:

try:
    ...
except (ValueError,TypeError) + (EOFError,ArithmeticError):
    ...
BASE_EXCEPTIONS = (ValueError,TypeError)

try:
    ...
except BASE_EXCEPTIONS + (EOFError,ArithmeticError):
    ...

Ruff version 0.3.2

@ottaviohartman
Copy link
Contributor

I can try to tackle this one!

One idea is to check for BinOp's on tuples and flatten -- is this the right direction?

@autinerd
Copy link
Contributor Author

Sounds good to me!

Using RUF005 to replace tuple concatenations with unpacking eliminates this case of course, because tuple unpacking is detected correctly.

@charliermarsh charliermarsh added the bug Something isn't working label Mar 17, 2024
charliermarsh pushed a commit that referenced this issue Mar 18, 2024
Fixes #10426 

## Summary

Fix rule B030 giving a false positive with Tuple operations like `+`.

[Playground](https://play.ruff.rs/17b086bc-cc43-40a7-b5bf-76d7d5fce78a)
```python
try:
    ...
except (ValueError,TypeError) + (EOFError,ArithmeticError):
    ...
```

## Reviewer notes

This is a little more convoluted than I was expecting -- because we can
have valid nested Tuples with operations done on them, the flattening
logic has become a bit more complex.

Shall I guard this behind --preview?

## Test Plan

Unit tested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants