flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

B014 fails on `except (*exceptions, TypeError):`

Open Gr1N opened this issue 5 years ago • 4 comments

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!

Gr1N avatar Jan 08 '20 19:01 Gr1N

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.

cooperlees avatar Jan 08 '20 19:01 cooperlees

I've fixed my code to avoid the issue, so there is no rush from my side.

Gr1N avatar Jan 09 '20 16:01 Gr1N

I call that a workaround, as your code is valid :) Thanks for being patient.

cooperlees avatar Jan 09 '20 16:01 cooperlees

I'm getting the following error in some case:

B014 Redundant exception types in except (IOError, OSError):. Write except ():, 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+).

mvaled avatar Sep 11 '20 21:09 mvaled

@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.

FozzieHi avatar Feb 20 '23 19:02 FozzieHi

Thanks - I'm not very good with chasing this up!

cooperlees avatar Feb 20 '23 19:02 cooperlees