C408 Unnecessary tuple call (rewrite as a literal)
PERF401 Use a list comprehension to create a transformed list
PLR1714 Consider merging multiple comparisons. Use a set if the elements are hashable.
RET503 Missing explicit return at the end of function able to return non-None value
RUF015 Prefer next(iter(channel_layer.channels)) over single element slice
SIM210 Use bool(...) instead of True if ... else False
% ruff rule PERF401
manual-list-comprehension (PERF401)
Derived from the Perflint linter.
What it does
Checks for for loops that can be replaced by a list comprehension.
Why is this bad?
When creating a transformed list from an existing list using a for-loop,
prefer a list comprehension. List comprehensions are more readable and
more performant.
Using the below as an example, the list comprehension is ~10% faster on
Python 3.11, and ~25% faster on Python 3.10.
Note that, as with all perflint rules, this is only intended as a
micro-optimization, and will have a negligible impact on performance in
most cases.
Example
original = list(range(10000))
filtered = []
for i in original:
if i % 2:
filtered.append(i)
Use instead:
original = list(range(10000))
filtered = [x for x in original if x % 2]
If you're appending to an existing list, use the extend method instead:
original = list(range(10000))
filtered.extend(x for x in original if x % 2)
Ruff rules:
tuple
call (rewrite as a literal)set
if the elements are hashable.return
at the end of function able to return non-None
valuenext(iter(channel_layer.channels))
over single element slicebool(...)
instead ofTrue if ... else False
%
ruff rule PERF401
manual-list-comprehension (PERF401)
Derived from the Perflint linter.
What it does
Checks for
for
loops that can be replaced by a list comprehension.Why is this bad?
When creating a transformed list from an existing list using a for-loop, prefer a list comprehension. List comprehensions are more readable and more performant.
Using the below as an example, the list comprehension is ~10% faster on Python 3.11, and ~25% faster on Python 3.10.
Note that, as with all
perflint
rules, this is only intended as a micro-optimization, and will have a negligible impact on performance in most cases.Example
Use instead:
If you're appending to an existing list, use the
extend
method instead: