Skip to content

Conversation

@chirizxc
Copy link
Contributor

@chirizxc chirizxc commented Jun 12, 2025

Summary

/closes #18639

Test Plan

update snapshots

@github-actions
Copy link
Contributor

github-actions bot commented Jun 12, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@chirizxc
Copy link
Contributor Author

chirizxc commented Jun 12, 2025

do we have to update doc as well? because examples from the ussie and the doc are different

i mean
изображение
изображение

@chirizxc
Copy link
Contributor Author

although it should only work for float

false
}

fn is_float(expr: &Expr) -> bool {
Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

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

There's this:

/// 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.3219280948873626

and I think we're already checking for number literals.

@chirizxc
Copy link
Contributor Author

chirizxc commented Jun 12, 2025

there's also a question about ruff_python_ast/src/generated.rs file
изображение

rustrover writes that crate:: is not needed in 834 places, should the crates/ruff_python_ast/generate.py itself be corrected?

Copy link
Contributor

@ntBre ntBre left a 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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's this:

/// 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.3219280948873626

and 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(_))
Copy link
Contributor

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.

@ntBre
Copy link
Contributor

ntBre commented Jun 12, 2025

there's also a question about ruff_python_ast/src/generated.rs file изображение

rustrover writes that crate:: is not needed in 834 places, should the crates/ruff_python_ast/generate.py itself be corrected?

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!

Copy link
Contributor

@ntBre ntBre left a 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.

Comment on lines 41 to 43
/// 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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@MichaReiser MichaReiser added the fixes Related to suggested fixes for violations label Jun 13, 2025
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks!

@ntBre ntBre changed the title [refurb] make fix for FURB163 (redundant-log-base) unsafe if log2/log10, starred arg, or comments [refurb] Make the fix for FURB163 unsafe for log2, log10, *args, and deleted comments Jun 16, 2025
@ntBre ntBre enabled auto-merge (squash) June 16, 2025 18:11
@ntBre ntBre merged commit 83b0cde into astral-sh:main Jun 16, 2025
30 checks passed
@chirizxc chirizxc deleted the fix/furb163 branch June 16, 2025 18:17
dcreager added a commit that referenced this pull request Jun 16, 2025
* 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)
  ...
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.

FURB163 fix should be unsafe when it changes behavior or deletes comments

3 participants