Skip to content
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

Faker.random_number raises exception randomly #1074

Closed
bontaurean opened this issue Dec 8, 2019 · 2 comments · Fixed by #1089
Closed

Faker.random_number raises exception randomly #1074

bontaurean opened this issue Dec 8, 2019 · 2 comments · Fixed by #1089

Comments

@bontaurean
Copy link

  • Faker version: 3.0.0

I advise you to document methods parameters and add some kind of validations.

Actual behavior

>>> fake.random_number(fix_len=True)
44805
>>> fake.random_number(fix_len=True)
9543
>>> fake.random_number(fix_len=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
  File "...site-packages/faker/providers/__init__.py", line 157, in random_number
    pow(10, digits - 1), pow(10, digits) - 1)
  File "/usr/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/usr/lib/python3.7/random.py", line 186, in randrange
    raise ValueError("non-integer arg 1 for randrange()")
ValueError: non-integer arg 1 for randrange()
@malefice
Copy link
Contributor

malefice commented Dec 9, 2019

@bontaurean if the docs are insufficient, the next thing you should try is to check the source. It is open source after all, but admittedly, the docs can use some work, and not all methods have docstrings, but this one does.

Anyway, this happens when random_digit()returns a 0 in this line. Basically, each call to this method has a 10% chance of failing. This should be easy to fix, so it is a good first issue for aspiring contributors. Feel free to submit a PR with a fix and better tests if you wish to take the initiative. I will only tackle this if there are no takers for quite some time.

@fcurella regarding docs, should we take advantage of sphinx's autodoc, write docstrings for existing provider methods, and require good docstrings for all provider methods moving forward?

@fcurella
Copy link
Collaborator

fcurella commented Dec 9, 2019

@malefice I"m not sure how hard would it be to integrate autodoc, since the providers' docs are already autogenerated by our custom documentor.py. But we should look into that.

fcurella pushed a commit that referenced this issue Jan 14, 2020
* ensure max number of digits generated in random_number is > -1 when param is not passed and > 0 when fix_len is True

* add additional tests for random_number functionality
IlfirinPL pushed a commit to IlfirinPL/faker that referenced this issue Apr 9, 2021
)

* ensure max number of digits generated in random_number is > -1 when param is not passed and > 0 when fix_len is True

* add additional tests for random_number functionality
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants