Skip to content

Commit

Permalink
Ignore exception classes which are not types
Browse files Browse the repository at this point in the history
Fixes #135
  • Loading branch information
ambv committed Sep 1, 2020
1 parent 459403a commit 55533c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ def _to_name_str(node):
return _to_name_str(node.value) + "." + node.attr


def _typesafe_issubclass(cls, class_or_tuple):
try:
return issubclass(cls, class_or_tuple)
except TypeError:
# User code specifies a type that is not a type in our current run. Might be
# their error, might be a difference in our environments. We don't know so we
# ignore this
return False


@attr.s
class BugBearVisitor(ast.NodeVisitor):
filename = attr.ib()
Expand Down Expand Up @@ -190,7 +200,7 @@ def visit_ExceptHandler(self, node):
good = list(filter(lambda e: e not in aliases, good))

for name, other in itertools.permutations(tuple(good), 2):
if issubclass(
if _typesafe_issubclass(
getattr(builtins, name, type), getattr(builtins, other, ())
):
if name in good:
Expand Down
8 changes: 8 additions & 0 deletions tests/b014.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ class MyError(Exception):
# socket.error and select.error are aliases of OSError. See PEP 3151 for
# more info.
pass


try:
pass
except (MyException, NotImplemented):
# NotImplemented is not an exception, let's not crash on it.
pass

0 comments on commit 55533c9

Please sign in to comment.