Skip to content

Use comprehensions #9285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Mar 15, 2024

Description

Use dict, list, and set comprehensions to improve the readability and performance of Python code.

% ruff check --select=C4,PERF401

rest_framework/schemas/coreapi.py:383:27: C419 Unnecessary list comprehension
rest_framework/utils/field_mapping.py:70:17: C405 Unnecessary `list` literal (rewrite as a `set` literal)
rest_framework/views.py:360:17: PERF401 Use a list comprehension to create a transformed list
tests/schemas/test_openapi.py:265:16: C414 Unnecessary `list` call within `sorted()`
tests/schemas/test_openapi.py:369:16: C414 Unnecessary `list` call within `sorted()`
tests/schemas/test_openapi.py:401:16: C414 Unnecessary `list` call within `sorted()`
tests/test_model_serializer.py:235:60: C408 Unnecessary `dict` call (rewrite as a literal)
tests/test_requests_client.py:58:25: C416 Unnecessary `dict` comprehension (rewrite using `dict()`)
tests/test_requests_client.py:65:25: C416 Unnecessary `dict` comprehension (rewrite using `dict()`)
tests/test_validators.py:118:13: C419 Unnecessary list comprehension
tests/test_validators.py:124:13: C419 Unnecessary list comprehension
tests/utils.py:30:20: C419 Unnecessary list comprehension

% ruff check --select=C4 --fix --unsafe-fixes

Found 11 errors (11 fixed, 0 remaining).

% ruff rule C414

unnecessary-double-cast-or-process (C414)

Derived from the flake8-comprehensions linter.

Fix is always available.

What it does

Checks for unnecessary list, reversed, set, sorted, and tuple
call within list, set, sorted, and tuple calls.

Why is this bad?

It's unnecessary to double-cast or double-process iterables by wrapping
the listed functions within an additional list, set, sorted, or
tuple call. Doing so is redundant and can be confusing for readers.

Examples

list(tuple(iterable))

Use instead:

list(iterable)

This rule applies to a variety of functions, including list, reversed,
set, sorted, and tuple. For example:

  • Instead of list(list(iterable)), use list(iterable).
  • Instead of list(tuple(iterable)), use list(iterable).
  • Instead of tuple(list(iterable)), use tuple(iterable).
  • Instead of tuple(tuple(iterable)), use tuple(iterable).
  • Instead of set(set(iterable)), use set(iterable).
  • Instead of set(list(iterable)), use set(iterable).
  • Instead of set(tuple(iterable)), use set(iterable).
  • Instead of set(sorted(iterable)), use set(iterable).
  • Instead of set(reversed(iterable)), use set(iterable).
  • Instead of sorted(list(iterable)), use sorted(iterable).
  • Instead of sorted(tuple(iterable)), use sorted(iterable).
  • Instead of sorted(sorted(iterable)), use sorted(iterable).
  • Instead of sorted(reversed(iterable)), use sorted(iterable).

Fix safety

This rule's fix is marked as unsafe, as it may occasionally drop comments
when rewriting the call. In most cases, though, comments will be preserved.

@cclauss cclauss closed this Mar 21, 2024
@cclauss cclauss deleted the use-comprehensions branch March 21, 2024 11:12
@tomchristie
Copy link
Member

Thanks for the close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants