astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
33.23k stars 1.11k forks source link

FURB157 has false negatives for non-finite float strings #14587

Open dscorbett opened 4 days ago

dscorbett commented 4 days ago

verbose-decimal-constructor (FURB157) has false negatives in Ruff 0.8.0 for some non-finite float strings. It recognizes the following strings case-insensitively in expressions like Decimal(float("inf")), but no others:

https://github.com/astral-sh/ruff/blob/a90e404c3f010446ab8c18b4793c78834eeb65b7/crates/ruff_linter/src/rules/refurb/rules/verbose_decimal_constructor.rs#L157

float strips white space. For example, Decimal(float(" nan ")) should be simplified to Decimal(" nan "). It could be further normalized to Decimal("NaN"), but FURB157 does not currently normalize other float strings, so I think it should keep the string argument unchanged.

float allows both signs for all non-finite values. Decimal(float("+inf")), Decimal(float("+infinity")), Decimal(float("+nan")), and Decimal(float("-nan")) should be simplified. FURB157 should not normalize these string arguments either, except for the last one. Decimal(float("-nan")) is equivalent to Decimal("nan"), not Decimal("-nan"), so for an argument of "-nan", the sign character should be deleted.

dylwil3 commented 3 days ago

Decimals strike again! Thanks for finding this, we'll get it fixed 😄