-
Notifications
You must be signed in to change notification settings - Fork 293
Properly coerce fractions as int #1757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
src/input/input_python.rs
Outdated
#[cfg(Py_3_11)] | ||
let as_int = self.call_method0("__int__"); | ||
#[cfg(not(Py_3_11))] | ||
let as_int = self.call_method0("__trunc__"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if there's a better way: maybe pyo3 can do the coercion directly? I've tried downcasting with no success.
CodSpeed Performance ReportMerging #1757 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think this needs to check the denominator is 1?
@@ -132,6 +133,7 @@ def test_int_py_and_json(py_and_json: PyAndJson, input_value, expected): | |||
(-i64_max + 1, -i64_max + 1), | |||
(i64_max * 2, i64_max * 2), | |||
(-i64_max * 2, -i64_max * 2), | |||
(Fraction(10_935_244_710_974_505), 10_935_244_710_974_505), # https://github.com/pydantic/pydantic/issues/12063 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also test for cases when the fraction is not an exact integer, I think in those cases we should fail int validation similar to floats and decimals?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM 👍
#[cfg(Py_3_12)] | ||
let is_integer = self.call_method0("is_integer")?.extract::<bool>()?; | ||
#[cfg(not(Py_3_12))] | ||
let is_integer = self.getattr("denominator")?.extract::<i64>().map_or(false, |d| d == 1); | ||
|
||
if is_integer { | ||
#[cfg(Py_3_11)] | ||
let as_int = self.call_method0("__int__"); | ||
#[cfg(not(Py_3_11))] | ||
let as_int = self.call_method0("__trunc__"); | ||
match as_int { | ||
Ok(i) => Ok(EitherInt::Py(i.as_any().to_owned())), | ||
Err(_) => break 'lax, | ||
} | ||
} else { | ||
Err(ValError::new(ErrorTypeDefaults::IntFromFloat, self)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be nice to have this as a fraction_as_int
method similar to the other components here.
Change Summary
Fixes pydantic/pydantic#12063.
Related issue number
Checklist
pydantic-core
(except for expected changes)