You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just saw this code in the PyTorch codebase and realized that I cannot think of a valid reason to use a generator comprehension like so (either should be removed or replaced with a call to iter).
b=range(1, 10)
print(sorted((aforainb)))
should either be
print(sorted(iter(b)))
or
print(sorted(b))
Obviously a more specific rule could be added for sorted (since we have a few specific sorted rules anyway), but it would be nice to have this be more generic as the (a for a in b) generator construct doesn't seem particularly useful and just adds an unnecessary context switch of the generator.
Is there a useful situation to use this generator construct? Or should we always flag it?
The text was updated successfully, but these errors were encountered:
Skylion007
changed the title
New Code: Detect useless generator pattern
Feature Request: (New Rule) Detect useless generator pattern
Apr 30, 2023
Description
I just saw this code in the PyTorch codebase and realized that I cannot think of a valid reason to use a generator comprehension like so (either should be removed or replaced with a call to
iter
).should either be
or
Obviously a more specific rule could be added for sorted (since we have a few specific sorted rules anyway), but it would be nice to have this be more generic as the
(a for a in b)
generator construct doesn't seem particularly useful and just adds an unnecessary context switch of the generator.Is there a useful situation to use this generator construct? Or should we always flag it?
The text was updated successfully, but these errors were encountered: