-
-
Notifications
You must be signed in to change notification settings - Fork 2.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add validation tests for _internal/_validators.py
#10763
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 Performance ReportMerging #10763 will not alter performanceComparing Summary
|
Coverage reportClick to see where and how coverage changed
This report was generated by python-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 :)
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
pydantic/pydantic/_internal/_validators.py
Line 374 in f6919b5
raise TypeError(f'Unable to extract decimal digits info from supplied value {decimal}') |
…ts_info Signed-off-by: tkasuz <63289889+tkasuz@users.noreply.github.com>
0bbfcd5
to
f6919b5
Compare
Signed-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? What AttributeError
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 the decimal
argument is not a Decimal
type, which is accepted as Any in the root function, an AttributeError
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 to Any
and add another assert
to check if the type is Decimal
? 🤔
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! 🚀
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