Skip to content

Commit 8eeca02

Browse files
authored
[ruff] FURB164 Replace -nan with nan when using the value to construct Decimal (#20391)
## Summary Fixes #19699 Normalize `Decimal(float("-nan"))` to `Decimal("nan")`. The same handling is implemented in: https://github.com/astral-sh/ruff/blob/c3e873dd827b9ec8dd49cff723089852ce2301e1/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs#L165
1 parent c94ddb5 commit 8eeca02

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

crates/ruff_linter/src/rules/refurb/rules/unnecessary_from_float.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,19 @@ fn handle_non_finite_float_special_case(
293293
return None;
294294
};
295295
let normalized = as_non_finite_float_string_literal(float_arg)?;
296-
let replacement_text = format!(r#"{constructor_name}("{normalized}")"#);
296+
let replacement_text = format!(
297+
r#"{constructor_name}("{}")"#,
298+
// `Decimal.from_float(float(" -nan")) == Decimal("nan")`
299+
if normalized == "-nan" {
300+
// Here we do not attempt to remove just the '-' character.
301+
// It may have been encoded (e.g. as '\N{hyphen-minus}')
302+
// in the original source slice, and the added complexity
303+
// does not make sense for this edge case.
304+
"nan"
305+
} else {
306+
normalized
307+
}
308+
);
297309
Some(Edit::range_replacement(replacement_text, call.range()))
298310
}
299311

crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB164_FURB164.py.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ help: Replace with `Decimal` constructor
363363
21 | _ = Decimal.from_float(float("-Infinity"))
364364
22 | _ = Decimal.from_float(float("nan"))
365365
- _ = Decimal.from_float(float("-NaN "))
366-
23 + _ = Decimal("-nan")
366+
23 + _ = Decimal("nan")
367367
24 | _ = Decimal.from_float(float(" \n+nan \t"))
368368
25 | _ = Decimal.from_float(float(" iNf \n\t "))
369369
26 | _ = Decimal.from_float(float("  -inF\n \t"))
@@ -655,7 +655,7 @@ help: Replace with `Decimal` constructor
655655
62 |
656656
63 | # Cases with non-finite floats - should produce safe fixes
657657
- _ = Decimal.from_float(float("-nan"))
658-
64 + _ = Decimal("-nan")
658+
64 + _ = Decimal("nan")
659659
65 | _ = Decimal.from_float(float("\x2dnan"))
660660
66 | _ = Decimal.from_float(float("\N{HYPHEN-MINUS}nan"))
661661

@@ -673,7 +673,7 @@ help: Replace with `Decimal` constructor
673673
63 | # Cases with non-finite floats - should produce safe fixes
674674
64 | _ = Decimal.from_float(float("-nan"))
675675
- _ = Decimal.from_float(float("\x2dnan"))
676-
65 + _ = Decimal("-nan")
676+
65 + _ = Decimal("nan")
677677
66 | _ = Decimal.from_float(float("\N{HYPHEN-MINUS}nan"))
678678

679679
FURB164 [*] Verbose method `from_float` in `Decimal` construction
@@ -689,4 +689,4 @@ help: Replace with `Decimal` constructor
689689
64 | _ = Decimal.from_float(float("-nan"))
690690
65 | _ = Decimal.from_float(float("\x2dnan"))
691691
- _ = Decimal.from_float(float("\N{HYPHEN-MINUS}nan"))
692-
66 + _ = Decimal("-nan")
692+
66 + _ = Decimal("nan")

0 commit comments

Comments
 (0)