Skip to main content

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

Project description

https://github.com/adamchainz/flake8-comprehensions/workflows/CI/badge.svg?branch=master https://img.shields.io/pypi/v/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:

python -m pip install flake8-comprehensions

Python 3.5 to 3.8 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 (dict/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 (dict/list/set) comprehension - ‘in’ can take a generator.

C413

Unnecessary list call around sorted().

C413

Unnecessary reversed call around sorted() - (use sorted(…, reverse=(True/False))/toggle reverse argument to sorted()).

C414

Unnecessary (list/reversed/set/sorted/tuple) call within list/set/sorted/tuple().

C415

Unnecessary subscript reversal of iterable within reversed/set/sorted().

C416

Unnecessary (list/set) comprehension - rewrite using list/set().

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)

  • Rewrite filter(lambda x: x % 2 == 0, [x ** 3 for x in range(10)]) as filter(lambda x: x % 2 == 0, (x ** 3 for x in range(10)))

The list of builtins that are checked for are:

  • all

  • any

  • enumerate

  • filter

  • frozenset

  • map

  • max

  • min

  • sorted

  • sum

  • tuple

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

It’s slower to call these types than to construct them with literals, e.g. dict() is slower than {}, because the name dict must be looked up in the global scope in case it has been rebound, and it includes the overhead of a function call. Same for the other two builtin 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 (dict/list/set) comprehension - ‘in’ can take a generator.

It’s unnecessary to pass a dict/list/set 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)

  • Rewrite y in {x ** 2 for x in foo} as y in (x ** 2 for x in foo)

C413: Unnecessary list/reversed call around sorted().

It’s unnecessary to use list() around sorted() as it already returns a list. It is also suboptimal to use reversed() around sorted() as the latter has a reverse argument. For example:

  • Rewrite list(sorted([2, 3, 1])) as sorted([2, 3, 1])

  • Rewrite reversed(sorted([2, 3, 1])) as sorted([2, 3, 1], reverse=True)

  • Rewrite reversed(sorted([2, 3, 1], reverse=True)) as sorted([2, 3, 1])

C414: Unnecessary (list/reversed/set/sorted/tuple) call within list/set/sorted/tuple().

It’s unnecessary to change the type of the iterable or change the order of elements within certain other function calls that will themselves define the order of the iterable or the type that is output. For example:

  • Rewrite list(list(iterable)) as list(iterable)

  • Rewrite list(tuple(iterable)) as list(iterable)

  • Rewrite tuple(list(iterable)) as tuple(iterable)

  • Rewrite tuple(tuple(iterable)) as tuple(iterable)

  • Rewrite set(set(iterable)) as set(iterable)

  • Rewrite set(list(iterable)) as set(iterable)

  • Rewrite set(tuple(iterable)) as set(iterable)

  • Rewrite set(sorted(iterable)) as set(iterable)

  • Rewrite set(reversed(iterable)) as set(iterable)

  • Rewrite sorted(list(iterable)) as sorted(iterable)

  • Rewrite sorted(tuple(iterable)) as sorted(iterable)

  • Rewrite sorted(sorted(iterable)) as sorted(iterable)

  • Rewrite sorted(reversed(iterable)) as sorted(iterable)

C415: Unnecessary subscript reversal of iterable within reversed/set/sorted().

It’s unnecessary to reverse the order of an iterable using a [::-1] before passing it into set() which will randomize the order, sorted() which will return a new sorted list, or reversed() which will effectively return the original iterable. For example:

  • Rewrite set(iterable[::-1]) as set(iterable)

  • Rewrite sorted(iterable[::-1]) as sorted(iterable, reverse=True)

  • Rewrite reversed(iterable[::-1]) as iterable

C416: Unnecessary (list/set) comprehension - rewrite using list/set().

It’s unnecessary to use a list comprehension if the elements are unchanged. The iterable should be wrapped in list() or set() instead. For example:

  • Rewrite [x for x in iterable] as list(iterable)

  • Rewrite {x for x in iterable} as set(iterable)

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

Uploaded Source

Built Distribution

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

flake8_comprehensions-3.2.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flake8-comprehensions-3.2.3.tar.gz
  • Upload date:
  • Size: 60.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for flake8-comprehensions-3.2.3.tar.gz
Algorithm Hash digest
SHA256 d5751acc0f7364794c71d06f113f4686d6e2e26146a50fa93130b9f200fe160d
MD5 006f2755c522285e8031e399e88c417a
BLAKE2b-256 9af960063957dd096966b5323b0c7a3aa33e4ae81f0ef675d80bc8655c6347c7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flake8_comprehensions-3.2.3-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for flake8_comprehensions-3.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 44eaae9894aa15f86e0c86df1e218e7917494fab6f96d28f96a029c460f17d92
MD5 9c34def5346f6d46e5a68278ac4b0ca2
BLAKE2b-256 797170e635c3178bd42898be5171cf5dae95d1e24add061cb121327164c77643

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