-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ty] Add precise inference for unpacking a TypeVar if the TypeVar has an upper bound with a precise tuple spec #19985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… an upper bound with a precise tuple spec
@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 |
There was a problem hiding this comment.
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 😃
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it
crates/ty_python_semantic/resources/mdtest/generics/legacy/functions.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
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