Repository navigation
Conversation
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
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| if constraints.iter().any(|expr| expr.is_starred_expr()) { | ||
| return None; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
I think Codex is correct here. I previously flagged a similar issue in #27202 (comment).
| if constraints.iter().any(|expr| expr.is_starred_expr()) { | ||
| return None; |
There was a problem hiding this comment.
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 👍 / 👎.
…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`.
|
Thanks for the review! Pushed a fix in 9ffcbd5: P1 (unfixable propagation to UP040): The Added a test case for P2 (retaining valid starred conversions): I agree some starred constraints could be representable in PEP 695 syntax. However, the current |
|
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. |
Summary
Fixes #26954.
When
TypeVaris 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 likeclass Foo[T: (*constraints)].This PR makes
expr_name_to_type_varreturnNonewhen 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
UP046_0.pyandUP047_0.pyfixtures withTypeVar("T_starred", *constraints)StarredConstraintsclass but no fix content (correct — TypeVar not recognized)starred_constraints()function produces no diagnostic at all (correct — TypeVar not recognized, so the function isn't detected as generic)