Skip to content

[ty] Add validation for type parameters with defaults after TypeVarTuple#23807

Merged
AlexWaygood merged 2 commits intomainfrom
claude/forbid-default-after-typevartuple-zZGrh
Mar 8, 2026
Merged

[ty] Add validation for type parameters with defaults after TypeVarTuple#23807
AlexWaygood merged 2 commits intomainfrom
claude/forbid-default-after-typevartuple-zZGrh

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Mar 8, 2026

Summary

This PR adds validation to enforce PEP 695 type parameter ordering rules. Specifically, it prevents type parameters with defaults from following a TypeVarTuple in both generic classes and type aliases, as this is prohibited by the typing spec.

The validation is necessary because a TypeVarTuple consumes all remaining positional type arguments, making any subsequent type parameters with defaults meaningless and ambiguous.

In due course, we need to enforce the same thing for legacy type variables as well, but attempting that got pretty hacky while we're still in a position where we don't have TypeVarTuple support elsewhere, so I left it for now.

Note that pyrefly complains about this, with a PEP-695 function -- but no other type checker does, so I decided not to complain about this. It's weird (why would you write this?), but functions can't be explicitly specialized, so I don't see anything wrong with it in the same way as with type aliases and classes:

def f[*Ts, T = int](): ...

Test Plan

mdtests and snapshots

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Mar 8, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 8, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 87.10%. The percentage of expected errors that received a diagnostic held steady at 77.81%. The number of fully passing files held steady at 63/131.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 8, 2026

mypy_primer results

Changes were detected when running on open source projects
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/build/wheel.py:99:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 62 diagnostics
+ Found 61 diagnostics

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 8, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 8, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@AlexWaygood
Copy link
Member Author

It seems the conformance suite only has tests for this check for legacy type parameters, not PEP-695 type parameters, and the legacy-parameter bit is the bit I deferred for now. But I think this is a worthwhile check to add anyway; it catches a TypeError at runtime. The PR also adds tests for the legacy-parameter bit, which may be able to reuse some of the code added in this PR.

@AlexWaygood AlexWaygood marked this pull request as ready for review March 8, 2026 17:00
@astral-sh-bot astral-sh-bot bot requested a review from oconnor663 March 8, 2026 17:00
Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

LGTM.

@AlexWaygood AlexWaygood enabled auto-merge (squash) March 8, 2026 18:12
@AlexWaygood AlexWaygood force-pushed the claude/forbid-default-after-typevartuple-zZGrh branch from 2bb61fa to 0bc361c Compare March 8, 2026 18:23
@AlexWaygood AlexWaygood merged commit 0d84a06 into main Mar 8, 2026
50 checks passed
@AlexWaygood AlexWaygood deleted the claude/forbid-default-after-typevartuple-zZGrh branch March 8, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants