Open lensvol opened 3 years ago
Sorry for my ignorance, but is this valid Python? If I make it raise ValueError, it complains about trying to catch an exception that does not inherit from BaseException
, thus a TypeError
:
Code:
#!/usr/bin/env python3
try:
raise ValueError("Bla")
except (ValueError, "oops"):
print(123)
Error:
cooper-mbp1:~ cooper$ python3 -V
Python 3.7.5
cooper-mbp1:~ cooper$ python3 /tmp/test.py
Traceback (most recent call last):
File "/tmp/test.py", line 4, in <module>
raise ValueError("Bla")
ValueError: Bla
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test.py", line 5, in <module>
except (ValueError, "oops"):
TypeError: catching classes that do not inherit from BaseException is not allowed
Can I please see an example that shows how this is valid and actually used please. Otherwise, I don't feel we need to fix this.
Sorry, I should have been more clear in my report. What I meant is that this snippet is syntactically valid and can be parsed by Python, as shown by your own example.
While this code is semantically incorrect, a user may benefit from having it reported in a more human-friendly manner.
There is also an issue with failed assertion essentially terminating execution of flake8
process, thus preventing other plugins from doing useful work.
Would you mind if I tried to solve that and did a pull request?
If there is a clean way to know you're in an except and you find a str, to just report an error, sure.
Originally reported here: https://github.com/wemake-services/wemake-python-styleguide/issues/1599#issuecomment-703526278
There is a problem with the case of
ast.Tuple
containing anything other than class name orast.Attribute
: source code that uses such constructs in except handler actually trips assertion checks insideflake8-bugbear
visitors:Test file:
Error output: