fix: cint -> avoid precision loss if already integer (#25735)
This commit is contained in:
parent
a6732a0e12
commit
ea5e1b61ad
2 changed files with 15 additions and 2 deletions
|
|
@ -55,6 +55,7 @@ from frappe.utils.data import (
|
|||
add_to_date,
|
||||
add_years,
|
||||
cast,
|
||||
cint,
|
||||
cstr,
|
||||
duration_to_seconds,
|
||||
expand_relative_urls,
|
||||
|
|
@ -1250,6 +1251,15 @@ class TestRounding(FrappeTestCase):
|
|||
def test_default_rounding(self):
|
||||
self.assertEqual(frappe.get_system_settings("rounding_method"), "Banker's Rounding")
|
||||
|
||||
@given(
|
||||
st.floats(min_value=-(2**32) - 1, max_value=2**32 + 1),
|
||||
st.integers(min_value=-(2**63) - 1, max_value=2**63 + 1),
|
||||
)
|
||||
def test_cint(self, floating_point, integer):
|
||||
self.assertEqual(cint(integer), integer)
|
||||
self.assertEqual(cint(str(integer)), integer)
|
||||
self.assertEqual(cint(str(floating_point)), int(floating_point))
|
||||
|
||||
|
||||
class TestArgumentTypingValidations(FrappeTestCase):
|
||||
def test_validate_argument_types(self):
|
||||
|
|
|
|||
|
|
@ -1122,9 +1122,12 @@ def cint(s: NumericType | str, default: int = 0) -> int:
|
|||
|
||||
"""
|
||||
try:
|
||||
return int(float(s))
|
||||
return int(s)
|
||||
except Exception:
|
||||
return default
|
||||
try:
|
||||
return int(float(s))
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
|
||||
def floor(s: NumericType | str) -> int:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue