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

Support exception aliases properly in B014 #129

Merged
merged 3 commits into from Jun 30, 2020

Conversation

ichard26
Copy link
Contributor

Resolves #110
Resolves #126

(env) richard-26@ubuntu-laptop:~/programming/flake8-bugbear$ cat test.py
try:
    with open("bugbear.py") as f:
        lines = f.readlines()
except (OSError, IOError):
    pass
(env) richard-26@ubuntu-laptop:~/programming/flake8-bugbear$ flake8 test.py
test.py:4:1: B014 Redundant exception types in `except (OSError, IOError):`.  Write `except OSError:`, which catches exactly the same exceptions.

A good example is OSError. Since Python 3.3, IOError, EnvironmentError,
select.error, etc merged into OSError and became aliases. Before this
change, both the aliases and the primary exceptions were removed from
the message.
bugbear.py Outdated Show resolved Hide resolved
Comment on lines +185 to +191
# Find and remove aliases exceptions and only leave the primary alone
primaries = filter(
lambda primary: primary in good, B014.exception_aliases.keys()
)
for primary in primaries:
aliases = B014.exception_aliases[primary]
good = list(filter(lambda e: e not in aliases, good))
Copy link
Contributor Author

@ichard26 ichard26 Jun 28, 2020

Choose a reason for hiding this comment

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

Perhaps this is a bit over-engineered. I could just define an edge-case for OSError.

Thoughts?

Copy of original review because I modified this code afterwards to be more functional with filter

Copy link
Collaborator

Choose a reason for hiding this comment

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

Na seems fine to me and makes sense.

Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

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

Looks good to me, maybe I'll give it a few days for @Zac-HD to agrees too ...

Comment on lines +185 to +191
# Find and remove aliases exceptions and only leave the primary alone
primaries = filter(
lambda primary: primary in good, B014.exception_aliases.keys()
)
for primary in primaries:
aliases = B014.exception_aliases[primary]
good = list(filter(lambda e: e not in aliases, good))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Na seems fine to me and makes sense.

Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Erroneous report message in B014 B014 recommends except ():
3 participants