-
Notifications
You must be signed in to change notification settings - Fork 309
Introduce exactness
into Decimal
validation logic
#1405
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,8 +248,8 @@ impl<'py> Input<'py> for Bound<'py, PyAny> { | |
str_as_int(self, s) | ||
} else if self.is_exact_instance_of::<PyFloat>() { | ||
float_as_int(self, self.extract::<f64>()?) | ||
} else if let Ok(decimal) = self.strict_decimal(self.py()) { | ||
decimal_as_int(self, &decimal) | ||
} else if let Ok(decimal) = self.validate_decimal(strict, self.py()) { | ||
sydney-runkle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
decimal_as_int(self, &decimal.into_inner()) | ||
} else if let Ok(float) = self.extract::<f64>() { | ||
float_as_int(self, float) | ||
} else if let Some(enum_val) = maybe_as_enum(self) { | ||
|
@@ -307,47 +307,37 @@ impl<'py> Input<'py> for Bound<'py, PyAny> { | |
Err(ValError::new(ErrorTypeDefaults::FloatType, self)) | ||
} | ||
|
||
fn strict_decimal(&self, py: Python<'py>) -> ValResult<Bound<'py, PyAny>> { | ||
fn validate_decimal(&self, strict: bool, py: Python<'py>) -> ValMatch<Bound<'py, PyAny>> { | ||
let decimal_type = get_decimal_type(py); | ||
// Fast path for existing decimal objects | ||
if self.is_exact_instance(decimal_type) { | ||
return Ok(self.to_owned()); | ||
} | ||
|
||
// Try subclasses of decimals, they will be upcast to Decimal | ||
if self.is_instance(decimal_type)? { | ||
return create_decimal(self, self); | ||
} | ||
|
||
Err(ValError::new( | ||
ErrorType::IsInstanceOf { | ||
class: decimal_type | ||
.qualname() | ||
.and_then(|name| name.extract()) | ||
.unwrap_or_else(|_| "Decimal".to_owned()), | ||
context: None, | ||
}, | ||
self, | ||
)) | ||
} | ||
|
||
fn lax_decimal(&self, py: Python<'py>) -> ValResult<Bound<'py, PyAny>> { | ||
let decimal_type = get_decimal_type(py); | ||
// Fast path for existing decimal objects | ||
if self.is_exact_instance(decimal_type) { | ||
return Ok(self.to_owned().clone()); | ||
} | ||
|
||
if self.is_instance_of::<PyString>() || (self.is_instance_of::<PyInt>() && !self.is_instance_of::<PyBool>()) { | ||
// checking isinstance for str / int / bool is fast compared to decimal / float | ||
create_decimal(self, self) | ||
Ok(ValidationMatch::exact(self.to_owned().clone())) | ||
} else if self.is_instance(decimal_type)? { | ||
// upcast subclasses to decimal | ||
return create_decimal(self, self); | ||
} else if self.is_instance_of::<PyFloat>() { | ||
create_decimal(self.str()?.as_any(), self) | ||
// Upcast subclasses to decimal | ||
create_decimal(self, self).map(ValidationMatch::strict) | ||
|
||
} else { | ||
Err(ValError::new(ErrorTypeDefaults::DecimalType, self)) | ||
if strict { | ||
return Err(ValError::new( | ||
ErrorType::IsInstanceOf { | ||
class: decimal_type | ||
.qualname() | ||
.and_then(|name| name.extract()) | ||
.unwrap_or_else(|_| "Decimal".to_owned()), | ||
context: None, | ||
}, | ||
self, | ||
)); | ||
} | ||
if self.is_instance_of::<PyString>() || (self.is_instance_of::<PyInt>() && !self.is_instance_of::<PyBool>()) | ||
{ | ||
// Checking isinstance for str / int / bool is fast compared to decimal / float | ||
create_decimal(self, self).map(ValidationMatch::lax) | ||
} else if self.is_instance_of::<PyFloat>() { | ||
create_decimal(self.str()?.as_any(), self).map(ValidationMatch::lax) | ||
} else { | ||
Err(ValError::new(ErrorTypeDefaults::DecimalType, self)) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 wonder if
Str
should be strict... we serialize decimals as strings now, correct?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.
Ah, ok! What about floats and ints, should those be strict as well?
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.
Is anything
lax
then?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 guess they're probably all strict? Ungh 😂
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.
So we don't need
lax
hahahThere 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.
For
Python
, I think they should be lax, based on existing tests...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.