Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.2k
Add validation tests for_internal/_validators.py
#10763
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This PR introduces new unit tests to enhance the test coverage by covering the following scenarios:- Added a type check for decimal validators and the corresponding unit tests.- Added unit tests to validate numeric constraints (e.g., 'gt', 'lt', etc.).- Added a unit test to validate the `Fraction` type.
codspeed-hqbot commentedNov 4, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
CodSpeed Performance ReportMerging#10763 willnot alter performanceComparing Summary
|
github-actionsbot commentedNov 4, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Coverage reportClick to see where and how coverage changed
This report was generated bypython-coverage-comment-action |
please review |
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.
Thanks for helping us to bump up our test coverage!
I've left a few recommendations. Let me know if you have any follow up inquiries :)
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
pydantic/_internal/_validators.py Outdated
if not isinstance(x, Decimal): | ||
raise TypeError(f'Expected Decimal, received {type(x).__name__}') |
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.
Do we test this anywhere?
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 ended up checking the Decimal type here instead
raiseTypeError(f'Unable to extract decimal digits info from supplied value{decimal}') |
…ts_infoSigned-off-by: tkasuz <63289889+tkasuz@users.noreply.github.com>
0bbfcd5
tof6919b5
CompareSigned-off-by: tkasuz <63289889+tkasuz@users.noreply.github.com>
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.
Other than that one question, tests look great! Thanks for your contribution!
decimal_tuple = decimal.as_tuple() | ||
if not isinstance(decimal_tuple.exponent, int): | ||
raise TypeError(f'Unable to extract decimal digits info from supplied value {decimal}') | ||
exponent = decimal_tuple.exponent | ||
num_digits = len(decimal_tuple.digits) | ||
if exponent >= 0: | ||
# A positive exponent adds that many trailing zeros | ||
# Ex: digit_tuple=(1, 2, 3), exponent=2 -> 12300 -> 0 decimal places, 5 digits | ||
num_digits += exponent | ||
decimal_places = 0 | ||
else: | ||
# If the absolute value of the negative exponent is larger than the | ||
# number of digits, then it's the same as the number of digits, | ||
# because it'll consume all the digits in digit_tuple and then | ||
# add abs(exponent) - len(digit_tuple) leading zeros after the decimal point. | ||
# Ex: digit_tuple=(1, 2, 3), exponent=-2 -> 1.23 -> 2 decimal places, 3 digits | ||
# Ex: digit_tuple=(1, 2, 3), exponent=-4 -> 0.0123 -> 4 decimal places, 4 digits | ||
decimal_places = abs(exponent) | ||
num_digits = max(num_digits, decimal_places) | ||
try: | ||
decimal_tuple = decimal.as_tuple() | ||
return decimal_places, num_digits |
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.
Can we leave this as is? WhatAttributeError
might occur here?
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.
If thedecimal
argument is not aDecimal
type, which is accepted as Any in the root function, anAttributeError
might occur because the as_tuple method may not exist
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.
Would it be better to change the argument type toAny
and add anotherassert
to check if the type isDecimal
? 🤔
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.
Makes sense, thanks for the improvement!
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.
Thanks! Great work! 🚀
0157e34
intopydantic:mainUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Change Summary
This PR introduces new unit tests to enhance the test coverage by covering the following scenarios:
Fraction
type.Confirmed that the test coverage for
_internal/_validators.py
increased from 96.43% to 100%Related issue number
#7656
Checklist
Selected Reviewer:@sydney-runkle