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

Add B017 - detection for an evil form of assertRaises #163

Merged
merged 2 commits into from Mar 31, 2021
Merged

Add B017 - detection for an evil form of assertRaises #163

merged 2 commits into from Mar 31, 2021

Conversation

cricalix
Copy link
Contributor

@cricalix cricalix commented Mar 30, 2021

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 check ex, 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.

```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.
Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Can you just:

  • Remove version change (who knows when I will release, but will be soon)
  • See if you can get rid of W503, if not no big deal
  • Fix the context manager raises whitespace

Then also

  • Add to README your explanation like other checks
    • Add this to "Change Log" in README too please

bugbear.py Outdated Show resolved Hide resolved
bugbear.py Show resolved Hide resolved
tests/b017.py Show resolved Hide resolved
@cricalix cricalix removed their assignment Mar 31, 2021
Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good to me and ready to merge.

@cooperlees cooperlees merged commit 9d18e5f into PyCQA:master Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants