From cfeb1224e6da6cb527ad09034aafdffe48fd2212 Mon Sep 17 00:00:00 2001 From: Akhil Narang Date: Wed, 4 Dec 2024 12:54:46 +0530 Subject: [PATCH] chore(flt): explicitly handle NoneTypes These were implicitly handled due to the catch-all 0.0 return, but type checkers break here, and a lot of places depend on this behaviour Signed-off-by: Akhil Narang --- frappe/utils/data.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/frappe/utils/data.py b/frappe/utils/data.py index 4dc2b6a55d..46c643caeb 100644 --- a/frappe/utils/data.py +++ b/frappe/utils/data.py @@ -1078,7 +1078,14 @@ def flt(s: NumericType | str, precision: int | None = None) -> float: ... -def flt(s: NumericType | str, precision: int | None = None, rounding_method: str | None = None) -> float: +@typing.overload +def flt(s: None) -> Literal[0.0]: + ... + + +def flt( + s: NumericType | str | None, precision: int | None = None, rounding_method: str | None = None +) -> float: """Convert to float (ignoring commas in string). :param s: Number in string or other numeric format. @@ -1097,7 +1104,13 @@ def flt(s: NumericType | str, precision: int | None = None, rounding_method: str 10500.57 >>> flt("a") 0.0 + >>> flt(None) + 0.0 """ + + if s is None: + return 0.0 + if isinstance(s, str): s = s.replace(",", "")