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 python validators for decimal constraints (max_digits
anddecimal_places
)#10506
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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
509fbbe
support for max digits and decimal places
sydney-runklec45b8b6
test and docs
sydney-runkle749f4d0
adding tests and docstring
sydney-runkle75ba3db
a few inline examples
sydney-runkle543fe18
fix examples
sydney-runkle2faa3dc
remove unnecessary normalization
sydney-runkle62c0dac
Revert "remove unnecessary normalization"
sydney-runkle4516d38
remove normalization arg
sydney-runkle2a129ac
fix test
sydney-runklea130fd1
Update pydantic/_internal/_validators.py
sydney-runklef8d1203
apply suggestions from code review
sydney-runklee8a0063
more PR feedback
sydney-runkle9adef0c
raise helpful type err
sydney-runkleFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
a few inline examples
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit75ba3dbbe160cbd055ba33878c52f18e8b350c19
There are no files selected for viewing
12 changes: 7 additions & 5 deletionspydantic/_internal/_validators.py
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 |
---|---|---|
@@ -361,14 +361,16 @@ def _extract_decimal_digits_info(decimal: Decimal, normalized: bool) -> tuple[in | ||
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 | ||
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 | ||
sydney-runkle marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
decimal_places = abs(exponent) | ||
digits = max(digits, decimal_places) | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.