Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: (New Rule) Detect useless generator pattern #503

Open
Skylion007 opened this issue Apr 30, 2023 · 1 comment
Open

Feature Request: (New Rule) Detect useless generator pattern #503

Skylion007 opened this issue Apr 30, 2023 · 1 comment

Comments

@Skylion007
Copy link
Contributor

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).

b = range(1, 10)
print(sorted((a for a in b)))

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?

@Skylion007 Skylion007 changed the title New Code: Detect useless generator pattern Feature Request: (New Rule) Detect useless generator pattern Apr 30, 2023
@adamchainz
Copy link
Owner

This seems like a legitimate rule to add. Would you like to make a PR?

Karine-Bauch added a commit to Karine-Bauch/flake8-comprehensions that referenced this issue Jul 21, 2023
Karine-Bauch added a commit to Karine-Bauch/flake8-comprehensions that referenced this issue Jul 27, 2023
Karine-Bauch added a commit to Karine-Bauch/flake8-comprehensions that referenced this issue Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants