Skip to content

FURB164 fixes should be safe in a few cases #21257

@dscorbett

Description

@dscorbett

Summary

Some fixes of unnecessary-from-float (FURB164) are marked unsafe but are actually safe: when the argument is specified as a keyword argument but would otherwise be marked safe, and when the argument to Fraction.from_decimal is a Decimal. Example:

$ cat >furb164.py <<'# EOF'
from decimal import Decimal
from fractions import Fraction
print(Fraction.from_float(f=4.2))
print(Fraction.from_decimal(dec=4))
print(Fraction.from_decimal(Decimal("4.2")))
print(Fraction.from_decimal(dec=Decimal("4.2")))
# EOF

$ python furb164.py
4728779608739021/1125899906842624
4
21/5
21/5

$ ruff --isolated check furb164.py --select FURB164 --preview --output-format concise -q
furb164.py:3:7: FURB164 Verbose method `from_float` in `Fraction` construction
furb164.py:4:7: FURB164 Verbose method `from_decimal` in `Fraction` construction
furb164.py:5:7: FURB164 Verbose method `from_decimal` in `Fraction` construction
furb164.py:6:7: FURB164 Verbose method `from_decimal` in `Fraction` construction

$ ruff --isolated check furb164.py --select FURB164 --preview --unsafe-fixes --fix
Found 4 errors (4 fixed, 0 remaining).

$ cat furb164.py
from decimal import Decimal
from fractions import Fraction
print(Fraction(4.2))
print(Fraction(4))
print(Fraction(Decimal("4.2")))
print(Fraction(Decimal("4.2")))

$ python furb164.py
4728779608739021/1125899906842624
4
21/5
21/5

Version

ruff 0.14.3 (8737a2d 2025-10-30)

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated to suggested fixes for violationspreviewRelated to preview mode features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions