Skip to main content

flake8 plugin which checks for code that can be simpified

Project description

PyPI version Code on Github Actions Status Code style: black

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-statements
  • SIM103: Return the boolean condition directly
  • SIM104: Use 'yield from iterable' (introduced in Python 3.3, see PEP 380)
  • SIM105: Use 'contextlib.suppress(...)' instead of try-except-pass
  • SIM201: 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


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.5.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_simplify-0.5.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file flake8_simplify-0.5.0.tar.gz.

File metadata

  • Download URL: flake8_simplify-0.5.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.4

File hashes

Hashes for flake8_simplify-0.5.0.tar.gz
Algorithm Hash digest
SHA256 7b6b23382547c2d58783abdd4ffba464da210b46fcac0b6ef2ab81304f5d1905
MD5 2d78dd18d06fac105fc3102722402789
BLAKE2b-256 b679f116057a17be741589d683f61be3096ca86a57e16fd4a1f99c80f584aa6a

See more details on using hashes here.

File details

Details for the file flake8_simplify-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: flake8_simplify-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.4

File hashes

Hashes for flake8_simplify-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b6d04f70126c424d2758a6581d7cf03504028f75cb37b524bb7141ecdef6420
MD5 a3504db3a8e68ea0ae6594621f2dadf2
BLAKE2b-256 0e0b0050d9c9451e66f557317862a913d1d0150f163f37c093481217860093c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page