PyCQA / flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.
MIT License
1.06k stars 104 forks source link

Warn on `except A | B:`, which is a runtime error #363

Closed Zac-HD closed 1 year ago

Zac-HD commented 1 year ago
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.

JelleZijlstra commented 1 year ago

I think I already made this happen: test.py:3:1: B030 Except handlers should only be names of exception classes.

Zac-HD commented 1 year ago

🤦‍♂️ just needed to update to the latest version, yep. Cheers Jelle!