flake8-bugbear
flake8-bugbear copied to clipboard
B014 fails on `except (*exceptions, TypeError):`
Hi, and thank you for such a great plugin for flake8
.
With release 20.1.1 B014 rule was introduced and flake8 fails with traceback on one case I found.
Example code:
exceptions = (Exception, ValueError)
try:
42
except (*exceptions, TypeError):
pass
My .flake8
configuration:
[flake8]
ignore = E203, E266, E501, W503
max-line-length = 88
max-complexity = 18
select = B,C,E,F,W,T4,B9
exclude =
venv/
.venv/
And error:
➜ flake8
Traceback (most recent call last):
File "/Users/ng/temp/flake8-bugbear-B014/venv/bin/flake8", line 10, in <module>
sys.exit(main())
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/flake8/main/cli.py", line 18, in main
app.run(argv)
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/flake8/main/application.py", line 393, in run
self._run(argv)
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/flake8/main/application.py", line 381, in _run
self.run_checks()
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/flake8/main/application.py", line 300, in run_checks
self.file_checker_manager.run()
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/flake8/checker.py", line 331, in run
self.run_serial()
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/flake8/checker.py", line 315, in run_serial
checker.run_checks()
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/flake8/checker.py", line 598, in run_checks
self.run_ast_checks()
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/flake8/checker.py", line 502, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/bugbear.py", line 34, in run
visitor.visit(self.tree)
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/bugbear.py", line 144, in visit
super().visit(node)
File "/Users/ng/.asdf/installs/python/3.8.0/lib/python3.8/ast.py", line 360, in visit
return visitor(node)
File "/Users/ng/.asdf/installs/python/3.8.0/lib/python3.8/ast.py", line 368, in generic_visit
self.visit(item)
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/bugbear.py", line 144, in visit
super().visit(node)
File "/Users/ng/.asdf/installs/python/3.8.0/lib/python3.8/ast.py", line 360, in visit
return visitor(node)
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/bugbear.py", line 278, in visit_Try
self.generic_visit(node)
File "/Users/ng/.asdf/installs/python/3.8.0/lib/python3.8/ast.py", line 368, in generic_visit
self.visit(item)
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/bugbear.py", line 144, in visit
super().visit(node)
File "/Users/ng/.asdf/installs/python/3.8.0/lib/python3.8/ast.py", line 360, in visit
return visitor(node)
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/bugbear.py", line 153, in visit_ExceptHandler
names = [_to_name_str(e) for e in node.type.elts]
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/bugbear.py", line 153, in <listcomp>
names = [_to_name_str(e) for e in node.type.elts]
File "/Users/ng/temp/flake8-bugbear-B014/venv/lib/python3.8/site-packages/bugbear.py", line 118, in _to_name_str
assert isinstance(node, ast.Attribute)
AssertionError
Can you look at this? Thanks!
Thanks for the report. Our AST code definitely won't support that. PRs welcome, otherwise I'll eventually try.
If you are interested in trying a PR, I would use this example as a new test case and then add support making it pass for the B014
.
I've fixed my code to avoid the issue, so there is no rush from my side.
I call that a workaround, as your code is valid :) Thanks for being patient.
I'm getting the following error in some case:
B014 Redundant exception types in
except (IOError, OSError):
. Writeexcept ():
, which catches exactly the same exceptions.
Notice that the recommendation of using except ():
is actually invalid it should say except OSError:
(in Python 3.3+).
@cooperlees This seems to be fine now, so can probably be closed.
@mvaled This also seems to be fine now, the following code generates the following output:
try:
pass
except (IOError, OSError):
pass
B014 Redundant exception types in `except (IOError, OSError):`. Write `except OSError:`, which catches exactly the same exceptions.
Thanks - I'm not very good with chasing this up!