-
Notifications
You must be signed in to change notification settings - Fork 2k
FURB157 has incorrect fixes when replacing strings with integers #14204
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't workingfixesRelated to suggested fixes for violationsRelated to suggested fixes for violations
Description
The fixes for verbose-decimal-constructor (FURB157) are sometimes incorrect in Ruff 0.7.3. Two of them change behavior and one introduces a syntax error.
First, negative zero should be kept as a string. int doesn’t have signed zeroes but Decimal does.
$ cat furb157.py
from decimal import Decimal
print(Decimal("-0"))
$ python furb157.py
-0
$ ruff check --isolated --preview --select FURB157 furb157.py --fix
Found 1 error (1 fixed, 0 remaining).
$ cat furb157.py
from decimal import Decimal
print(Decimal(-0))
$ python furb157.py
0Second, the empty string should not be normalized to zero. The empty string signals an InvalidOperation, which can be trapped and converted to NaN or can cause an error. “Empty” here refers to the string after normalizing away white space and underscores.
$ cat furb157.py
from decimal import Decimal, ExtendedContext
print(Decimal("_", ExtendedContext))
print(Decimal(" "))
$ python furb157.py
NaN
Traceback (most recent call last):
File "furb157.py", line 3, in <module>
print(Decimal(" "))
^^^^^^^^^^^
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
$ ruff check --isolated --preview --select FURB157 furb157.py --fix
Found 2 errors (2 fixed, 0 remaining).
$ cat furb157.py
cat furb157.py
from decimal import Decimal, ExtendedContext
print(Decimal(0, ExtendedContext))
print(Decimal(0))
$ python furb157.py
0
0Third, decimal int literals with too many digits cause syntax errors. The minimum maximum digit count is 640. If the integer-valued string argument to Decimal has over that many digits, it should stay a string.
$ { printf 'from decimal import Decimal\nDecimal("1'; printf 0%0.s {1..640}; printf '")\n'; } >furb157.py
$ PYTHONINTMAXSTRDIGITS=640 python furb157.py
$ ruff check --isolated --preview --select FURB157 furb157.py --fix
Found 1 error (1 fixed, 0 remaining).
$ PYTHONINTMAXSTRDIGITS=640 python furb157.py 2>&1 | tail -n 1
SyntaxError: Exceeds the limit (640 digits) for integer string conversion: value has 641 digits; use sys.set_int_max_str_digits() to increase the limit - Consider hexadecimal for huge integer literals to avoid decimal conversion limits.Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingfixesRelated to suggested fixes for violationsRelated to suggested fixes for violations