CODE VERIFIED

Ruff 0.17.5 Fixes Source Boundaries When Autofixes Add or Remove Keyword Arguments With Parenthesized Values

Ruff 0.17.5 fixes autofix boundaries for parenthesized keyword values. The merged diff shows correct insertion, removal, and preserved arguments.

Nov 18, 2026, 6:25 am PST · 7 min read
Ruff 0.17.5 keyword editing illustrated with a Python call containing a parenthesized value.

Ruff 0.17.5 fixes a small source-coordinate mistake with a visible consequence: an autofix could edit inside a keyword argument when it needed to operate on the whole argument. The release, published November 12, 2026, includes the correction from merged pull request #30189. Its regression snapshots demonstrate adding an argument before a parenthesized keyword value and removing parenthesized keyword arguments while retaining a following argument.

Quick Answer

The change corrects the boundaries used by Ruff’s shared add_argument and remove_argument helpers. In Python source such as category=(DeprecationWarning), the keyword argument starts at category. The parenthesized value occupies a smaller region after the equals sign. Those positions are not interchangeable when constructing a text edit.

For insertion, Ruff now uses the first existing keyword argument’s own start position. For removal, it uses the whole keyword argument’s range. Positional expressions retain separate handling that looks for enclosing parentheses. The merged snapshots show the corrected output for the B028 and UP022 rules; they do not establish coverage of every autofix or every possible parenthesized expression.

In Plain English

An autofix needs both a semantic decision and an exact place to change the source. Deciding to add stacklevel=2 is one task. Finding a position where that text can be inserted without breaking an existing name=value pair is another.

Consider the official B028 fixture:

warnings.warn("test", category=(DeprecationWarning))

The call has a positional message followed by a keyword argument. The parentheses belong to the keyword’s value. They do not move the start of the complete keyword argument away from its name. Inserting a new argument at a boundary derived from the parenthesized value risks splitting the existing argument after its name and equals sign.

Removal has the complementary requirement. Removing a keyword argument must remove its name, equals sign, and value together. Selecting only the parenthesized value leaves part of the keyword behind. The surrounding argument list also matters: the edit must account for separators while preserving arguments that remain in the call.

The core distinction is therefore between the source range of an argument and the source range of an expression nested inside it. Ruff’s correction makes that distinction explicit for keywords while keeping expression-specific parenthesis handling for positional arguments.

The full option keyword spans its name and grouped value, with an insertion point before the keyword name.

Code Evidence

Adding before the first existing keyword

The merged change in crates/ruff_linter/src/fix/edits.rs removes the parenthesized-value lookup from the keyword insertion branch. The resulting branch is:

if let Some(keyword) = arguments.keywords.first() {
    // The keyword's range starts at its name, even if its value is parenthesized.
    Edit::insertion(format!("{argument}, "), keyword.start())
}

This directly implements the helper’s documented behavior: insert before the first existing keyword argument when one exists; otherwise, add at the end of the argument list. The changed keyword branch inserts the requested text followed by a comma and space at keyword.start(). It no longer substitutes a range derived from the keyword’s value.

The B028 snapshot records this before-and-after result:

# Before
warnings.warn("test", category=(DeprecationWarning))

# After
warnings.warn("test", stacklevel=2, category=(DeprecationWarning))

The message remains first, the new keyword appears before category, and the existing parenthesized value remains intact. The labels above are editorial; the calls reproduce the official snapshot’s input and replacement.

A second official fixture makes the first keyword’s value span multiple lines:

warnings.warn(
    message=(
        "a"
        "b"
    ),
    category=UserWarning,
)

Here, the snapshot changes the line beginning message=( to stacklevel=2, message=(. This is useful evidence because insertion must happen before the first keyword, even when that keyword is the message itself and its value continues across several lines. The existing category argument remains a later neighbor. The helper’s job is to choose the argument boundary, not a point within the multiline value.

Removing complete keywords, handling positional expressions separately

The removal helper previously asked for a parenthesized range around arg.value() before falling back to the argument’s range. The merged version branches on the kind of argument:

let parenthesized_range = match arg {
    ast::ArgOrKeyword::Keyword(keyword) => keyword.range(),
    ast::ArgOrKeyword::Arg(expr) => {
        token::parenthesized_range(expr.into(), arguments.into(), tokens)
            .unwrap_or(expr.range())
    }
};

For a keyword, the selected range now covers the keyword itself, including its value. For a positional expression, the helper still searches for a parenthesized source range and falls back to the expression’s range. This separate branch matters because parentheses surrounding a positional expression can be relevant to removing that complete argument. The fix does not simply discard parenthesis handling throughout the helper.

These reconstructed examples are illustrative, not additional official fixtures:

example((value), following=True)
example(option=(value), following=True)

In the first call, the removable item is a positional expression with surrounding parentheses. In the second, it is a keyword whose value has parentheses. Treating both as a value expression obscures the keyword’s name and equals sign. The new match distinguishes those two source-edit requirements.

Separators and arguments that remain

The official UP022 additions exercise removal when another keyword follows the removed arguments. They place parentheses around either stderr or stdout:

subprocess.run(["foo"], stdout=subprocess.PIPE, stderr=(subprocess.PIPE), text=True)
subprocess.run(["foo"], stdout=(subprocess.PIPE), stderr=subprocess.PIPE, text=True)

For both inputs, the merged snapshot records the same replacement:

subprocess.run(["foo"], capture_output=True, text=True)

These cases show more than a value disappearing. The complete stdout and stderr arguments are replaced by capture_output=True, while the positional command and the following text=True argument remain. The resulting call also has the necessary separators between its remaining arguments.

The supplied diff retains the removal helper’s branch for an argument that is not the last node, immediately after the corrected range selection. The added fixture comment explicitly identifies this non-final-argument situation. Together with the snapshots, that establishes why the neighboring argument matters: removing the selected keyword must leave the following argument connected to a valid argument list. The displayed hunks do not show the complete separator algorithm, so the evidence supports the corrected range choice and these concrete outcomes rather than a new general claim about all comma handling.

Why It Matters

Shared edit helpers sit beneath individual lint rules. A rule can correctly identify an improvement while its source-edit machinery chooses the wrong boundary. This patch addresses that lower-level problem: where an insertion begins and how much source a removal identifies as the argument.

The two regression families exercise different uses of those helpers. B028 adds an explicit stacklevel keyword to a warning call. UP022 replaces paired pipe arguments with capture_output. Their common feature is a parenthesized keyword value, but the edits move in opposite directions. Correcting insertion alone would not establish correct removal, and removing a value alone would not establish removal of its complete keyword.

The snapshots also preserve an important distinction between syntactic correctness and behavioral safety. Both new B028 cases and both new UP022 cases carry the note that the fix is unsafe and may change runtime behavior. Correcting source boundaries does not remove that qualification. Developers should review the rule’s proposed change as well as the resulting syntax.

For maintainers of source-transformation tools, the broader engineering lesson is an inference from this patch: test edits around nested syntax and around arguments that will remain after the transformation. A selected expression can look like the relevant unit semantically while still being too small for the required textual operation.

An Open Question

Which argument-list shapes should source-transformation tools prioritize in regression tests to catch confusion between an expression’s range and its enclosing argument’s range?

The merged cases offer concrete starting points: a first keyword with a parenthesized value, a multiline first keyword, and removable keywords followed by a surviving keyword. The question is how to extend that coverage efficiently, without treating a few successful snapshots as proof that every neighboring-token arrangement is covered.

Scope and Limitations

This article is based on the supplied research report and the supplied official merged GitHub diff and snapshot excerpts. The patch and snapshots take precedence over the PR’s prose when describing implementation and output. PR #30189 was merged on November 9, 2026; Ruff 0.17.5 was published on November 12, 2026.

The PR author’s test plan reports 240 relevant tests passing and two failures with the old edits.rs in the added cases. Those numbers are attributed to the PR’s report. No tests were run for this article, and the snapshot excerpts are recorded expected transformations rather than an independent execution performed here.

The demonstrated scope is the shared keyword-argument boundary correction and its B028 and UP022 regressions. The supplied evidence does not establish performance changes, exhaustive syntax coverage, or a change to either rule’s unsafe-fix classification. It also does not establish that every existing Ruff autofix was affected.

Sources