faker icon indicating copy to clipboard operation
faker copied to clipboard

pyfloat() - floats passed as min_value and max_value params TypeError: 'float' object cannot be interpreted as an integer

Open josidridolfo opened this issue 1 year ago • 1 comments

  • Faker version: 25.0.1
  • OS: Mac OS Sonoma 14.4.1

Brief summary of the issue goes here.

faker.pyfloat()'s min_value and max_value no longer accepts floats; ints work. This is despite the documentation stating that floats are valid datatypes for the min_value and max_value params.

from faker import faker
faker = Faker()
eur_val = faker.pyfloat(left_digits=10,
                            right_digits=2,
                            positive=True,
                            min_value=0.01,
                            max_value=999999999.99,
                )

Expected behavior

output should be a float between 0.01 and 999999999.99

Actual behavior

output does not include floats below 1 or above the max value.

Relevant error:

  File "/usr/src/app/contracts/management/commands/populate_contracts.py", line 42, in handle
    "currency_value": faker.pyfloat(
                      ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/faker/providers/python/__init__.py", line 133, in pyfloat
    left_number = self._safe_random_int(
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/faker/providers/python/__init__.py", line 172, in _safe_random_int
    return self.random_int(min_value, max_value - 1)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/faker/providers/__init__.py", line 316, in random_int
    return self.generator.random.randrange(min, max + 1, step)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/random.py", line 312, in randrange
    istop = _index(stop)
            ^^^^^^^^^^^^
TypeError: 'float' object cannot be interpreted as an integer

josidridolfo avatar May 02 '24 17:05 josidridolfo

I test the code and worked well as expected, on version 25.8.0, can you provide your faker version?

And in your code the f of faker should be capital.

from faker import Faker

The code I tested:

from faker import Faker

faker = Faker()
eur_val = faker.pyfloat(left_digits=10,
                            right_digits=2,
                            positive=True,
                            min_value=0.01,
                            max_value=999999999.99,
                )

print(eur_val)
#OUTPUT
#930165130.36

parsariyahi avatar Jun 10 '24 14:06 parsariyahi

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Sep 09 '24 01:09 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Sep 23 '24 01:09 github-actions[bot]