Skip to main content

A flake8 plugin to help you write better list/set/dict comprehensions.

Project description

flake8-comprehensions

https://img.shields.io/pypi/v/flake8-comprehensions.svg https://img.shields.io/travis/adamchainz/flake8-comprehensions.svg

A flake8 plugin that helps you write better list/set/dict comprehensions.

  • Free software: ISC license

Installation

Install from pip with:

pip install flake8-comprehensions

It will then automatically be run as part of flake8; you can check it has been picked up with:

$ flake8 --version
2.4.1 (pep8: 1.7.0, pyflakes: 0.8.1, flake8-comprehensions: 1.0.0, mccabe: 0.3.1) CPython 2.7.11 on Darwin

Rules

Code

Rule

C400

Unnecessary generator - rewrite as a list comprehension.

C401

Unnecessary generator - rewrite as a set comprehension.

C402

Unnecessary generator - rewrite as a dict comprehension.

C403

Unnecessary list comprehension - rewrite as a set comprehension.

C404

Unnecessary list comprehension - rewrite as a dict comprehension.

C405

Unnecessary list literal - rewrite as a set literal.

C406

Unnecessary list literal - rewrite as a dict literal.

Examples

C400-402: Unnecessary generator

It’s unnecessary to use list, set, or dict around a generator expression, since there are equivalent comprehensions for these types. For example:

  • list(f(x) for x in foo) is better as [f(x) for x in foo]

  • set(f(x) for x in foo) is better as {f(x) for x in foo}

  • dict((x, f(x)) for x in foo) is better as {x: f(x) for x in foo}

C403-404: Unnecessary list comprehension

It’s unnecessary to use a list comprehension inside a call to set or dict, since there are equivalent comprehensions for these types. For example:

  • set([f(x) for x in foo]) is better as {f(x) for x in foo}

  • dict([(x, f(x)) for x in foo]) is better as {x: f(x) for x in foo}

C405-406: Unnecessary list literal

It’s unnecessary to use a list literal within a call to set or dict since there is literal syntax for these types. For example:

  • set([1, 2]) is better as {1, 2}

  • set([]) is better as set()

  • dict([]) is better as {}

  • dict([(1, 2)]) is better as {1: 2}

History

Pending Release

  • New release notes here

1.2.0 (2016-07-11)

  • Split all rule codes by type. This allows granular selection of the rules in flake8 configuration.

1.1.1 (2016-04-06)

  • Fix crash on method calls

1.1.0 (2016-04-06)

  • C401 rule that complains about unnecessary list comprehensions inside calls to set() or dict().

  • C402 rule that complains about unnecessary list literals inside calls to set() or dict().

1.0.0 (2016-04-05)

  • C400 rule that complains about an unnecessary usage of a generator when a list/set/dict comprehension would do.

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-comprehensions-1.2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

flake8_comprehensions-1.2.0-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flake8-comprehensions-1.2.0.tar.gz.

File metadata

File hashes

Hashes for flake8-comprehensions-1.2.0.tar.gz
Algorithm Hash digest
SHA256 7a13f0a85cb6f6a28decbd521c5f38eb98c7e20b843accc85e9f69a27e164583
MD5 c67a4ec4cf8b62a6050c1cef93386ee4
BLAKE2b-256 831a7590b2c0689a903af4b042b6341755a58c1d61ece2501cce6731fe454efb

See more details on using hashes here.

File details

Details for the file flake8_comprehensions-1.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_comprehensions-1.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 efdd365fc9e9e5773e93d9891a74efe014760a23de68420cb989bf2b7e951986
MD5 dec6c78fd88daaf54d1605377e65b55a
BLAKE2b-256 e6b7f963b239c705a31536c4683f277b50e98eabee31670b2338534949cee071

See more details on using hashes here.

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