Skip to content

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

Merged
merged 3 commits into from
Nov 16, 2024

Conversation

tkasuz
Copy link
Contributor

@tkasuz tkasuz commented Nov 4, 2024

Change Summary

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.

Confirmed that the test coverage for _internal/_validators.py increased from 96.43% to 100%

Related issue number

#7656

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @sydney-runkle

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.
@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Nov 4, 2024
Copy link

codspeed-hq bot commented Nov 4, 2024

CodSpeed Performance Report

Merging #10763 will not alter performance

Comparing tkasuz:increase_types_test_coverage (57783eb) with main (bcfd413)

Summary

✅ 44 untouched benchmarks

Copy link
Contributor

github-actions bot commented Nov 4, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic/_internal
  _validators.py
Project Total  

This report was generated by python-coverage-comment-action

@tkasuz
Copy link
Contributor Author

tkasuz commented Nov 4, 2024

please review

Copy link
Contributor

@sydney-runkle sydney-runkle left a 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 :)

Comment on lines 391 to 392
if not isinstance(x, Decimal):
raise TypeError(f'Expected Decimal, received {type(x).__name__}')
Copy link
Contributor

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?

Copy link
Contributor Author

@tkasuz tkasuz Nov 15, 2024

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

raise TypeError(f'Unable to extract decimal digits info from supplied value {decimal}')

@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Nov 14, 2024
@pydantic-hooky pydantic-hooky bot assigned tkasuz and unassigned sydney-runkle Nov 14, 2024
…ts_info

Signed-off-by: tkasuz <63289889+tkasuz@users.noreply.github.com>
@tkasuz tkasuz force-pushed the increase_types_test_coverage branch from 0bbfcd5 to f6919b5 Compare November 15, 2024 04:31
@tkasuz tkasuz requested a review from sydney-runkle November 15, 2024 04:44
Signed-off-by: tkasuz <63289889+tkasuz@users.noreply.github.com>
Copy link
Contributor

@sydney-runkle sydney-runkle left a 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!

Comment on lines -349 to -370
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
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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? 🤔

Copy link
Contributor

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!

Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Great work! 🚀

@sydney-runkle sydney-runkle merged commit 0157e34 into pydantic:main Nov 16, 2024
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting author revision awaiting changes from the PR author relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants