Skip to content

[ty] More type-variable default validation#23639

Merged
AlexWaygood merged 3 commits intomainfrom
claude/fix-type-param-scope-oe7ea
Mar 5, 2026
Merged

[ty] More type-variable default validation#23639
AlexWaygood merged 3 commits intomainfrom
claude/fix-type-param-scope-oe7ea

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Feb 28, 2026

Summary

We have several checks at the moment which fire on legacy type variables with invalid defaults in a class context, but which fail to check for legacy type variables with invalid defaults in a function context. This PR adds those missing checks.

The typing spec states that both legacy and PEP-695 type variables are not allowed to have defaults that:

  • reference type variables bound in outer scopes, or
  • reference type variables that are put into scope later on in the type parameter list or function signature

The typing spec also states that a type variable with a default is not allowed to come before any type variables without defaults.

Test Plan

mdtests and snapshots

@AlexWaygood AlexWaygood added ty Multi-file analysis & type inference ecosystem-analyzer labels Feb 28, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 28, 2026

Typing conformance results

No changes detected ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 28, 2026

mypy_primer results

No ecosystem changes detected ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 28, 2026

Memory usage report

Memory usage unchanged ✅

@AlexWaygood
Copy link
Member Author

Following #23623, I guess we now pass all the conformance tests regarding typevar default scoping. But I still think this PR improves semantics -- it matches pyright's behaviour. The upstream tests seem like they're missing some coverage.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 28, 2026

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@AlexWaygood AlexWaygood force-pushed the claude/fix-type-param-scope-oe7ea branch 2 times, most recently from af5547b to c1a2615 Compare February 28, 2026 23:31
@AlexWaygood AlexWaygood changed the title [ty] Detect type variables with invalid defaults [ty] More type-variable default validation Feb 28, 2026
@AlexWaygood AlexWaygood marked this pull request as ready for review February 28, 2026 23:37
@AlexWaygood AlexWaygood force-pushed the claude/fix-type-param-scope-oe7ea branch 2 times, most recently from 8e099a8 to 2920f62 Compare March 2, 2026 13:15
@carljm carljm removed their request for review March 2, 2026 17:44
Copy link
Contributor

@oconnor663 oconnor663 left a comment

Choose a reason for hiding this comment

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

LGTM with some nits.

Copy link
Contributor

@oconnor663 oconnor663 left a comment

Choose a reason for hiding this comment

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

Meant to put the green checkmark on the last comment.

@oconnor663 oconnor663 removed their assignment Mar 3, 2026
@AlexWaygood AlexWaygood force-pushed the claude/fix-type-param-scope-oe7ea branch from 2920f62 to 6408c53 Compare March 5, 2026 13:02
Copy link
Member Author

Choose a reason for hiding this comment

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

I know, I know... 400+ lines being added to infer/builder.rs... I'll rip it out into a submodule as a followup.

@AlexWaygood AlexWaygood enabled auto-merge (squash) March 5, 2026 13:04
@AlexWaygood AlexWaygood merged commit 5f0fd91 into main Mar 5, 2026
50 checks passed
@AlexWaygood AlexWaygood deleted the claude/fix-type-param-scope-oe7ea branch March 5, 2026 13:06
carljm added a commit that referenced this pull request Mar 5, 2026
* main:
  Update conformance suite commit hash (#23746)
  conformance.py: Collapse the summary paragraph when nothing changed (#23745)
  [ty] Make inferred specializations line up with source types more better (#23715)
  Bump 0.15.5 (#23743)
  [ty] Render all changed diagnostics in conformance.py (#23613)
  [ty] Split deferred checks out of `types/infer/builder.rs` (#23740)
  Discover markdown files by default in preview mode (#23434)
  [ty] Use `HasOptionalDefinition` for `except` handlers (#23739)
  [ty] Fix precedence of `all` selector in TOML configurations (#23723)
  [ty] Make `all` selector case sensitive (#23713)
  [ty] Add a diagnostic if a `TypeVar` is used to specialize a `ParamSpec`, or vice versa (#23738)
  [ty] Override home directory in ty tests (#23724)
  [ty] More type-variable default validation (#23639)
  [ty] Validate bare ParamSpec usage in type annotations, and support stringified ParamSpecs as the first argument to `Callable` (#23625)
  [ty] Add `all` selector to ty.json's `schema` (#23721)
  [ty] Add quotes to related issues links (#23720)
  [ty] Fix panic on incomplete except handlers (#23708)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants