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

Possible false positive for B015

Open henryiii opened this issue 5 years ago • 4 comments

We discovered some potential false positives in pybind11 (see here) - specifically, a test like this reports:

with pytest.raises(TypeError):
    y < object()

Might not be fixable, and this check has helped in other cases, but thought I'd point it out here.

henryiii avatar Dec 18 '20 22:12 henryiii

Yeah, that is “technically pointless”, I guess. Thanks for stating the check has also helped. It seems lots of people have loved this check (myself included).

I think this will just have to be commented to be ignored. I like the comment as it shows the author’s intent and acknowledgement.

Let’s leave the issue open tho cause someone smarter than I might see a way to use the AST to notice we’re in a context manager & using raises, but I worry adding this complicates and could cause us to maybe miss a pointless comparison.

cooperlees avatar Dec 19 '20 19:12 cooperlees

Quite a few of my pointless comparisons have been in pytest.raises, actually, see https://github.com/scikit-hep/hist/commit/b9c447680ee7441049ae15e38e8d09b1494917d2 . The problem here is that the comparison itself raises, and that's what's being checked.

henryiii avatar Dec 19 '20 20:12 henryiii

FYI, this check actually caught a real bug in https://github.com/scikit-hep/awkward-1.0/pull/682 :)

henryiii avatar Jan 28 '21 14:01 henryiii

Yeah, that is “technically pointless”, I guess.

How so? I'm confused. AFAIC, the false-positive is not in question. B015 should not flag within a with pytest.raises(TypeError): context.

janosh avatar Aug 06 '22 02:08 janosh