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

Rewrite list(x for x in y) to list(y) #10403

Closed
charliermarsh opened this issue Mar 14, 2024 · 3 comments
Closed

Rewrite list(x for x in y) to list(y) #10403

charliermarsh opened this issue Mar 14, 2024 · 3 comments
Labels
help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@charliermarsh
Copy link
Member

Given list(x for x in y), we first hit C400, which fixes to [x for x in y], which hits C416, which fixes to list(y). We should consider short-circuiting this.

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule help wanted Contributions especially welcome labels Mar 14, 2024
@boolean-light
Copy link
Contributor

I would like to contribute on this. Should I implement it on inside C400 or add an another rule which matches only expression of type list(x for x in y)?

@charliermarsh
Copy link
Member Author

I think we should augment C400 to support it.

@charliermarsh
Copy link
Member Author

Closed by #10419. Thanks @boolean-light!

charliermarsh pushed a commit that referenced this issue Mar 15, 2024
…matches `C416` (#10419)

## Summary

Short-circuit implementation mentioned in #10403.

I implemented this by extending C400:
- Made `UnnecessaryGeneratorList` have information of whether the the
short-circuiting occurred (to put diagnostic)
- Add additional check for whether in `unnecessary_generator_list`
function.

Please give me suggestions if you think this isn't the best way to
handle this :)

## Test Plan

Extended `C400.py` a little, and written the cases where:
- Code could be converted to one single conversion to `list` e.g.
`list(x for x in range(3))` -> `list(range(3))`
- Code couldn't be converted to one single conversion to `list` e.g.
`list(2 * x for x in range(3))` -> `[2 * x for x in range(3)]`
- `list` function is not built-in, and should not modify the code in any
way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions especially welcome rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

2 participants