faker icon indicating copy to clipboard operation
faker copied to clipboard

Pyfloat raises TypeError for None min or max value

Open HugoJP1 opened this issue 1 year ago • 5 comments

  • Faker version: 19.3.0
  • OS: Mac OS 12.2.1 and Ubuntu 22.04.3
  • Python 3.11

When calling pyfloat with a max_value of None and a min_value, then the method sometimes raises unsupported operand type(s) for -: 'NoneType' and 'float'

Steps to reproduce

Run the following code

from faker import Faker


for i in range(3000):
    try:
        Faker().pyfloat(
                    left_digits=5,
                    right_digits=2,
                    min_value=99884.7,
                    positive=True,
                    max_value=None,
                )
    except Exception as e:
        print(e)
        print(i)

Expected behavior

There should be no exception printed.

Actual behavior

unsupported operand type(s) for -: 'NoneType' and 'float'
17
unsupported operand type(s) for -: 'NoneType' and 'float'
155
unsupported operand type(s) for -: 'NoneType' and 'float'
175
unsupported operand type(s) for -: 'NoneType' and 'float'
510
unsupported operand type(s) for -: 'NoneType' and 'float'
541
unsupported operand type(s) for -: 'NoneType' and 'float'
943
unsupported operand type(s) for -: 'NoneType' and 'float'
1005
unsupported operand type(s) for -: 'NoneType' and 'float'
1243
unsupported operand type(s) for -: 'NoneType' and 'float'
1255
unsupported operand type(s) for -: 'NoneType' and 'float'
1399
unsupported operand type(s) for -: 'NoneType' and 'float'
1677
unsupported operand type(s) for -: 'NoneType' and 'float'
1854
unsupported operand type(s) for -: 'NoneType' and 'float'
2036
unsupported operand type(s) for -: 'NoneType' and 'float'
2247
unsupported operand type(s) for -: 'NoneType' and 'float'
2448
unsupported operand type(s) for -: 'NoneType' and 'float'
2578
unsupported operand type(s) for -: 'NoneType' and 'float'
2812
unsupported operand type(s) for -: 'NoneType' and 'float'
2826

Process finished with exit code 0

I dug into the code a little and it looks to me like the issue with the code is here:

# It's possible for the result to end up > than max_value or < than min_value
        # When this happens we introduce some variance so we're not always the exactly the min_value or max_value.
        # Which can happen a lot depending on the difference of the values.
        # Ensure the variance is bound by the difference between the max and min
        if max_value is not None:
            if result > max_value:
                result = result - (result - max_value + self.generator.random.uniform(0, max_value - min_value))
        if min_value is not None:
            if result < min_value:
                result = result + (min_value - result + self.generator.random.uniform(0, max_value - min_value))

Specifically the self.generator.random.uniform(0, max_value - min_value) as the code is not handling the case where max_value or min_value is None. If it is None it should probably be handled in an extra if clause.

HugoJP1 avatar Sep 07 '23 14:09 HugoJP1

Thank you for the report! Do you have time to work on a Pull Request?

fcurella avatar Sep 07 '23 15:09 fcurella

Hi @fcurella I have attempted a solution and put up a PR: https://github.com/joke2k/faker/pull/1908

HugoJP1 avatar Sep 08 '23 17:09 HugoJP1

@HugoJP1 Unfortunately, I had to revert your changes because the caused too many regressions. See #1912, #1915, and #1926

fcurella avatar Oct 11 '23 18:10 fcurella

Hi @fcurella, can you assign this to me please. I would like to take this up

mukulbindal avatar Jan 08 '24 19:01 mukulbindal

No need to assign anyone here - just open a corresponding PR with your proposed fix/enhancement.

stefan6419846 avatar Jan 08 '24 19:01 stefan6419846

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

github-actions[bot] avatar Apr 08 '24 01:04 github-actions[bot]

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

github-actions[bot] avatar Apr 22 '24 01:04 github-actions[bot]