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.
pip install flake8-bugbearVerified import paths — ran on the pinned version, not inferred.
Install `flake8` and `flake8-bugbear`, then run `flake8` on a Python file. `flake8-bugbear`'s checks will be automatically included in the output, demonstrating common warnings like mutable default arguments (B006), bare excepts (B001), and issues with custom exception `__init__` (B042).
Upgrade your Python environment to 3.10 or a later version. If unable to upgrade Python, pin flake8-bugbear to a version less than 25.10.21 (e.g., `flake8-bugbear<25.10.21`).
Explicitly enable the B-series checks in your flake8 configuration (e.g., in `.flake8`, `setup.cfg`, or `pyproject.toml`) by adding `select = C,E,F,W,B` or by using the `--extend-select=B` command-line option.
It is recommended to use `extend-ignore` and `extend-select` in your flake8 configuration to manage which codes are active, aligning with future flake8 plugin best practices.
Initialize mutable defaults inside the function, typically by setting the default to `None` and then checking for `None` to create a new mutable object. Example: `def func(arg=None): arg = arg or []`.
Always specify the exception type to catch, usually `except Exception:`, or `except SomeSpecificError:` for more targeted handling. Use `except BaseException:` only when truly intending to catch all possible exceptions.
Review the release notes for new versions to understand new checks and their implications. Configure your `.flake8` or `pyproject.toml` to ignore specific new checks if they are not relevant to your project or if you need to address them incrementally.
Add `B9` to your `select` option in your `flake8` configuration (e.g., `select = C,E,F,W,B,B9`) to enable all opinionated checks, or select specific ones (e.g., `select = B901,B950`). Some B9xx checks might require ignoring other flake8 checks (e.g., `E501` for `B950`).