Add B017 - detection for an evil form of assertRaises #163
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
with assertRaises(Exception):
is basically a "catch 'em all" assert that casts far too broad of a net when it comes to detecting failures in code being tested. Assertions should be testing specific failure cases, not "did Python throw /any/ type of error?", and so the context manager form, or the assertRaisesRegex form are far better to use.With the implementation of this code, it is possible that someone would write
with self.assertRaises(Exception) as ex:
and then never checkex
, but linters should then warn about the unused context variable.W503 had to be ignored because
test_selfclean_bugbear()
was upset by the warnings being raised for the syntax in use, despite that syntax being the syntax created by the recommended formatter, black.This kind of lint check can also be found in openstack/hacking as H202, but implementing that module for flake8 requires pulling in a lot of extras, and it's very opinionated about versions of the extras to import.