mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
[refurb] Parse more exotic decimal strings in verbose-decimal-constructor (FURB157)
(#14098)
FURB157 suggests replacing expressions like `Decimal("123")` with `Decimal(123)`. This PR extends the rule to cover cases where the input string to `Decimal` can be easily transformed into an integer literal. For example: ```python Decimal("1__000") # fix: `Decimal(1000)` ``` Note: we do not implement the full decimal parsing logic from CPython on the grounds that certain acceptable string inputs to the `Decimal` constructor may be presumed purposeful on the part of the developer. For example, as in the linked issue, `Decimal("١٢٣")` is valid and equal to `Decimal(123)`, but we do not suggest a replacement in this case. Closes #13807
This commit is contained in:
parent
239cbc6f33
commit
2b76fa8fa1
3 changed files with 96 additions and 5 deletions
|
@ -15,3 +15,23 @@ decimal.Decimal("0")
|
|||
Decimal(0)
|
||||
Decimal("Infinity")
|
||||
decimal.Decimal(0)
|
||||
|
||||
# Handle Python's Decimal parsing
|
||||
# See https://github.com/astral-sh/ruff/issues/13807
|
||||
|
||||
# Errors
|
||||
Decimal("1_000")
|
||||
Decimal("__1____000")
|
||||
|
||||
# Ok
|
||||
Decimal("2e-4")
|
||||
Decimal("2E-4")
|
||||
Decimal("_1.234__")
|
||||
Decimal("2e4")
|
||||
Decimal("2e+4")
|
||||
Decimal("2E4")
|
||||
Decimal("1.2")
|
||||
# Ok: even though this is equal to `Decimal(123)`,
|
||||
# we assume that a developer would
|
||||
# only write it this way if they meant to.
|
||||
Decimal("١٢٣")
|
Loading…
Add table
Add a link
Reference in a new issue