-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[refurb] Mark the FURB161 fix unsafe except for integers and booleans
#17240
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
Conversation
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this. Could you update the rule's documentation with a ## Fif safety header and explain when the fix is safe (or isn't)? This will also make reviewing easier
|
This PR doesn’t fully fix #16457. The point of that issue is that FURB161 should be unsafe by default, with a few exceptions. This PR keeps it safe by default, the only exception being string literals. |
Thanks for the explanation. I approved the modification to make the rule unsafe if we can not determine if the argument of the |
|
Using |
ntBre
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! Overall I think this is good, but we can simplify things a bit by using a fix Applicability, and then I think we can do a better job with type inference with ResolvedPythonType::from, specifically for Expr::BinOp (cases like 1 + 2) and for Expr::UnOp (cases like -1).
If we extend the safe handling to bool, we may also be able to resolve expressions like True or False.
...linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB161_FURB161.py.snap
Show resolved
Hide resolved
...linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB161_FURB161.py.snap
Outdated
Show resolved
Hide resolved
Co-authored-by: Brent Westbrook <[email protected]>
Hey @ntBre |
|
Thanks, it looks good to me! Let's save this for a follow-up PR like the ruff/crates/ruff_python_semantic/src/analyze/typing.rs Lines 968 to 971 in 27a315b
You definitely don't have to work on this, just a couple of ideas if you're interested :) I pushed two tiny tweaks:
I'll merge as soon as CI passes! |
refurb] Unsafe fixes in FURB161refurb] Mark the FURB161 fix unsafe except for integers and booleans
The PR fixes #16457 .
Specifically,
FURB161is marked safe, but the rule generates safe fixes only in specific cases. Therefore, we attempt to mark the fix as unsafe when we are not in one of these cases.For instances, the fix is marked as aunsafe just in case of strings (as pointed out in the issue). Let me know if I should change something.