Skip to content

[pyupgrade] Preserve grouping in multiline UP040 fixes - #28164

Merged
ntBre merged 3 commits into
astral-sh:mainfrom
fredrikblau:fix/28102-up040-multiline-alias
Sep 15, 2026
Merged

ntBre merged 3 commits into
astral-sh:mainfrom
fredrikblau:fix/28102-up040-multiline-alias

Conversation

@fredrikblau

Copy link
Copy Markdown
Contributor

Summary

UP040 fixes for multiline TypeAliasType unions could remove the grouping supplied by the call syntax, producing invalid Python. Multiline binary and boolean expressions are now parenthesized in the generated type statement.

Fixes #28102

Test Plan

  • INSTA_FORCE_PASS=1 INSTA_UPDATE=always cargo test -p ruff_linter --lib rules::pyupgrade
  • cargo fmt --all -- --check
  • git diff --check

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 29, 2026 19:39
@fredrikblau fredrikblau changed the title [] Preserve grouping in multiline fixes [pyupgrade] Preserve grouping in multiline UP040 fixes Aug 29, 2026
@ntBre

ntBre commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Could you check if the unwrapped_call_argument helper method added in #28145 would be useful here? This issue was mentioned during PR review, and it sounds promising.

@ntBre ntBre added the bug An issue describing something that isn't working, or a PR that fixes a bug label Sep 1, 2026
@astral-sh-bot

astral-sh-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Rewriting a `TypeAliasType` assignment to a `type` statement drops the
call, so a value that relied on the call's parentheses to span several
lines became a syntax error:

    LongAlias = TypeAliasType(
        "LongAlias",
        int
        | str
    )

Build the right-hand side with `unwrapped_call_argument`, which restores
the parentheses when the argument needs them and leaves the argument's
own parentheses alone when it already has them.

Fixes astral-sh#28102
@fredrikblau
fredrikblau force-pushed the fix/28102-up040-multiline-alias branch from 1682f12 to 9cebbae Compare September 4, 2026 22:57
@fredrikblau

Copy link
Copy Markdown
Contributor Author

Could you check if the unwrapped_call_argument helper method added in #28145 would be useful here? This issue was mentioned during PR review, and it sounds promising.

Yes — unwrapped_call_argument works here, thanks. I've swapped the rule over to it.

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@ntBre
ntBre enabled auto-merge (squash) September 15, 2026 20:22
@ntBre
ntBre merged commit 052a5d3 into astral-sh:main Sep 15, 2026
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Fix error] UP040 fix can introduce a syntax error

3 participants