Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add flake8-bugbear to the test suite #9279

Closed
ShadowJonathan opened this issue Jan 30, 2021 · 2 comments
Closed

Add flake8-bugbear to the test suite #9279

ShadowJonathan opened this issue Jan 30, 2021 · 2 comments
Labels
T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks.

Comments

@ShadowJonathan
Copy link
Contributor

Adding the flake8-bugbear plugin can help us catch bugs early before they occur, adding it on the codebase produces a enormous amount of the following codes;

  • B001 Do not use bare 'except:', it also catches unexpected events like memory errors, interrupts, system exit, and so on. Prefer 'except Exception:'. If you're sure what you're doing, be explicit and write 'except BaseException:'.
  • B006 Do not use mutable data structures for argument defaults. They are created during function definition time. All calls to the function reuse this one instance of that data structure, persisting changes between them.
  • B007 Loop control variable 'encoded_signature' not used within the loop body. If this is intended, start the name with an underscore.
  • B008 Do not perform function calls in argument defaults. The call is performed only once at function definition time. All calls to your function will reuse the result of that definition-time function call. If this is intended, assign the function call to a module-level variable and use that variable as a default value.
  • B010 Do not call setattr with a constant attribute value, it is not any safer than normal property access.
  • B306 'BaseException.message' has been deprecated as of Python 2.6 and is removed in Python 3. Use 'str(e)' to access the user-readable message. Use 'e.args' to access arguments passed to the exception.

I think that's reasonable to take a closer look at where bugbear notes these linting errors, as it indeed can look likely for these bugs to exist there.

@ShadowJonathan
Copy link
Contributor Author

ShadowJonathan commented Jan 30, 2021

Worth noting: B90 (B901 through B903) enable some interesting "opinionated" (per readme) warnings, such as;

  • B901: Using 'return x' in a generator function used to be syntactically invalid in Python 2. In Python 3 'return x' can be used in a generator as a return value in conjunction with 'yield from'.
  • B902 Invalid first argument 'cls' used for instance method. Use the canonical first argument name in methods, i.e. self.
  • B903 Data class should either be immutable or use __slots__ to save memory. Use collections.namedtuple to generate an immutable class, or enumerate the attributes in a __slot__ declaration in the class to leave attributes mutable.

Of which, B902 and B903 trigger on the codebase.

B950 also gets triggered, which is a little more opinionated version of E501 (max line length per PEP8), but it's already checked by black, per setup.cfg

@erikjohnston erikjohnston added the T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks. label Feb 4, 2021
@ShadowJonathan
Copy link
Contributor Author

Superceded by #9366

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants