-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
B018 does not detect unused tuple expressions #14131
Comments
Flagging this as print("test"), Removing the Reasoning whether a function is only be possible in the most trivial cases because standard library functions in typeshed don't annotate if they have side effects. That's why Ruff's current behavior is correct because it would be unsafe to assume the function has no side effects. |
Indeed, and a Maybe the answer is just to enable flake8-commas linting... |
Actually, hmm. It's definitely still a useless expression (unassigned tuple), just a weirdly formatted one where the "fix" is to remove the |
Yeah. I think detecting unnecessary tuples could be in the spirit of the rule, but it could also be its own rule, similar to |
To me this seems to be working as I'd expect (but I'm happy to be overruled by the community). On the one hand we have a lint rule detecting whether an expression is useless. I would think that if removing something useless affected the behavior of the program, then it must not have been useless to begin with. In other words, "useless" seems, to me, to be synonymous with "can be removed with no effect". The expression in question is a tuple expression, and removing it would necessarily remove all of its members. There is no expression in the AST that's pointing to the "nonexistent second member": ~ echo "f()," | python -m ast
Module(
body=[
Expr(
value=Tuple(
elts=[
Call(
func=Name(id='f', ctx=Load()),
args=[],
keywords=[])],
ctx=Load()))],
type_ignores=[]) So there's not really an expression you could point to and call "useless", right? On the other hand, as you point out, we have a lint rule detecting trailing commas on bare tuples. So I'd think it would be the job of that lint rule to fix this expression. |
I think the idea here is that
I should have taken a closer look at that rule. I agree, we already have a rule that covers what you want. |
Ah, ok, I've properly understood what I want: pylint W0106 which does warn about such things def foo(): ...
[1, 2, foo()]
print("foo"),
obviously the fix is not to remove it entirely (as functions may have side effects) - it's one of
I don't think you'll need a type checker to do that (though pyright does detect the unused expression)
in #970 this lint is listed as implemented via B018, so... |
Ok thanks this helps me understand better! My takeaway is that I still feel like "useless" indicates "can be removed" not "should be modified", so I would vote to keep B018 from emitting a diagnostic for this case, but then you have |
Actual flake8-bugbear flags up this specific example:
|
Alright, I stand corrected! We should certainly have parity with bugbear for this. Thanks for your persistence in the face of my pedantry! |
B018 (useless-expression) does not detect function calls with bare tuples:
COM818 (trailing-comma-on-bare-tuple) detects this as it should, but I feel like B018 should flag this as well?
Ruff 0.7.2
The text was updated successfully, but these errors were encountered: