flake8-blind-except
flake8-blind-except is a `flake8` extension that identifies "blind" `except:` statements in Python code, specifically bare `except:` or `except Exception:` without logging or re-raising. It helps enforce better error handling practices by flagging these broad catches. The current version is 0.2.1, and its release cadence is slow, indicating a mature and stable plugin.
Common errors
-
flake8: command not found
cause The `flake8` package, which is necessary to run `flake8-blind-except`, is not installed or not available in the current shell's PATH.fixInstall `flake8` and `flake8-blind-except` together: `pip install flake8 flake8-blind-except`. -
No B902 errors reported, even with blind `except:` statements in code.
cause `flake8-blind-except` might not be installed, or your `flake8` configuration is explicitly ignoring `B902` or selecting a limited set of checks that exclude it.fixFirst, verify installation with `pip show flake8-blind-except`. Then, check your `flake8` configuration files (e.g., `pyproject.toml`, `setup.cfg`, `.flake8`) for `ignore = B902` or `select = ...` directives. -
ModuleNotFoundError: No module named 'setuptools'
cause Older versions of `flake8-blind-except` (prior to 0.2.1) incorrectly listed `setuptools` in `install_requires`, which could lead to installation issues in some minimal environments.fixUpgrade `flake8-blind-except` to version 0.2.1 or newer: `pip install --upgrade flake8-blind-except`.
Warnings
- gotcha As a `flake8` plugin, `flake8-blind-except` relies on `flake8` itself being installed and correctly configured. Ensure `flake8` is installed in the same environment.
- gotcha Existing `flake8` configuration files (e.g., `pyproject.toml`, `setup.cfg`, `.flake8`) can override which checks are run. If `B902` errors are not showing up, check your `ignore` or `select` directives.
- gotcha The plugin specifically targets 'blind' excepts (bare `except:` or `except Exception:`). It does not flag more specific but still overly broad catches like `except BaseException:`, which may still hide important errors.
Install
-
pip install flake8-blind-except
Quickstart
# 1. Install flake8 and the plugin
# pip install flake8 flake8-blind-except
# 2. Create a Python file with a blind except (e.g., my_module.py)
# with open('my_module.py', 'w') as f:
# f.write("""try:\n 1/0\nexcept:\n pass # This will trigger B902\n""")
# 3. Run flake8 on the file
# This command will output the B902 error for the blind except
# flake8 my_module.py