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 https://img.shields.io/badge/code%20style-black-000000.svg

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

Installation

Install from pip with:

pip install flake8-comprehensions

Python 3.5-3.7 supported.

When installed it will automatically be run as part of flake8; you can check it is being picked up with:

$ flake8 --version
3.7.8 (flake8-comprehensions: 3.0.0, mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.8.0 on Linux

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/tuple) literal - rewrite as a set literal.

C406

Unnecessary (list/tuple) literal - rewrite as a dict literal.

C407

Unnecessary list comprehension - ‘<builtin>’ can take a generator.

C408

Unnecessary (dict/list/tuple) call - rewrite as a literal.

C409

Unnecessary (list/tuple) passed to tuple() - (remove the outer call to tuple()/rewrite as a tuple literal).

C410

Unnecessary (list/tuple) passed to list() - (remove the outer call to list()/rewrite as a list literal).

C411

Unnecessary list call - remove the outer call to list().

C412

Unnecessary list comprehension - ‘in’ can take a generator.

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:

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

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

  • Rewrite dict((x, f(x)) for x in foo) 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:

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

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

C405-406,C409-410: Unnecessary list/tuple literal

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

  • Rewrite tuple([1, 2]) or tuple((1, 2)) as (1, 2)

  • Rewrite tuple([]) as ()

  • Rewrite list([1, 2]) or list((1, 2)) as [1, 2]

  • Rewrite list([]) as []

  • Rewrite set([1, 2]) or set((1, 2)) as {1, 2}

  • Rewrite set([]) as set()

  • Rewrite dict([(1, 2)]) or dict(((1, 2),)) as {1: 2}

  • Rewrite dict([]) as {}

C407: Unnecessary list comprehension - ‘<builtin>’ can take a generator

It’s unnecessary to pass a list comprehension to some builtins that can take generators instead. For example:

  • Rewrite sum([x ** 2 for x in range(10)]) as sum(x ** 2 for x in range(10))

  • Rewrite all([foo.bar for foo in foos]) as all(foo.bar for foo in foos)

The list of builtins that are checked for are:

  • all

  • any

  • enumerate

  • frozenset

  • max

  • min

  • sorted

  • sum

  • tuple

C408: Unnecessary (dict/list/tuple) call - rewrite as a literal.

It’s slower to call e.g. dict() than using the empty literal, because the name dict must be looked up in the global scope in case it has been rebound. Same for the other two basic types here. For example:

  • Rewrite dict() as {}

  • Rewrite list() as []

  • Rewrite tuple() as ()

C411: Unnecessary list call - remove the outer call to list().

It’s unnecessary to use a list around list comprehension, since it is equivalent without it. For example:

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

C412: Unnecessary list comprehension - ‘in’ can take a generator.

It’s unnecessary to pass a list comprehension to ‘in’ that can take a generator instead. For example:

  • Rewrite y in [f(x) for x in foo] as y in (f(x) for x in foo)

History

3.0.1 (2019-10-28)

  • Fix version display on flake8 --version (removing dependency on cached-property). Thanks to Jon Dufresne.

3.0.0 (2019-10-25)

  • Update Flake8 support to 3.0+ only. 3.0.0 was released in 2016 and the plugin hasn’t been tested with it since.

2.3.0 (2019-10-25)

  • Converted setuptools metadata to configuration file. This meant removing the __version__ attribute from the package. If you want to inspect the installed version, use importlib.metadata.version("flake8-comprehensions") (docs / backport).

  • Add dependencies on cached-property and importlib-metadata.

  • Fix false negatives in C407 for cases when enumerate and sum() are passed more than one argument.

2.2.0 (2019-08-12)

  • Update Python support to 3.5-3.7, as 3.4 has reached its end of life.

  • C412 rule that complains about using list comprehension with in.

2.1.0 (2019-03-01)

  • Add missing builtin enumerate to C407.

2.0.0 (2019-02-02)

  • Drop Python 2 support, only Python 3.4+ is supported now.

1.4.1 (2017-05-17)

  • Fix false positives in C408 for calls using *args or **kwargs.

1.4.0 (2017-05-14)

  • Plugin now reserves the full C4XX code space rather than just C40X

  • C408 rule that complains about using tuple(), list(), or dict() instead of a literal.

  • C409 and C410 rules that complain about an unnecessary list or tuple that could be rewritten as a literal.

  • C411 rule that complains about using list comprehension inside a list() call.

1.3.0 (2017-05-01)

  • Don’t allow installation with Flake8 3.2.0 which doesn’t enable the plugin. This bug was fixed in Flake8 3.2.1.

  • Prevent false positives of C402 from generators of expressions that aren’t two-tuples.

  • C405 and C406 now also complain about unnecessary tuple literals.

1.2.1 (2016-06-27)

  • C407 rule that complains about unnecessary list comprehensions inside builtins that can work on generators.

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

Uploaded Source

Built Distribution

flake8_comprehensions-3.0.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flake8-comprehensions-3.0.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for flake8-comprehensions-3.0.1.tar.gz
Algorithm Hash digest
SHA256 d1337979289e2877693b49a862c3e193a443833e2843e0e2450d54abd93aea9a
MD5 2cea429246aa01d1108e9fe10620d7e7
BLAKE2b-256 f96eb90d98de08e8696fc377e2d1fcd1cb1332fc6e60befcd47d32c0e893839d

See more details on using hashes here.

File details

Details for the file flake8_comprehensions-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: flake8_comprehensions-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4

File hashes

Hashes for flake8_comprehensions-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d3a8339e262205a15a4a84f6363eabb63e2dbd8292c4655366aaa0452eb28496
MD5 e2097ecfcca687ef13377faeb61a3007
BLAKE2b-256 761f3a2eaf62799c2c7409db6eb71fb34b8ba64b4a0fcdbf26e1063349062825

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