reimplemented-starmap (FURB140)#
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 generator expressions, list and set comprehensions that can
be replaced with itertools.starmap.
Why is this bad?#
When unpacking values from iterators to pass them directly to
a function, prefer itertools.starmap.
Using itertools.starmap is more concise and readable. Furthermore, it is
more efficient than generator expressions, and in some versions of Python,
it is more efficient than comprehensions.
Known problems#
Since Python 3.12, itertools.starmap is less efficient than
comprehensions (#7771). This is due to PEP 709, which made
comprehensions faster.
Example#
scores = [85, 100, 60]
passing_scores = [60, 80, 70]
def passed_test(score: int, passing_score: int) -> bool:
return score >= passing_score
passed_all_tests = all(
passed_test(score, passing_score)
for score, passing_score in zip(scores, passing_scores)
)
Use instead:
from itertools import starmap
scores = [85, 100, 60]
passing_scores = [60, 80, 70]
def passed_test(score: int, passing_score: int) -> bool:
return score >= passing_score
passed_all_tests = all(starmap(passed_test, zip(scores, passing_scores)))