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

Pyfloat raises TypeError for None min or max value #1907

Closed
HugoJP1 opened this issue Sep 7, 2023 · 7 comments · Fixed by #1908
Closed

Pyfloat raises TypeError for None min or max value #1907

HugoJP1 opened this issue Sep 7, 2023 · 7 comments · Fixed by #1908

Comments

@HugoJP1
Copy link
Contributor

HugoJP1 commented Sep 7, 2023

  • 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.

@fcurella
Copy link
Collaborator

fcurella commented Sep 7, 2023

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

@HugoJP1
Copy link
Contributor Author

HugoJP1 commented Sep 8, 2023

Hi @fcurella I have attempted a solution and put up a PR: #1908

@fcurella
Copy link
Collaborator

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

@mukulbindal
Copy link

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

@stefan6419846
Copy link
Contributor

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

Copy link

github-actions bot commented Apr 8, 2024

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

@github-actions github-actions bot added the stale label Apr 8, 2024
Copy link

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2024
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.

4 participants