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

F841: support fixing unused assignments in tuples by renaming variables #9107

Merged
merged 1 commit into from
Dec 12, 2023

Conversation

sciyoshi
Copy link
Contributor

Summary

A fairly common pattern which triggers F841 is unused variables from tuple assignments, e.g.:

user, created = User.objects.get_or_create(...)
      ^ F841: Local variable `created` is assigned to but never used

This error is currently not auto-fixable.

This PR adds support for fixing the error automatically by renaming the unused variable to have a leading underscore (i.e. _created) iff the dummy-variable-rgx setting would match it.

I considered using renamers::Renamer here, but because by the nature of the error there should be no references to it, that seemed like overkill. Also note that the fix might break by shadowing the new name if it is already used elsewhere in the scope. I left it as is because

  1. the renamed variable matches the "unused" regex, so it should hopefully not already be used,
  2. the fix is marked as unsafe so it should be reviewed manually anyways, and
  3. I'm not actually sure how to check the scope for the new variable name 😅

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this makes sense to me. Appreciate the clear rationale too!

@charliermarsh charliermarsh added the fixes Related to suggested fixes for violations label Dec 12, 2023
@charliermarsh charliermarsh merged commit c306f85 into astral-sh:main Dec 12, 2023
17 checks passed
@sciyoshi sciyoshi deleted the f841-rename-tuple branch December 12, 2023 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants