Fix Faker.random_number intermittent exceptions #1074 #1089
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.
What does this change
Uses
self.random_digit_not_null()
instead ofself.random_digit()
to generate a random max length of digits when none are passed toself.random_number()
method.Also, raise more aptly worded / obvious ValueError when digits param is less than 1 and fix_len param is set to True.
What was wrong
The use of
self.random_digit()
to generate a random max length of digits in a random number gives it a 10% chance of equaling 0 and failing with a ValueError when fix_len is set to True.How this fixes it
Setting
digits = self.random_digit_not_null()
will always generate a number between 1 and 9 instead of 0 and 9, resolving the case where a user specifiesfix_len=True
and doesn't specify a digits param.Fixes #1074