id: prefer-generator-in-any-all
language: python
severity: hint
message: Use generator expressions instead of list comprehensions inside `any()` and `all()`.
note: |
This rule uses rewriters to transform each list comprehension argument
inside `any()` or `all()` calls into a generator expression. The
rewriter strips the enclosing brackets from each matched list comprehension.
Generator expressions avoid allocating an intermediate list when the
consuming function only needs to iterate once.
rewriters:
- id: strip-brackets
rule:
all:
- kind: list_comprehension
- pattern: $LC
transform:
INNER:
substring:
source: $LC
startChar: 1
endChar: -1
fix: $INNER
rule:
pattern: $FUNC($ARG)
constraints:
FUNC:
regex: ^(any|all)$
ARG:
kind: list_comprehension
transform:
NEW_ARG:
rewrite:
rewriters: [strip-brackets]
source: $ARG
fix: $FUNC($NEW_ARG)
files:
- "**/*.py"
- "!tests/**"
- "!**/test_*.py"
- "!**/*_test.py"