-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 preview changes to tuple unpacking #8884
Comments
Thanks for issue. I think the first step may be to just change the violation message to suggest using a It seems like a separate error code might help with your unused_a, unused_b = foo() We should probably still allow a fix for this to remove both variables as they would for unused_c = foo() Perhaps you could try demoting the fix for F841 to unsafe? |
Thanks, yup, custom error message mentioning underscore prefix is definitely a good step and is maybe enough to solve this issue. Note one reason why separate error code is viable here is that flake8 doesn't complain about this tuple unpacking case. The reason I mention the |
I'd like to voice my support a separate error code for unused variable in unpacking. The regular F841 is unambiguously useful, but having to add underscores for the unpacking case is just noise in most cases. There are some cases where it might be useful, e.g. if trying to mimic Go-style error handling in Python, Other languages have a special treatment for a |
@bluetech I think the proposal here is that we'd avoid raising a violation for |
@zanieb I may be misunderstanding your message, but as far as I can see,
(To be clear, I'll completely accept an answer that my preference is too specific and not worth the complexity and I should just use |
Hm. Personally, I don't see a violation for |
The difference is that (in codebases I work on) it's pretty common to unpack tuples and not use all the elements. There isn't dead code to be removed and so the signal to noise ratio ends up being different (especially without autofix). The other difference is that the autofix for the old behaviour is something I don't usually want (it's too conservative and can end up leaving dead code confusingly looking like it has side effects), whereas an autofix for new behaviour is something I'd always want. Note that ruff already has different codes for different kinds of unused variables: B007 is already a different error code. |
To add my answer: The difference is, with In Rust |
For a separate error code, there is an edge case which may want to be treated differently - if all members in the unpacking are unused, it probably should still produce |
Maybe... note such an unpacking isn't safe to remove entirely because it acts as an assertion on length. @zanieb @charliermarsh would you accept a PR moving this check to a different error code? (There's no other way of getting my ideal autofix behaviour here is there?) |
@hauntsaninja -- I'm fine with moving this to a separate rule like |
To be clear, your "ideal autofix" is that we do offer the "prefix unused tuple elements" fix but have a way to disable the "remove the left-hand side" autofix? |
Yes, that is my ideal autofix behaviour. Context: |
I'm returning this to "needs-decision" after we've discussed this more internally. From my notes:
We concluded that we need to develop a guideline that specifies when it is okay to have multiple rules that detect the same or very similar code smell and when it is not. |
I would support splitting this out into a separate check. In general, I think it's very useful for users for rules to be as fine-grained as possible so that they can easily select exactly which diagnostics and fixes they find to be helpful for them. This also seems to be to be distinct enough that I think users would understand why this would be a separate check. For an assignment that doesn't involve unpacking, such as
I think for a lot of our users, the ability we have to provide autofixes is a key value add over other tools in the Python ecosystem. I know when I first started using Ruff, this was a far bigger attraction than the performance gains that Ruff boasted. I think changes that make it easier for people to enable autofixes on their code are pretty valuable. |
We ran into this issue while migrating from flake8 to ruff, and support this change being split into another rule. F841 hasn't been a controversial error code to fix for us under flake8. In addition to what's been said above, I'll also add:
Our best solution for now is to ignore F841 in our preview-enabled config and run it separately via |
I'm added to the idea of splitting this rule for tuple unpacking. I can understand the reluctance to create different rules for similar situations, but I think that the all of this discussion proves that the unpacking of tuple is a behavior specific enough to have a distinc implementation. |
Okay, let's go ahead and split this out into a separate check (probably in the RUF category). Thanks for the discussion, all! PRs for this are welcome. |
#8489 changes F841 so that it complains about unused variables resulting from tuple unpacking.
There are a few issues:
An autofix to automatically add the underscore prefix would help!Implemented in F841: support fixing unused assignments in tuples by renaming variables #9107B007
is separate fromF841
. This also helps if you don't find the new lint useful, which I don't reallyThe text was updated successfully, but these errors were encountered: