-
Notifications
You must be signed in to change notification settings - Fork 247
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
avoid coercing int subclasses to floats #914
Conversation
CodSpeed Performance ReportMerging #914 will improve performances by 12.32%Comparing Summary
Benchmarks breakdown
|
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #914 +/- ##
=======================================
Coverage 93.78% 93.79%
=======================================
Files 105 105
Lines 15331 15335 +4
Branches 25 25
=======================================
+ Hits 14378 14383 +5
+ Misses 947 946 -1
Partials 6 6
Continue to review full report in Codecov by Sentry.
|
please review. |
@@ -303,7 +303,9 @@ impl<'a> Input<'a> for PyAny { | |||
if PyBool::is_exact_type_of(self) { | |||
Err(ValError::new(ErrorTypeDefaults::IntType, self)) | |||
} else { | |||
Ok(EitherInt::Py(self)) | |||
// force to an int to upcast to a pure python int |
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.
we now do the same in "strict" mode to match lax mode
i would definitely consider reverting this change - substituting consistency between lax and strict for a smaller change of breaking changes.
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.
Personally I think we want to go the other way and not upcast in both, I already can't remember why we wanted the upcast in lax mode.
Regardless of to upcast or not upcast, I think both modes should behave the same, so we should change one or the other.
Also you might want to consider using BigInt
here if the i64
extraction fails.
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.
Just for my understanding, this means that now if isinstance(obj, int)
is true then we essentially do self.extract::<i64>()
which upcasts into an exact int
type eventually. Previously subclasses of ints would fail to validate as an int and somehow end up as a float?
well, end up being coerced to a float, then back to an int - hence with large numbers precision was lost. Otherwise yes. |
@roman-right are you happy with this - particularly the treatment of subclasses? |
Also related: #913 |
Hi @samuelcolvin ! It works well for my case. Thank you. |
Change Summary
Fix validation if int subclasses.
With this we now upcast sub-types of
int
toint
in both strict and lax mode, the rationale is as follows:extract::<f64>()
logic which we're now avoiding)Related issue number
fix pydantic/pydantic#7201
fix #913
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @adriangb