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

PERF101 false positive #9925

Closed
Skylion007 opened this issue Feb 10, 2024 · 2 comments · Fixed by #9955
Closed

PERF101 false positive #9925

Skylion007 opened this issue Feb 10, 2024 · 2 comments · Fixed by #9955
Assignees
Labels
bug Something isn't working

Comments

@Skylion007
Copy link

Skylion007 commented Feb 10, 2024

I was running ruff on Sympy, applied the experimental ruff PERF rules and hit a bug. When running it on this file at this commit:
https://github.com/sympy/sympy/blob/b3d060459054d0cbd925a06fc7719a6f3b498a91/sympy/polys/galoistools.py#L1812 it recommends removing the list cast, but one can't do that because the inner while loop modifies the list that it is iterating over leading to an error. I tried to extract a smaller reproducer out of this, but was unable to.

version used: ruff 0.2.0
ruff --select PERF --show-source --fix file.py leads to the invalid fix.

sympy/polys/galoistools.py:1812:18: PERF101 [*] Do not cast an iterable to `list` before iterating over it
     |
1811 |     for k in range(1, len(V)):
1812 |         for f in list(factors):
     |                  ^^^^^^^^^^^^^ PERF101
1813 |             s = K.zero
     |
     = help: Remove `list()` cast

worse, this is a safe fix and it makes the file invalid.

@Skylion007 Skylion007 changed the title PERF401 false positive PERF101 false positive Feb 10, 2024
@charliermarsh charliermarsh added the bug Something isn't working label Feb 10, 2024
@charliermarsh
Copy link
Member

Makes sense, thanks for filing!

@charliermarsh charliermarsh self-assigned this Feb 11, 2024
@charliermarsh
Copy link
Member

I'll fix this today.

charliermarsh added a commit that referenced this issue Feb 12, 2024
## Summary

This PR ensures that if a list `x` is modified within a `for` loop, we
avoid flagging `list(x)` as unnecessary. Previously, we only detected
calls to exactly `.append`, and they couldn't be nested within other
statements.

Closes #9925.
nkxxll pushed a commit to nkxxll/ruff that referenced this issue Mar 10, 2024
…#9955)

## Summary

This PR ensures that if a list `x` is modified within a `for` loop, we
avoid flagging `list(x)` as unnecessary. Previously, we only detected
calls to exactly `.append`, and they couldn't be nested within other
statements.

Closes astral-sh#9925.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants