1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
from dataclasses import dataclass
from mypy.nodes import CallExpr, MemberExpr, RefExpr
from refurb.checks.common import stringify
from refurb.error import Error
@dataclass
class ErrorInfo(Error):
"""
When constructing a Fraction or Decimal using a float, don't use the
`from_float()` or `from_decimal()` class methods: Just use the more concise
`Fraction()` and `Decimal()` class constructors instead.
Bad:
```
ratio = Fraction.from_float(1.2)
score = Decimal.from_float(98.0)
```
Good:
```
ratio = Fraction(1.2)
score = Decimal(98.0)
```
"""
name = "no-from-float"
code = 164
categories = ("decimal", "fractions", "readability")
KNOWN_FUNCS = {
"_decimal.Decimal.from_float",
"fractions.Fraction.from_float",
"fractions.Fraction.from_decimal",
}
def check(node: CallExpr, errors: list[Error]) -> None:
match node:
case CallExpr(
callee=MemberExpr(
expr=RefExpr(fullname="_decimal.Decimal" | "fractions.Fraction") as ref,
name="from_float" | "from_decimal" as ctor,
),
args=[arg],
):
if f"{ref.fullname}.{ctor}" not in KNOWN_FUNCS:
return
base = stringify(ref)
arg = stringify(arg) # type: ignore
old = f"{base}.{ctor}({arg})"
new = f"{base}({arg})"
errors.append(ErrorInfo.from_node(node, f"Replace `{old}` with `{new}`"))
|