Skip to content

Conversation

@danparizher
Copy link
Contributor

Summary

Fixes FURB157 false negative where Decimal("_-1") was not flagged as verbose when underscores precede the sign character. This fixes #21186.

Problem Analysis

The verbose-decimal-constructor (FURB157) rule failed to detect verbose Decimal constructors when the sign character (+ or -) was preceded by underscores. For example, Decimal("_-1") was not flagged, even though it can be simplified to Decimal(-1).

The bug occurred because the rule checked for the sign character at the start of the string before stripping leading underscores. According to Python's Decimal parser behavior (as documented in CPython's _pydecimal.py), underscores are removed before parsing the sign. The rule's logic didn't match this behavior, causing a false negative for cases like "_-1" where the underscore came before the sign.

This was a regression introduced in version 0.14.3, as these cases were correctly flagged in version 0.14.2.

Approach

The fix updates the sign extraction logic to:

  1. Strip leading underscores first (matching Python's Decimal parser behavior)
  2. Extract the sign from the underscore-stripped string
  3. Preserve the string after the sign for normalization purposes

This ensures that cases like Decimal("_-1"), Decimal("_+1"), and Decimal("_-1_000") are correctly detected and flagged. The normalization logic was also updated to use the string after the sign (without underscores) to avoid double signs in the replacement output.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 1, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added bug Something isn't working rule Implementing or modifying a lint rule labels Nov 3, 2025
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this makes sense to me!

@ntBre ntBre merged commit 47e41ac into astral-sh:main Nov 4, 2025
39 checks passed
@danparizher danparizher deleted the fix-21186 branch November 4, 2025 16:13
carljm added a commit to MatthewMckee4/ruff that referenced this pull request Nov 6, 2025
* main: (188 commits)
  [ty] Discover site-packages from the environment that ty is installed in (astral-sh#21286)
  [ty] Make special cases for `UnionType` slightly narrower (astral-sh#21276)
  Require ignore 0.4.24 in `Cargo.toml` (astral-sh#21292)
  [ty] Favour imported symbols over builtin symbols (astral-sh#21285)
  docs: revise Ruff setup instructions for Zed editor (astral-sh#20935)
  [ty] Update salsa (astral-sh#21281)
  [syntax-error]: no binding for nonlocal  PLE0117 as a semantic syntax error (astral-sh#21032)
  [ty] Constraining a typevar with itself (possibly via union or intersection) (astral-sh#21273)
  [`ruff`] Fix false positives on starred arguments (`RUF057`) (astral-sh#21256)
  [ty] Simplify unions containing multiple type variables during inference (astral-sh#21275)
  [ty] Add `ty_server::Db` trait (astral-sh#21241)
  [ty] Refactor `Range` to/from `TextRange` conversion as prep for notebook support (astral-sh#21230)
  [ty] Fix playground crash when file name includes path separator (astral-sh#21151)
  [`refurb`] Fix false negative for underscores before sign in `Decimal` constructor (`FURB157`) (astral-sh#21190)
  [ty] Allow values of type `None` in type expressions (astral-sh#21263)
  Run codspeed benchmarks with `profiling` profile (astral-sh#21261)
  [ty] Update expected diagnostic count in benchmarks (astral-sh#21269)
  Avoid extra parentheses for long `match` patterns with `as` captures (astral-sh#21176)
  [ty] Update salsa (astral-sh#21265)
  [ty] `dict` is not assignable to `TypedDict` (astral-sh#21238)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FURB157 misses decimal.Decimal("_-1")

2 participants