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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,24 @@ def visit_ExceptHandler(self, node):
# (MyError, MyError) # duplicate names
# (MyError, BaseException) # everything derives from the Base
# (Exception, TypeError) # builtins where one subclasses another
# (IOError, OSError) # IOError is an alias of OSError since Python3.3
# but note that other cases are impractical to hande from the AST.
# We expect this is mostly useful for users who do not have the
# builtin exception hierarchy memorised, and include a 'shadowed'
# subtype without realising that it's redundant.
good = sorted(set(names), key=names.index)
if "BaseException" in good:
good = ["BaseException"]
# Find and remove aliases exceptions and only leave the primary alone
primaries = [
primary
for primary in B014.exception_aliases.keys()
if primary in good
]
for primary in primaries:
aliases = B014.exception_aliases[primary]
good = [e for e in good if e not in aliases]
ichard26 marked this conversation as resolved.
Show resolved Hide resolved

for name, other in itertools.permutations(tuple(good), 2):
if issubclass(
getattr(builtins, name, type), getattr(builtins, other, ())
Expand Down Expand Up @@ -639,6 +650,16 @@ def visit(self, node):
"Write `except {2}{1}:`, which catches exactly the same exceptions."
)
)
B014.exception_aliases = {
"OSError": {
"IOError",
"EnvironmentError",
"WindowsError",
"mmap.error",
"socket.error",
"select.error",
}
}

# Those could be false positives but it's more dangerous to let them slip
# through if they're not.
Expand Down
13 changes: 12 additions & 1 deletion tests/b014.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Should emit:
B014 - on lines 10, 16, 27, 41, and 48
B014 - on lines 10, 16, 27, 41, 48, and 55
"""

import re
Expand Down Expand Up @@ -48,3 +48,14 @@ class MyError(Exception):
except (re.error, re.error):
# Duplicate exception types as attributes
pass


try:
pass
except (IOError, EnvironmentError, OSError):
# Detect if a primary exception and any its aliases are present.
#
# Since Python 3.3, IOError, EnvironmentError, WindowsError, mmap.error,
# socket.error and select.error are aliases of OSError. See PEP 3151 for
# more info.
pass
1 change: 1 addition & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def test_b014(self):
B014(27, 0, vars=("MyError, MyError", "", "MyError")),
B014(41, 0, vars=("MyError, BaseException", " as e", "BaseException")),
B014(48, 0, vars=("re.error, re.error", "", "re.error")),
B014(55, 0, vars=("IOError, EnvironmentError, OSError", "", "OSError"),),
)
self.assertEqual(errors, expected)

Expand Down