Skip to content

Conversation

@VascoSch92
Copy link
Contributor

@VascoSch92 VascoSch92 commented Apr 6, 2025

The PR fixes #16457 .

Specifically, FURB161 is 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.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 6, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Apr 6, 2025
@ntBre ntBre self-assigned this Apr 6, 2025
Copy link
Member

@MichaReiser MichaReiser left a 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

@dscorbett
Copy link

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.

@VascoSch92 VascoSch92 requested a review from MichaReiser April 7, 2025 19:24
@VascoSch92
Copy link
Contributor Author

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 bin method is of type int or bool

@dscorbett
Copy link

Using ResolvedPythonType::from would help catch more expressions than literals, like -1 or 1 + 2.

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

@VascoSch92
Copy link
Contributor Author

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.

Hey @ntBre
I changed to use ResolvedPythonType. Let me know if I used it in the correct way ;-)

@VascoSch92 VascoSch92 requested a review from ntBre April 18, 2025 10:33
@ntBre
Copy link
Contributor

ntBre commented Apr 18, 2025

Thanks, it looks good to me!

Let's save this for a follow-up PR like the Expr::Name idea I mentioned above, but I think we can even do better than ResolvedPythonType to handle variable assignments too, like the x = 10 case in the existing tests. There doesn't seem to be an existing is_bool function in our typing module, but we could easily add one similar to is_int:

/// Test whether the given binding can be considered an integer.
pub fn is_int(binding: &Binding, semantic: &SemanticModel) -> bool {
check_type::<IntChecker>(binding, semantic)
}

You definitely don't have to work on this, just a couple of ideas if you're interested :)

I pushed two tiny tweaks:

  • reverting a couple of lines that just moved around throughout the PR
  • adding a little more context to the fix safety docs

I'll merge as soon as CI passes!

@ntBre ntBre changed the title [refurb] Unsafe fixes in FURB161 [refurb] Mark the FURB161 fix unsafe except for integers and booleans Apr 18, 2025
@ntBre ntBre merged commit f8061e8 into astral-sh:main Apr 18, 2025
22 checks passed
@VascoSch92 VascoSch92 deleted the unsafe-fixes-FURB161 branch April 18, 2025 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FURB161 fix should be marked unsafe in some contexts

4 participants