Skip to content

Commit

Permalink
Support exception aliases properly in B014 (#129)
Browse files Browse the repository at this point in the history
* Add tests

* Support exception aliases properly

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.

* Functionalize
  • Loading branch information
ichard26 authored Jun 30, 2020
1 parent c18db4f commit 41c788b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
19 changes: 19 additions & 0 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,22 @@ 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 = 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))

for name, other in itertools.permutations(tuple(good), 2):
if issubclass(
getattr(builtins, name, type), getattr(builtins, other, ())
Expand Down Expand Up @@ -639,6 +648,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

0 comments on commit 41c788b

Please sign in to comment.