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

Extend B017 to pytest.raises

Open tsx opened this issue 3 years ago • 1 comments

with pytest.raises(Exception): is very similar to assertRaises, and all the justification of B017 applies to that too.

tsx avatar Apr 19 '21 10:04 tsx

Sounds reasonable. Unit-tested adding to B017 will be merged.

cooperlees avatar Nov 28 '21 17:11 cooperlees

I have a case where I use:

with pytest.raises(Exception, match='Parsing error'):
    call_my_func()

Currently, B017 catches this as a problematic but I have a valid match here which catches a very specific error message

AbdealiLoKo avatar Jan 16 '23 11:01 AbdealiLoKo

I still feel it's a good practice to subclass Exception here too ...

If someone wants to add if match= is passed to not error, we can ...

cooperlees avatar Jan 16 '23 19:01 cooperlees

The error currently says Either assert for a more specific exception (builtin or custom), use assertRaisesRegex, or use the context manager form of assertRaises.

Which I assume refers to the unittest functions. But, it'd be weird to suggest using assertRaisesRegex() and then say that match is not allowed.

Dreamsorcerer avatar Jan 16 '23 19:01 Dreamsorcerer

I have a case where I use:

with pytest.raises(Exception, match='Parsing error'):
    call_my_func()

Currently, B017 catches this as a problematic but I have a valid match here which catches a very specific error message

@AbdealiLoKo As a workaround, you can use https://github.com/m-burst/flake8-pytest-style/blob/master/docs/rules/PT011.md to catch that.

mrcljx avatar Jan 19 '23 11:01 mrcljx

Sure thanks @mrcljx will take a look.

AbdealiLoKo avatar Jan 19 '23 18:01 AbdealiLoKo

Appears this has been resolved (probably someone forgot the 'fixes' in a PR?).

Dreamsorcerer avatar Jan 22 '23 14:01 Dreamsorcerer

Yup. This is all done. Thanks for the nudge.

cooperlees avatar Jan 22 '23 23:01 cooperlees

FWIW - I dislike using assertRaisesRegex too, just make a custom exception type. I do accept there are edge cases where you're testing something you can't easily change etc. etc.

cooperlees avatar Jan 22 '23 23:01 cooperlees