Skip to content

fix(UP046,UP047): skip autofix for starred TypeVar constraints - #28232

Closed
mturac wants to merge 2 commits into
astral-sh:mainfrom
mturac:fix/issue-26954
Closed

mturac wants to merge 2 commits into
astral-sh:mainfrom
mturac:fix/issue-26954

Conversation

@mturac

@mturac mturac commented Sep 1, 2026

Copy link
Copy Markdown

Summary

Fixes #26954.

When TypeVar is called with starred positional arguments (e.g. TypeVar("T", *constraints)), the constraints cannot be statically resolved. UP046 and UP047 would previously attempt to convert these to PEP 695 syntax, producing invalid output like class Foo[T: (*constraints)].

This PR makes expr_name_to_type_var return None when any constraint argument is a starred expression, so the TypeVar is not recognized for conversion. The diagnostic may still be emitted but no autofix is offered.

Test plan

  • Added test cases to both UP046_0.py and UP047_0.py fixtures with TypeVar("T_starred", *constraints)
  • UP046 snapshot updated: diagnostic emitted for StarredConstraints class but no fix content (correct — TypeVar not recognized)
  • UP047 snapshot unchanged: starred_constraints() function produces no diagnostic at all (correct — TypeVar not recognized, so the function isn't detected as generic)
  • All 161 pyupgrade tests pass

When `TypeVar` is called with starred positional arguments
(e.g. `TypeVar("T", *constraints)`), the constraints cannot be
statically resolved to produce valid PEP 695 type parameter syntax.

Return `None` from `expr_name_to_type_var` when any constraint
argument is an `Expr::Starred`, so UP046/UP047 emit no fix for
these TypeVars. The diagnostic may still appear but will not offer
an invalid autofix.

Fixes astral-sh#26954
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre September 1, 2026 21:39
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-01T21:43:52.420773Z 1b0b303 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1b0b303822

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +343 to +344
if constraints.iter().any(|expr| expr.is_starred_expr()) {
return None;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 1. Propagate unfixable constraints to every caller

When a locally defined starred TypeVar is passed via TypeAliasType(..., type_params=(T_starred,)), returning None here reaches the unwrap_or at non_pep695_type_alias.rs:176, which treats it as unrestricted and emits the safe UP040 fix type Alias[T_starred] = ..., silently discarding the original constraints. A mixed UP047 function similarly still fixes any convertible parameters because that caller ignores any_skipped. Distinguish an unconvertible local TypeVar from an unknown name so every caller can suppress its fix, and cover the shared helper's UP040 and mixed-UP047 paths.

AGENTS.md reference: AGENTS.md:L180-L182

Useful? React with 👍 / 👎.

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.

I think Codex is correct here. I previously flagged a similar issue in #27202 (comment).

Comment on lines +343 to +344
if constraints.iter().any(|expr| expr.is_starred_expr()) {
return None;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 2. Retain valid starred constraint conversions

Starred constraints are representable in PEP 695 syntax: the fixture's sole-star case can become T_starred: (*constraints,), while TypeVar("T", bool, *constraints) already rendered as the valid T: (bool, *constraints) before this change. Rejecting every starred expression therefore removes correct UP040/UP046/UP047 migrations, including cases whose previous output was already valid; preserve these constraints and add the tuple comma only when the star is the sole element instead of treating them as unconvertible.

AGENTS.md reference: AGENTS.md:L180-L183

Useful? React with 👍 / 👎.

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.

This also seems correct.

@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.

Codex's inline comments seem correct to me.

I initially wondered if this fix was better combined with #27202, but it seems like a small parenthesization change is all that's needed here.

…dler

When `expr_name_to_type_var` returns `None` for a TypeVar with starred
constraints, the UP040 handler's `unwrap_or` fallback silently treated
it as an unrestricted TypeVar, emitting a fix that drops the constraints.

Replace `map` + `unwrap_or` with `and_then` so a `None` from
`expr_name_to_type_var` propagates through `collect` and suppresses the
fix entirely, matching how UP046/UP047 already handle this via
`any_skipped`.
@mturac

mturac commented Sep 1, 2026

Copy link
Copy Markdown
Author

Thanks for the review! Pushed a fix in 9ffcbd5:

P1 (unfixable propagation to UP040): The unwrap_or fallback in non_pep695_type_alias.rs was indeed silently treating unfixable starred TypeVars as unrestricted. Replaced map + unwrap_or with and_then so None propagates through collect and suppresses the fix entirely — matching how UP046/UP047 already handle this via any_skipped.

Added a test case for TypeAliasType with starred constraints confirming no fix is emitted.

P2 (retaining valid starred conversions): I agree some starred constraints could be representable in PEP 695 syntax. However, the current TypeVarRestriction::Constraint representation doesn't model starred expressions, so emitting them correctly would need a broader refactor (new variant or wrapper). I think it's safer to skip the fix for now and handle valid starred conversions in a follow-up — the current change at least prevents the silent constraint-dropping regression.

@ntBre ntBre added the bot:ai-policy-close Auto-close a PR due to a violation of the org AI policy label Sep 1, 2026
@astral-sh-bot

astral-sh-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

We support using AI (i.e., LLMs) while coding. However, AI should not be used to communicate with maintainers.

Please read and follow our AI policy when submitting issues and pull requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:ai-policy-close Auto-close a PR due to a violation of the org AI policy

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fixes for UP046 and UP047 introduce syntax errors for starred constraints

2 participants