-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[refurb] Make the fix for FURB163 unsafe for log2, log10, *args, and deleted comments
#18645
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
…/log10, starred arg, or comments
|
|
do we have to update doc as well? because examples from the ussie and the doc are different |
|
although it should only work for float |
| false | ||
| } | ||
|
|
||
| fn is_float(expr: &Expr) -> bool { |
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.
i'm not sure, but maybe such a is_float already exists somewhere in the helper or something like that
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.
There's this:
ruff/crates/ruff_python_semantic/src/analyze/typing.rs
Lines 981 to 984 in 4cfaa39
| /// Test whether the given binding can be considered an instance of `float`. | |
| pub fn is_float(binding: &Binding, semantic: &SemanticModel) -> bool { | |
| check_type::<FloatChecker>(binding, semantic) | |
| } |
which would work on a Binding if you wanted to resolve variables too, but do we need to check if the argument is a float? You can see the same floating point behavior with less exotic arguments:
>>> math.log2(10)
3.321928094887362
>>> math.log(10, 2)
3.3219280948873626and I think we're already checking for number literals.
|
there's also a question about ruff_python_ast/src/generated.rs file rustrover writes that |
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! I just had a couple of suggestions.
And yes, we need to update the documentation with a ## Fix safety section because the fix is now unsafe in many cases.
| false | ||
| } | ||
|
|
||
| fn is_float(expr: &Expr) -> bool { |
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.
There's this:
ruff/crates/ruff_python_semantic/src/analyze/typing.rs
Lines 981 to 984 in 4cfaa39
| /// Test whether the given binding can be considered an instance of `float`. | |
| pub fn is_float(binding: &Binding, semantic: &SemanticModel) -> bool { | |
| check_type::<FloatChecker>(binding, semantic) | |
| } |
which would work on a Binding if you wanted to resolve variables too, but do we need to check if the argument is a float? You can see the same floating point behavior with less exotic arguments:
>>> math.log2(10)
3.321928094887362
>>> math.log(10, 2)
3.3219280948873626and I think we're already checking for number literals.
| Edit::range_replacement(format!("{binding}({number})"), call.range()), | ||
| [edit], | ||
| if (matches!(base, Base::Two | Base::Ten) && is_float(arg)) | ||
| || matches!(arg, Expr::Starred(_)) |
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.
nit: I think you can use arg.is_starred_expr here.
This is interesting, I guess clippy doesn't warn about this. We could probably open a separate issue or PR for this, if you wanted! |
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.
Nice! One more docs nit, but this is otherwise good to go.
| /// This rule's fix is marked as unsafe when the argument is a starred expression | ||
| /// or if there are comments within the function call range, | ||
| /// as either can affect semantics or readability. |
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.
I think I would treat these two cases separately. My understanding is that the fix is unsafe when comments are in the replacement range because they will be deleted, which seems a bit separate from either semantics or readability.
And I don't think the *args case has any effect on readability, it "just" changes the semantics of the code by raising an error when a different number of arguments are passed.
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.
if write it like this, i think it would make more sense:
This fix is marked unsafe when the argument is a starred expression, as this changes the call semantics and may raise runtime errors. It is also unsafe if comments are present within the call, as they will be removed. Additionally, math.log(x, base) and math.log2(x) / math.log10(x) may differ due to floating-point rounding.
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!
crates/ruff_linter/src/rules/refurb/rules/redundant_log_base.rs
Outdated
Show resolved
Hide resolved
refurb] make fix for FURB163 (redundant-log-base) unsafe if log2/log10, starred arg, or commentsrefurb] Make the fix for FURB163 unsafe for log2, log10, *args, and deleted comments
* main: (38 commits) [`pyupgrade`] Suppress `UP008` diagnostic if `super` symbol is not builtin (#18688) [pylint] Fix `PLW0128` to check assignment targets in square brackets and after asterisks (#18665) [`refurb`] Make the fix for `FURB163` unsafe for `log2`, `log10`, `*args`, and deleted comments (#18645) [ty] allow `T: Never` as subtype of `Never` (#18687) [ty] Use more parallelism when running corpus tests (#18711) [ty] Support `dataclasses.KW_ONLY` (#18677) [`ruff`] Check for non-context-manager use of `pytest.raises`, `pytest.warns`, and `pytest.deprecated_call` (`RUF061`) (#17368) Add syntax error when conversion flag does not immediately follow exclamation mark (#18706) [`flake8-pyi`] Fix `custom-typevar-for-self` with string annotations (`PYI019`) (#18311) Drop confusing second `*` from glob pattern example (#18709) [ty] Stabilize completions (#18650) [ty] Correctly label typeshed-sync PRs (#18702) Update Rust crate memchr to v2.7.5 (#18696) Update dependency react-resizable-panels to v3.0.3 (#18691) Update Rust crate clap to v4.5.40 (#18692) Update Rust crate libcst to v1.8.2 (#18695) Update Rust crate jiff to v0.2.15 (#18693) Update Rust crate libc to v0.2.173 (#18694) Update Rust crate syn to v2.0.103 (#18698) Update Rust crate toml to v0.8.23 (#18699) ...




Summary
/closes #18639
Test Plan
update snapshots