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
Use instead:
This rule applies to a variety of functions, including list()
, reversed()
,
set()
, sorted()
, and tuple()
. For example:
- Instead of
list(list(iterable))
, uselist(iterable)
. - Instead of
list(tuple(iterable))
, uselist(iterable)
. - Instead of
tuple(list(iterable))
, usetuple(iterable)
. - Instead of
tuple(tuple(iterable))
, usetuple(iterable)
. - Instead of
set(set(iterable))
, useset(iterable)
. - Instead of
set(list(iterable))
, useset(iterable)
. - Instead of
set(tuple(iterable))
, useset(iterable)
. - Instead of
set(sorted(iterable))
, useset(iterable)
. - Instead of
set(reversed(iterable))
, useset(iterable)
. - Instead of
sorted(list(iterable))
, usesorted(iterable)
. - Instead of
sorted(tuple(iterable))
, usesorted(iterable)
. - Instead of
sorted(sorted(iterable))
, usesorted(iterable)
. - Instead of
sorted(reversed(iterable))
, usesorted(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.