fix: cint -> avoid precision loss if already integer (#25735)

This commit is contained in:
Ankush Menat 2024-03-29 22:31:53 +05:30 committed by GitHub
parent a6732a0e12
commit ea5e1b61ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View file

@ -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):

View file

@ -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: