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

[flake8-bugbear] Allow tuples of exceptions (B030) #10437

Merged
merged 11 commits into from Mar 18, 2024

Conversation

ottaviohartman
Copy link
Contributor

Fixes #10426

Summary

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

Playground

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.

Copy link

github-actions bot commented Mar 17, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@autinerd
Copy link
Contributor

Oh, we seem to have worked on it at the same time, I just wanted to open a PR fixing this as well 😅

I checked out your PR and checked it against my tests, and it doesn't seem to be complete with the nesting.

try:
    ...
except (ValueError, *(RuntimeError, TypeError), *((ArithmeticError,) + (EOFError,))):
                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B030
    ...

This is flagged as error, although this is valid.

@ottaviohartman
Copy link
Contributor Author

Ok I can close this in favor of yours.

@ottaviohartman
Copy link
Contributor Author

@autinerd actually let me know if you think I should fix that bug and continue, or just close this! Sorry about the confusion - I meant to say that I'd work on this issue.

@autinerd
Copy link
Contributor

I think that would be a good idea if you work on it, as my knowledge in the Ruff codebase (and Rust in general) is not really good. My solution was to handle it recursively, maybe it is possible to do it iteratively as well.

Comment on lines +118 to +121
try:
pass
except (ValueError, *(RuntimeError, TypeError), *((ArithmeticError,) + (EOFError,))):
pass
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@autinerd added this test case

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

This looks good! Thanks.

Expr::BinOp(ast::ExprBinOp {
left,
right,
op: Operator::Add,
Copy link
Member

Choose a reason for hiding this comment

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

Gated these to Operator::Add.

@charliermarsh charliermarsh added the bug Something isn't working label Mar 18, 2024
@charliermarsh charliermarsh changed the title fix(B030): Allow adding tuples of exception classes [flake8-bugbear] Allow tuples of exceptions (B030) Mar 18, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) March 18, 2024 00:28
@charliermarsh
Copy link
Member

No need for preview here since it's a bug fix.

@charliermarsh charliermarsh merged commit 6123a5b into astral-sh:main Mar 18, 2024
17 checks passed
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 this pull request may close these issues.

Bug in B030: False positive when adding tuples of exception classes
3 participants