flake8-simplify
flake8-simplify is a plugin for the Flake8 linter that identifies and suggests simplifications for Python code. It helps developers improve code readability, reduce complexity, and catch common minor mistakes by checking for patterns that can be written more concisely. The current version is 0.30.0, and it maintains a consistent release cadence with regular updates introducing new rules and maintenance fixes.
Warnings
- breaking The minimum Python version required by `flake8-simplify` has increased. Version `0.30.0` and newer require Python `>=3.9.0`. Older versions (e.g., `0.2.0`) supported Python `3.6` to `3.8`. Users on older Python installations must upgrade their Python version or pin an older `flake8-simplify` version.
- breaking Several `SIM` rules, including `SIM119`, `SIM204-207`, `SIM122`, and `SIM123`, have been moved to the separate `flake8-scream` plugin. If your project relied on these specific checks, they will no longer be performed by `flake8-simplify` alone.
- deprecated The `SIM106` rule ('Handle error-cases first') has been removed due to an excessive number of false positives. Code that previously triggered this warning will no longer do so, which might lead to a perceived gap in linting coverage if this rule was actively used.
- gotcha Rules prefixed with `SIM9xx` are considered experimental. Their behavior, error codes, or even their inclusion might change significantly (or be promoted to a stable `SIMxxx` code) in future releases without adhering to standard breaking change protocols.
- gotcha Versions prior to `0.19.3` of `flake8-simplify` had a known issue where the `SIM104` rule ('Use yield from iterable') could produce false positives, particularly in the context of `async` functions. This could lead to incorrect linting suggestions. [cite: '0.19.3' release notes]
Install
-
pip install flake8-simplify
Imports
- flake8-simplify
Not directly imported by user code; it's a plugin for the 'flake8' command-line tool.
Quickstart
import os
# SIM201: Use 'a != b' instead of 'not a == b'
def bad_comparison(a, b):
if not a == b:
return True
return False
# SIM101: Multiple isinstance-calls which can be merged into a single call
def bad_instance(obj):
if isinstance(obj, int) or isinstance(obj, float):
return "Number"
return "Other"
# To run flake8-simplify:
# 1. Save the code above as example.py
# 2. Open your terminal in the same directory
# 3. Run: pip install flake8 flake8-simplify
# 4. Run: flake8 example.py