Skip to content

Conversation

AlexWaygood
Copy link
Member

Summary

If the upper bound of a TypeVar can be unpacked, then so can the TypeVar, because no solutions of the TypeVar exist that would not be assignable to the TypeVar's upper bound.

This is similar in spirit to #19981!

Test Plan

Mdtests

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Aug 19, 2025
Comment on lines 472 to 493
@dataclass
class Team[T: tuple[int, str]]:
employees: list[T]

def x[T: tuple[int, str]](team: Team[T]):
age, name = team.employees[0]
reveal_type(age) # revealed: int
reveal_type(name) # revealed: str

class Age(int): ...
class Name(str): ...

class Employee(NamedTuple):
age: Age
name: Name

EMPLOYEES: Final = (Employee(name=Name("alice"), age=Age(42)),)
team = Team(employees=list(EMPLOYEES))
reveal_type(team.employees) # revealed: list[Employee]
age, name = team.employees[0]
reveal_type(age) # revealed: Age
reveal_type(name) # revealed: Name
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 enjoyed writing this test. So cool to see the number of features that are coming together here 😃

Copy link
Contributor

github-actions bot commented Aug 19, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

Copy link
Contributor

github-actions bot commented Aug 19, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

Copy link
Member

@dcreager dcreager left a comment

Choose a reason for hiding this comment

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

Love it

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Nice!

@AlexWaygood AlexWaygood merged commit 662d18b into main Aug 19, 2025
37 checks passed
@AlexWaygood AlexWaygood deleted the alex/unpack-tvar branch August 19, 2025 21:11
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