Derived from the refurb linter.
Fix is sometimes available.
This rule is unstable and in preview. The
--preview flag is required for use.
What it does#
Checks for uses of
enumerate that discard either the index or the value
when iterating over a sequence.
Why is this bad?#
enumerate function is useful when you need both the index and
value of a sequence.
If you only need the index or values of a sequence, you should iterate over
range(len(...)) or the sequence itself, respectively, instead. This is
more efficient and communicates the intent of the code more clearly.
This rule is prone to false negatives due to type inference limitations;
namely, it will only suggest a fix using the
len builtin function if the
sequence passed to
enumerate is an instantiated as a list, set, dict, or
tuple literal, or annotated as such with a type annotation.
len builtin function is not defined for all object types (such as
generators), and so refactoring to use
enumerate is not always