[refurb] Fix false positive for float and complex numbers in FURB116 (#17661)

This commit is contained in:
Victor Hugo Gomes 2025-05-03 10:59:46 -03:00 committed by GitHub
parent b7d0b3f9e5
commit 097af060c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View file

@ -17,3 +17,8 @@ print(bin(int(f"{num}"))[2:]) # FURB116 (no autofix)
## invalid
print(oct(0o1337)[1:])
print(hex(0x1337)[3:])
# https://github.com/astral-sh/ruff/issues/16472
# float and complex numbers should be ignored
print(bin(1.0)[2:])
print(bin(3.14j)[2:])

View file

@ -1,6 +1,6 @@
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::{self as ast, Expr, ExprCall};
use ruff_python_ast::{self as ast, Expr, ExprCall, Number};
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
@ -110,6 +110,17 @@ pub(crate) fn fstring_number_format(checker: &Checker, subscript: &ast::ExprSubs
return;
};
// float and complex numbers are false positives, ignore them.
if matches!(
arg,
Expr::NumberLiteral(ast::ExprNumberLiteral {
value: Number::Float(_) | Number::Complex { .. },
..
})
) {
return;
}
// Generate a replacement, if possible.
let replacement = if matches!(
arg,