Bandit conflicts with pyflakes
See original GitHub issueBandit is conflicting with pyflakes when both are used together, due to how the monkeypatching of ast nodes is done by both projects.
The issue was diagnosed on the flake8 bug tracker at https://gitlab.com/pycqa/flake8/issues/536#note_166421843
Both bandit and pyflakes monkeypatch the ast nodes, then inspect that state later. In this case, the bandit monkeypatching interferes with pyflakes detection of its own monkeypatching.
Here’s the test case provided by @asottile in the flake8 issue:
>>> from bandit.core.config import BanditConfig
>>> from bandit.core.meta_ast import BanditMetaAst
>>> from bandit.core.metrics import Metrics
>>> from bandit.core.node_visitor import BanditNodeVisitor
>>> from bandit.core.test_set import BanditTestSet
>>> from pyflakes.checker import Checker
>>> import ast
>>> profile = {}
>>> bnv = BanditNodeVisitor(
...     'filename',
...     BanditMetaAst(),
...     BanditTestSet(BanditConfig(), profile=profile),
...     False,
...     [],
...     Metrics(),
... )
>>> tree = ast.parse("""def test():
...     try:
...         x = 5
...         if True:
...             x = 10  # noqa: F841
...     except AttributeError:
...         pass
... """)
>>> bnv.generic_visit(tree)
>>> Checker(tree=tree)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 673, in __init__
    self.runDeferred(self._deferredFunctions)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 710, in runDeferred
    handler()
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1474, in runFunction
    self.handleChildren(node, omit='decorator_list')
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1073, in handleChildren
    self.handleNode(node, tree)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1120, in handleNode
    handler(node)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1623, in TRY
    self.handleNode(child, node)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1120, in handleNode
    handler(node)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1073, in handleChildren
    self.handleNode(node, tree)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1120, in handleNode
    handler(node)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1073, in handleChildren
    self.handleNode(node, tree)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1120, in handleNode
    handler(node)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1353, in NAME
    self.handleNodeStore(node)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 1017, in handleNodeStore
    self.addBinding(node, binding)
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 876, in addBinding
    not self.differentForks(node, existing.source)):
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 857, in differentForks
    if self.descendantOf(lnode, items, ancestor) ^ \
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 835, in descendantOf
    if self.getCommonAncestor(node, a, stop):
  File "/tmp/t/venv/lib/python3.5/site-packages/pyflakes/checker.py", line 827, in getCommonAncestor
    if (lnode.depth > rnode.depth):
AttributeError: 'ExceptHandler' object has no attribute 'depth'
Can this please get fixed inside bandit?
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
 Top Results From Across the Web
Top Results From Across the Web
Using Bandit in the Field - Kevin London's blog
Bandit is an open source tool that runs security checks for all the most common insecure Python functions in your code and gives...
Read more >Hypermodern Python 3: Linting
Bandit is a tool designed to find common security issues in Python code. Install it via the flake8-bandit plugin: # noxfile.py
Read more >"pyflakes" failed during execution due to "'ExceptHandler' object ...
flake8 fails to run pyflakes when flake-bandit version is >=2.0.0 changing flake-bandit version to 1.0.2 fixes the error. Similar error was reported earlier ......
Read more >Python Code Quality: Tools & Best Practices
PyFlakes, Logical, Analyzes programs and detects various errors ; pycodestyle, Stylistic, Checks against some of the style conventions in PEP 8 ; pydocstyle ......
Read more >Resolving new pip backtracking runtime issue
I would not recommend this generally but in some cases you can have a working set of packages versions although there are some...
Read more > Top Related Medium Post
Top Related Medium Post
No results found
 Top Related StackOverflow Question
Top Related StackOverflow Question
No results found
 Troubleshoot Live Code
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free Top Related Reddit Thread
Top Related Reddit Thread
No results found
 Top Related Hackernoon Post
Top Related Hackernoon Post
No results found
 Top Related Tweet
Top Related Tweet
No results found
 Top Related Dev.to Post
Top Related Dev.to Post
No results found
 Top Related Hashnode Post
Top Related Hashnode Post
No results found

I will take a stab at it today and submit a PR today. Hopefully we can get this closed out in the next day or two and fix the issues people are experiencing
Please see PR #492 fixing this issue