Skip to content

[ty] Infer generic class pattern capture types - #26479

Merged
charliermarsh merged 3 commits into
mainfrom
charlie/fix-generic-class-pattern-captures
Jul 1, 2026
Merged

[ty] Infer generic class pattern capture types#26479
charliermarsh merged 3 commits into
mainfrom
charlie/fix-generic-class-pattern-captures

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 30, 2026

Copy link
Copy Markdown
Member

Summary

When a class pattern matches a generic subclass through a specialized base-class subject, captures declared on the subclass previously fell back to Unknown or object. Narrowing retained the base specialization, but member lookup still used an unspecialized pattern class.

This infers the pattern subclass's type arguments by solving the mapping from its generic base to the subject class. We use the result only when every pattern-class type variable has one exact invariant solution; covariant bases and unconstrained parameters keep the existing conservative fallback:

class Base[T]: ...

class Child[T](Base[T]):
    item: T

def f(value: Base[int]) -> None:
    match value:
        case Child(item=item):
            reveal_type(item)  # int

Closes astral-sh/ty#3879.

@charliermarsh charliermarsh added the ty Multi-file analysis & type inference label Jun 30, 2026
@charliermarsh
charliermarsh marked this pull request as ready for review July 1, 2026 00:49
@charliermarsh
charliermarsh requested a review from a team as a code owner July 1, 2026 00:49
@charliermarsh
charliermarsh merged commit 9e76adb into main Jul 1, 2026
63 checks passed
@charliermarsh
charliermarsh deleted the charlie/fix-generic-class-pattern-captures branch July 1, 2026 00:49
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.

Generic type parameters not propagated to names bound in match class patterns

1 participant