flake8-bugbear: bugbear 21.4.1 + 21.4.2 fails with AttributeError

Newly updated bugbear 21.4.1 fails with AttributeError:

21:06:51 Traceback (most recent call last):
21:06:51   File "/usr/lib/python3.6/multiprocessing/pool.py", line 119, in worker
21:06:51     result = (True, func(*args, **kwds))
21:06:51   File "/usr/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
21:06:51     return list(map(*args))
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/flake8/checker.py", line 676, in _run_checks
21:06:51     return checker.run_checks()
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/flake8/checker.py", line 589, in run_checks
21:06:51     self.run_ast_checks()
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/flake8/checker.py", line 494, in run_ast_checks
21:06:51     for (line_number, offset, text, _) in runner:
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 36, in run
21:06:51     visitor.visit(self.tree)
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 165, in visit
21:06:51     super().visit(node)
21:06:51   File "/usr/lib/python3.6/ast.py", line 253, in visit
21:06:51     return visitor(node)
21:06:51   File "/usr/lib/python3.6/ast.py", line 261, in generic_visit
21:06:51     self.visit(item)
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 165, in visit
21:06:51     super().visit(node)
21:06:51   File "/usr/lib/python3.6/ast.py", line 253, in visit
21:06:51     return visitor(node)
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 306, in visit_ClassDef
21:06:51     self.generic_visit(node)
21:06:51   File "/usr/lib/python3.6/ast.py", line 261, in generic_visit
21:06:51     self.visit(item)
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 165, in visit
21:06:51     super().visit(node)
21:06:51   File "/usr/lib/python3.6/ast.py", line 253, in visit
21:06:51     return visitor(node)
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 302, in visit_FunctionDef
21:06:51     self.generic_visit(node)
21:06:51   File "/usr/lib/python3.6/ast.py", line 261, in generic_visit
21:06:51     self.visit(item)
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 165, in visit
21:06:51     super().visit(node)
21:06:51   File "/usr/lib/python3.6/ast.py", line 253, in visit
21:06:51     return visitor(node)
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 321, in visit_With
21:06:51     self.check_for_b017(node)
21:06:51   File "/src/.tox/flake8-py36/lib/python3.6/site-packages/bugbear.py", line 442, in check_for_b017
21:06:51     hasattr(item_context.func, "attr")
21:06:51 AttributeError: 'Name' object has no attribute 'func'
21:06:51 """

See https://integration.wikimedia.org/ci/job/pywikibot-core-tox-docker/14952/console

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 18 (6 by maintainers)

Most upvoted comments

Ok, test case added, and confirms that my in-flight fix fixes that case. I’ll have to walk the AST tree to get a better feel for what your syntax is doing, but it looks like there’s another layer of nesting in that case.

@cooperlees, @cricalix , here’s a stripped down example of code bugbear is error-ing out on for us:

import asyncio
from unittest import TestCase
 
 
class Foo:
    pass


class TestFoo(TestCase):
    def test_(self):
        with self.assertRaises(asyncio.CancelledError):
        #from asyncio import CancelledError  # no AttributeError
        #with self.assertRaises(CancelledError):
            Foo()

Note that when CancelledError is imported from asyncio, the AttributeError does not occur.

K, #166 is up for review.