[pylint] Implement swap-with-temporary-variable (PLR1712) by Bnyro · Pull Request #22205 · astral-sh/ruff (original) (raw)

@ntBre added rule

Implementing or modifying a lint rule

preview

Related to preview mode features

labels

Dec 29, 2025

ntBre

@ntBre ntBre changed the title[pylint]: Implement consider-swap-variables (PLR1712) [pylint] Implement swap-with-temporary-variable (PLR1712)

Jan 2, 2026

ntBre

ntBre

@Bnyro

@Bnyro Bnyro deleted the temporary-variable-swap branch

February 19, 2026 23:35

knutwannheden pushed a commit to openrewrite/ruff that referenced this pull request

Feb 20, 2026

@Bnyro @knutwannheden

…al-sh#22205)

Summary

This PR implements the consider-swap-variables rule from pylint. Basically it tries to find code parts that swap two variables with each other using a temporary variable.

Example code:

temp = x
x = y
y = temp

can be simplified to

x, y = y, x

https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/consider-swap-variables.html

Test Plan

I've added new snapshots tests.

PS: Since this is my first contribution here and I'm not too familiar with the codebase, suggestions are very welcome! The implementation might also not be 100% memory-optimized yet, since we use clone a few times.

@ntBre ntBre mentioned this pull request

Feb 26, 2026

ntBre added a commit that referenced this pull request

Feb 26, 2026

@ntBre

… (PLR1712) (#22205)"

This is an alternative to #23588, checking the ecosystem results in comparison.

ntBre added a commit that referenced this pull request

Feb 26, 2026

@ntBre

Summary

The huge number of changes in #22205 (comment) should have obviously been a red flag, but I think it would be nice if CI failed when new ecosystem panics were introduced. This PR adds a check for diagnostic lines that start with panic: Panicked at crates/, raises a ToolError if any are found in the results from the comparison executable, and then also exits non-zero if any errors are returned.

If exiting non-zero is going too far, we could also just raise the ToolError, as that will at least trigger this message, which was not the case on the PLR1712 PR:

https://github.com/astral-sh/ruff/blob/f14edd8661e2803254f89265548c7487f47a09f6/python/ruff-ecosystem/ruff_ecosystem/check.py#L103-L106

Another option would be not to exit zero if Ruff panics, even if --exit-zero is used, but I saw that ty has the same behavior and assumed that that was intentional.

Test Plan

Local testing on the 0.15.3 tag showing that ruff-ecosystem exited non-zero. I can also introduce a panic into a lint rule to test this in CI

@ntBre ntBre mentioned this pull request

Feb 26, 2026

ntBre added a commit that referenced this pull request

Mar 4, 2026

@ntBre

Summary

The huge number of changes in #22205 (comment) should have obviously been a red flag, but I think it would be nice if CI failed when new ecosystem panics were introduced. This PR adds a check for diagnostic lines that start with panic: Panicked at crates/, raises a ToolError if any are found in the results from the comparison executable, and then also exits non-zero if any errors are returned fails the CI run if the corresponding error message was printed.

After trying this out in CI, I opted not to change the script's exit code itself because that suppressed the ecosystem comment. It feels a little hackier this way but preserves the behavior I wanted of both failing CI and still getting the ecosystem comment to help with debugging.

Test Plan

Local testing on the 0.15.3 tag showing that ruff-ecosystem exited non-zero and some manual testing in CI, as you can see below.

nicopauss pushed a commit to Intersec/lib-common that referenced this pull request

Apr 1, 2026

@renovate-bot @nicopauss

Released on 2026-03-19.

Released on 2026-03-12.

Released on 2026-03-05.

Released on 2026-02-26.

This is a follow-up release to 0.15.3 that resolves a panic when the new rule PLR1712 was enabled with any rule that analyzes definitions, such as many of the ANN or D rules.

Released on 2026-02-26.

Released on 2026-02-19.

Released on 2026-02-12.

Released on 2026-02-03.

Check out the blog post for a migration guide and overview of the changes!

The following rules have been stabilized and are no longer in preview:

The following behaviors have been stabilized:

This release introduces the new 2026 style guide, with the following changes:

Renovate-Branch: renovate/2024.6-ruff-0.x Change-Id: I8f8e865435fde1fc736fe2528261a604acb46215 Priv-Id: f7e1d99008e3617149c4b639a9a2bbc06212d064

KotlinIsland pushed a commit to KotlinIsland/basedpython that referenced this pull request

May 1, 2026

@ntBre

Summary

The huge number of changes in astral-sh/ruff#22205 (comment) should have obviously been a red flag, but I think it would be nice if CI failed when new ecosystem panics were introduced. This PR adds a check for diagnostic lines that start with panic: Panicked at crates/, raises a ToolError if any are found in the results from the comparison executable, and then also exits non-zero if any errors are returned fails the CI run if the corresponding error message was printed.

After trying this out in CI, I opted not to change the script's exit code itself because that suppressed the ecosystem comment. It feels a little hackier this way but preserves the behavior I wanted of both failing CI and still getting the ecosystem comment to help with debugging.

Test Plan

Local testing on the 0.15.3 tag showing that ruff-ecosystem exited non-zero and some manual testing in CI, as you can see below.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})