Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugbear 21.4.1 + 21.4.2 fails with AttributeError #164

Closed
xqt opened this issue Apr 1, 2021 · 18 comments · Fixed by #165
Closed

bugbear 21.4.1 + 21.4.2 fails with AttributeError #164

xqt opened this issue Apr 1, 2021 · 18 comments · Fixed by #165

Comments

@xqt
Copy link

xqt commented Apr 1, 2021

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

@mraspaud
Copy link

mraspaud commented Apr 1, 2021

Same here, was just filing an issue myself:

We have automating lint checks with flake8 with bugbear running and it looks like the latest release from about an hour ago broke something. Here is what we are getting now
https://github.com/pytroll/satpy/pull/1628/checks?check_run_id=2248802984#step:6:23

AttributeError: 'Name' object has no attribute 'func'

Was running fine until then.

We are calling this with:
https://github.com/pytroll/satpy/blob/master/.github/workflows/ci.yaml#L25

Or are we doing something wrong?

Thanks
cc @djhoese

@cooperlees
Copy link
Collaborator

cooperlees commented Apr 1, 2021

No, this seems like a bug we will need to repro with tests and fix.

Two workarounds are:

  • roll back to 21.3.2
  • disable B017 in flake8 conf

I will try find cycles tonight, but my day is very busy. PR welcome.

cc: @cricalix

@cricalix
Copy link
Contributor

cricalix commented Apr 1, 2021

@xqt - could you give me a sample line of code that this is breaking on? I'm thinking I can fix with a hasattr(item_context, "func") in the if block, but it'd be good to have some more negative test cases to ensure I'm catching everything correctly.

@cooperlees
Copy link
Collaborator

Please test with #165 - There might be more edge cases but I think this will get it.

@cooperlees
Copy link
Collaborator

21.4.2 released. Will yank 21.4.1 from PyPI.

@cooperlees cooperlees reopened this Apr 1, 2021
@cooperlees
Copy link
Collaborator

@ambv - Can you please yank 21.4.1, I don't have access.

@DStape
Copy link

DStape commented Apr 1, 2021

Unfortunately with 21.4.2 I still experience:

File "~/.local/share/virtualenvs/BC5Y161a/lib/python3.8/site-packages/bugbear.py", line 446, in check_for_b017
    and item_context.args[0].id == "Exception"  # noqa W503
AttributeError: 'Attribute' object has no attribute 'id'

(was AttributeError: 'Attribute' object has no attribute 'func' in 21.4.1)

@cooperlees
Copy link
Collaborator

cooperlees commented Apr 1, 2021

We need some code examples please to add to the unit test code to ensure we fix.

Please pin to old version for now. I’d ya k the versions but I can’t. My express release didn’t help. Sorry. Going to need more conditions.

@cricalix
Copy link
Contributor

cricalix commented Apr 1, 2021

Hmm. Naive approach that's the fastest fix is possibly to put a try block on that whole set of code, and let it silently fail. Not the nicest way though, and we lose the bug reports that would enable me to improve the code.

The error you're getting says that you've got a line of code that triggers as being a function, having the attr attribute in the AST node for the Call, the attr is assertRaises, there's one argument (I was deliberately avoiding assertRaises(Exception, Bob)), and yet whatever that AST node is, it doesn't have an id attribute.

Do you have code that does something like self.assertRaises("string") @DStape ?

@cooperlees cooperlees changed the title bugbear 21.4.1 fails with AttributeError bugbear 21.4.1 + 21.4.2 fails with AttributeError Apr 1, 2021
@cooperlees
Copy link
Collaborator

Since i can't yank I'd like to forward fix or do something crude to stop people's CI dying all over the world ...

@cricalix
Copy link
Contributor

cricalix commented Apr 1, 2021

I have a somewhat forward fix about to go up, but I really can't tell if I'm fixing all the cases of the problem. Adding another condition to the tree walk to check the type of element for args[0].

@DStape
Copy link

DStape commented Apr 1, 2021

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

@cricalix
Copy link
Contributor

cricalix commented Apr 1, 2021

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.

@cricalix
Copy link
Contributor

cricalix commented Apr 1, 2021

K, #166 is up for review.

@cricalix
Copy link
Contributor

cricalix commented Apr 1, 2021

And yeah, it's a messy set of stuff in the merge; I haven't used git for several years now, and it shows. I can drop my fork and re-do the submission if needed.

@xqt
Copy link
Author

xqt commented Apr 2, 2021

@xqt - could you give me a sample line of code that this is breaking on? I'm thinking I can fix with a hasattr(item_context, "func") in the if block, but it'd be good to have some more negative test cases to ensure I'm catching everything correctly.

Difficult to give a code sample, our framework has 50.000 LOC. All I can say is that our tests passes with Python 3.5.7 but failed due to this issue with Python 3.6.9. There is one difference: Python 3.6 tests require mwparserfromhell and bs4 to be installed. And Python 3.5 uses BB 19.8.0.

Anyway this issue is solved with 21.4.3:
https://integration.wikimedia.org/ci/job/pywikibot-core-tox-docker/14958/console

Thank you

@csdev
Copy link

csdev commented Apr 2, 2021

21.4.3 works for me now. (21.4.1 was broken for me even though I don't use unittest.assertRaises -- I use pytest.raises on Python 3.7.9.)
However, if other users are still experiencing problems, perhaps you can make B017 an opt-in feature like B9xx until it is more stable?

@mraspaud
Copy link

Works for me too. Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants