flake8-bugbear
A plugin for flake8 finding likely bugs and design problems in your program. It contains warnings that don't belong in pyflakes and pycodestyle, offering more opinionated checks. The current version is 25.11.29, with releases occurring several times a year, often adding new checks or refining existing ones.
Warnings
- breaking Starting with version 25.10.21, flake8-bugbear dropped support for Python 3.9. It now requires Python 3.10 or newer.
- breaking If you are using flake8 version 5.0 or newer, checks for plugins (including flake8-bugbear) are no longer enabled by default unless explicitly selected. This can lead to flake8-bugbear checks not running.
- deprecated Bugbear's internal enforcement for explicit selection of its 'opinionated warnings' (B9xx checks) is deprecated and will be removed in a future release. While currently requiring explicit selection, the mechanism for doing so will change.
- gotcha Using mutable objects (like lists or dictionaries) as default arguments in function definitions can lead to unexpected behavior, as the default object is shared across all calls.
- gotcha Bare `except:` statements (B001) catch all exceptions, including `SystemExit`, `KeyboardInterrupt`, and `MemoryError`, which can hide critical issues or prevent graceful shutdowns.
- gotcha flake8-bugbear regularly introduces new checks (e.g., B042 for missing `super().__init__` in custom exceptions, B043 for `delattr` with constant, B912 for `map()` without `strict=`). Upgrading the library may introduce new warnings in existing code.
- gotcha The B9xx series of checks are considered 'opinionated' and are often not enabled by default. They require explicit selection in your `flake8` configuration.
Install
-
pip install flake8-bugbear
Imports
- N/A
N/A
Quickstart
# bad_code.py
def mutable_default(a, b=[]):
b.append(a)
return b
result1 = mutable_default(1)
result2 = mutable_default(2) # This will trigger B006
def bare_except_example():
try:
1/0
except: # This will trigger B001
pass
class MyCustomException(Exception):
def __init__(self, message):
self.message = message
# B042 will warn about missing super().__init__
delattr(object(), 'attribute') # B043 will warn about delattr with constant
# Run from your terminal after saving as bad_code.py:
# pip install flake8 flake8-bugbear
# flake8 bad_code.py