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

[flake8-comprehensions] C413 autofix bug #4879

Closed
zariiii9003 opened this issue Jun 5, 2023 · 3 comments · Fixed by #4891
Closed

[flake8-comprehensions] C413 autofix bug #4879

zariiii9003 opened this issue Jun 5, 2023 · 3 comments · Fixed by #4891
Labels
bug Something isn't working question Asking for support or clarification

Comments

@zariiii9003
Copy link

The autofix for C413 can lead to bugs if the key parameter is used. Here is a little code example to show, that the results before and after the autofix are different.

original = [(1, 0), (2, 0), (3, 1), (4, 1)]
print(list(reversed(sorted(original, key=lambda x: x[1]))))
print(sorted(original, key=lambda x: x[1], reverse=True))

Here is the output:

[(4, 1), (3, 1), (2, 0), (1, 0)]
[(3, 1), (4, 1), (1, 0), (2, 0)]
@charliermarsh
Copy link
Member

Do reversed(...) and reverse=True break ties differently?

@charliermarsh charliermarsh added bug Something isn't working question Asking for support or clarification labels Jun 5, 2023
@charliermarsh
Copy link
Member

Yeah, it looks like reversed does a full reversal, whereas reverse ends up retaining the original order for ties.

@dhruvmanila
Copy link
Member

dhruvmanila commented Jun 6, 2023

Wow, didn't know about this. I think the solution would be to make the auto-fix "sometimes" and not fix it when there's a key parameter present. The key parameter makes the sorting different for reversed and sorted(..., reverse=True).

Edit: Not making the auto-fix "sometimes" but rather not flagging this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Asking for support or clarification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants