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

RUF032 fix can produce strings that are invalid for Decimal #13258

Closed
dscorbett opened this issue Sep 5, 2024 · 0 comments · Fixed by #13275
Closed

RUF032 fix can produce strings that are invalid for Decimal #13258

dscorbett opened this issue Sep 5, 2024 · 0 comments · Fixed by #13275
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome preview Related to preview mode features

Comments

@dscorbett
Copy link

RUF032 produces an invalid string argument when the existing argument is a float with multiple unary operators. Decimal’s string parser only accepts one sign character.

$ ruff --version
ruff 0.6.4
$ cat ruf032.py
from decimal import Decimal
Decimal(++0.3)
$ ruff check --isolated --preview --select RUF032 ruf032.py --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).
$ python ruf032.py 2>&1 | tail -n 2
    Decimal("++0.3")
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]

RUF032 also stringifies the argument if it is a float with the ~ unary operator. Applying ~ to a float raises a TypeError and should never happen, but if someone does write that, it is probably not helpful to convert it to a string, which makes it raise a different error that the surrounding code might not be expecting.

$ cat ruf032_error.py
from decimal import Decimal
Decimal(~1.0)
$ python ruf032_error.py 2>&1 | tail -n 3
    Decimal(~1.0)
            ^^^^
TypeError: bad operand type for unary ~: 'float'
$ ruff check --isolated --preview --select RUF032 ruf032_error.py --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).
$ python ruf032_error.py 2>&1 | tail -n 2
    Decimal("~1.0")
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
@dhruvmanila dhruvmanila added fixes Related to suggested fixes for violations needs-triage labels Sep 6, 2024
@MichaReiser MichaReiser added bug Something isn't working help wanted Contributions especially welcome preview Related to preview mode features and removed needs-triage labels Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome preview Related to preview mode features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants