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'

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

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.4.1.tar.gz (6.6 kB view hashes)

Uploaded Source

Built Distribution

flake8_simplify-0.4.1-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

Supported by

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