Summary
unnecessary-from-float (FURB164) does not consistently simplify expressions containing non-finite float strings. This is similar to #14587. A non-finite float string can having leading or trailing white space. It can also have an otiose sign: +inf, +nan, or -nan. FURB164 only recognizes strings without these extras. It should treat them the same and fix all or none of them.
$ cat >furb164.py <<'# EOF'
from decimal import Decimal
Decimal.from_float(float("\n+nan\n"))
Decimal.from_float(float("\xa0-nan\u3000"))
Decimal.from_float(float(" +inf "))
# cf.:
Decimal.from_float(float("nan"))
Decimal.from_float(float("-inf"))
# EOF
$ ruff --isolated check --preview --select FURB164 furb164.py --fix
Found 5 errors (5 fixed, 0 remaining).
$ cat furb164.py
from decimal import Decimal
Decimal(float("\n+nan\n"))
Decimal(float("\xa0-nan\u3000"))
Decimal(float(" +inf "))
# cf.:
Decimal("nan")
Decimal("-inf")
For those first three examples, the fix should produce:
Decimal("\n+nan\n")
Decimal("\xa0nan\u3000")
Decimal(" +inf ")
Note that the sign should be removed from -nan. Otherwise, the strings stay the same.
Alternatively, the removal of the float call could be considered out of scope for this rule because verbose-decimal-constructor (FURB157) already handles it. In that case, FURB164 should convert Decimal.from_float(float("nan")) (for example) to Decimal(float("nan")) instead of simplifying it to Decimal("nan").
Version
ruff 0.9.10 (0dfa810 2025-03-07)
Summary
unnecessary-from-float (FURB164) does not consistently simplify expressions containing non-finite float strings. This is similar to #14587. A non-finite float string can having leading or trailing white space. It can also have an otiose sign:
+inf,+nan, or-nan. FURB164 only recognizes strings without these extras. It should treat them the same and fix all or none of them.For those first three examples, the fix should produce:
Note that the sign should be removed from
-nan. Otherwise, the strings stay the same.Alternatively, the removal of the
floatcall could be considered out of scope for this rule because verbose-decimal-constructor (FURB157) already handles it. In that case, FURB164 should convertDecimal.from_float(float("nan"))(for example) toDecimal(float("nan"))instead of simplifying it toDecimal("nan").Version
ruff 0.9.10 (0dfa810 2025-03-07)