[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:
Dylan 2024-11-05 13:33:04 -06:00 committed by GitHub
parent 239cbc6f33
commit 2b76fa8fa1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 96 additions and 5 deletions

View file

@ -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("١٢٣")