flake8 plugin which checks for code that can be simpified
Project description
flake8-simplify
A flake8 plugin that helps you simplify your code.
Installation
Install with pip
:
pip install flake8-simplify
Python 3.6 to 3.8 are supported.
Usage
Just call flake8 .
in your package or flake your.py
:
$ flake8 .
./foo/__init__.py:690:12: SIM101 Multiple isinstance-calls which can be merged into a single call for variable 'other'
Rules
SIM101
: Multiple isinstance-calls which can be merged into a single call by using a tuple as a second argument.SIM102
: Use a single if-statement instead of nested if-statementsSIM103
: Return the boolean condition directlySIM104
: Use 'yield from iterable' (introduced in Python 3.3, see PEP 380)SIM105
: Use 'contextlib.suppress(...)' instead of try-except-passSIM106
: Handle error-cases firstSIM201
: Use 'a != b' instead of 'not a == b'SIM202
: Use 'a == b' instead of 'not a != b'SIM203
: Use 'a not in b' instead of 'not (a in b)'SIM204
: Use 'a >= b' instead of 'not (a < b)'SIM205
: Use 'a > b' instead of 'not (a <= b)'SIM206
: Use 'a <= b' instead of 'not (a > b)'SIM207
: Use 'a < b' instead of 'not (a <= b)'SIM208
: Use 'a' instead of 'not (not a)'SIM210
: Use 'bool(a)' instead of 'True if a else False'SIM211
: Use 'not a' instead of 'False if a else True'SIM212
: Use 'a if a else b' instead of 'b if not a else a'SIM220
: Use 'False' instead of 'a and not a'SIM221
: Use 'True' instead of 'a or not a'SIM222
: Use 'True' instead of '... or True'SIM223
: Use 'False' instead of '... and False'
The SIM201
- SIM208
rules have one good reason to be ignored: When you are
checking an error condition:
if not correct_condition:
handle_error() # e.g. raise Exception
Examples
SIM101
# Bad
isinstance(a, int) or isinstance(a, float)
# Good
isinstance(a, (int, float))
SIM102
# Bad
if a:
if b:
c
# Good
if a and b:
c
SIM201
# Bad
not a == b
# Good
a != b
SIM202
# Bad
not a != b
# Good
a == b
SIM203
# Bad
not a in b
# Good
a not in b
SIM204
# Bad
not a < b
# Good
a >= b
SIM205
# Bad
not a <= b
# Good
a > b
SIM206
# Bad
not a > b
# Good
a <= b
SIM207
# Bad
not a >= b
# Good
a < b
SIM208
# Bad
not (not a)
# Good
a
SIM210
# Bad
True if a else False
# Good
bool(a)
SIM211
# Bad
False if a else True
# Good
not a
SIM212
# Bad
b if not a else a
# Good
a if a else b
SIM220
# Bad
a and not a
# Good
False
SIM221
# Bad
a or not a
# Good
True
SIM222
# Bad
... or True
# Good
True
SIM223
# Bad
... and False
# Good
False
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flake8_simplify-0.6.0.tar.gz
(7.9 kB
view hashes)
Built Distribution
Close
Hashes for flake8_simplify-0.6.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c1de01e5f0dab89ec58025132a4c688672d723dd633c770a266646da5ae9d5b |
|
MD5 | d9ada24374124708db094a17b9290032 |
|
BLAKE2b-256 | 92c23a0adc68748388411e6b4c88dae0ec0ed5d06266fea5816e54318c165504 |