Warn on `except A | B:`, which is a runtime error
Zac-HD opened this issue · comments
Zac Hatfield-Dodds commented
t = ZeroDivisionError | ValueError # Python 3.10 syntax for types.UnionType
assert isinstance(ZeroDivisionError(), t) # isinstance and issubclass work
try:
1/0
except ZeroDivisionError | ValueError as e: # but this fails!
print(e)
Traceback (most recent call last):
File "demo.py", line 5, in <module>
1/0
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "demo.py", line 6, in <module>
except ZeroDivisionError | ValueError as e:
TypeError: catching classes that do not inherit from BaseException is not allowed
I think we should have a lint warning for this, since I've seen it bite in the wild and it will remain an error going forward.
Jelle Zijlstra commented
I think I already made this happen: test.py:3:1: B030 Except handlers should only be names of exception classes
.
Zac Hatfield-Dodds commented
🤦♂️ just needed to update to the latest version, yep. Cheers Jelle!