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

False positive in PERF402 when append is more complex. #5494

Closed
bgianfo opened this issue Jul 3, 2023 · 1 comment · Fixed by #5508
Closed

False positive in PERF402 when append is more complex. #5494

bgianfo opened this issue Jul 3, 2023 · 1 comment · Fixed by #5508
Assignees
Labels
bug Something isn't working

Comments

@bgianfo
Copy link

bgianfo commented Jul 3, 2023

The new rule PERF402 suggests to convert lists to trivial copies in cases where it's not really possible. Ideally the tool would not report such false positives.

If you're filing a bug report, please consider including the following information:

  • A minimal code snippet that reproduces the bug.
 # In this example you can't trivially convert this to a `list.copy` as the rule suggests.  
 for r in record_dict["values"]:
        list_of_stuff[r["other_value"]].append(r)
  • The command you invoked (e.g., ruff /path/to/file.py --fix), ideally including the --isolated flag.

    ruff --select=PERF402 example.py

  • The current Ruff settings (any relevant sections from your pyproject.toml).

    PERF ruleset is enabled

  • The current Ruff version (ruff --version).

    version = 0.0.276

@charliermarsh charliermarsh added the bug Something isn't working label Jul 3, 2023
@charliermarsh
Copy link
Member

Makes sense, we should probably require that the left-hand side expression is a static name (or at least an expression that doesn't depend on the iteration key). Thanks!

@charliermarsh charliermarsh added the help wanted Contributions especially welcome label Jul 3, 2023
@charliermarsh charliermarsh self-assigned this Jul 4, 2023
@charliermarsh charliermarsh removed the help wanted Contributions especially welcome label Jul 4, 2023
charliermarsh added a commit that referenced this issue Jul 4, 2023
## Summary

We need to avoid raising "rewrite as a comprehension" violations in
cases like:

```python
d = defaultdict(list)

for i in [1, 2, 3]:
    d[i].append(i**2)
```

Closes #5494.

Closes #5500.
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