-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Infer typevar specializations for Callable types
#21551
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
base: main
Are you sure you want to change the base?
Changes from all commits
544dafa
998b20f
3b509e9
20ecb56
fc2f175
b7fb679
9950c12
fedc754
2c62674
2b949b3
d88120b
957304e
7bbf839
3045258
a303b7a
58c67fd
beb2956
a0f64bd
d3fd988
2e46c8d
db5834d
77ce24a
85e6143
3bcca62
75e9d66
94aca37
b90cdfc
1e33d25
b314119
54a4f2e
3384392
8c7e20a
c0dc6cf
c74eb12
db488e3
056258c
657685f
b84a35f
a372e63
d47e9a6
6138152
c60560f
ecb9c13
22c7fc4
c56d5cc
b3e4855
81fc51e
72e0c32
f29200c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -379,14 +379,13 @@ T = TypeVar("T") | |
| def invoke(fn: Callable[[A], B], value: A) -> B: | ||
| return fn(value) | ||
|
|
||
| def identity(x: T) -> T: | ||
| def identity(x: T, /) -> T: | ||
| return x | ||
|
|
||
| def head(xs: list[T]) -> T: | ||
| def head(xs: list[T], /) -> T: | ||
| return xs[0] | ||
|
|
||
| # TODO: this should be `Literal[1]` | ||
| reveal_type(invoke(identity, 1)) # revealed: Unknown | ||
| reveal_type(invoke(identity, 1)) # revealed: Literal[1] | ||
|
|
||
| # TODO: this should be `Unknown | int` | ||
| reveal_type(invoke(head, [1, 2, 3])) # revealed: Unknown | ||
|
Comment on lines
390
to
391
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This TODO is not also removed because we end up inferring this constraint set when comparing We then try to remove We should be able to pick which I think would then be enough to propagate through the return type to discharge this TODO. I think this would require adding more derived facts to the sequent map. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,7 +304,7 @@ x11: list[Literal[1] | Literal[2] | Literal[3]] = [1, 2, 3] | |
| reveal_type(x11) # revealed: list[Literal[1, 2, 3]] | ||
|
|
||
| x12: Y[Y[Literal[1]]] = [[1]] | ||
| reveal_type(x12) # revealed: list[Y[Literal[1]]] | ||
| reveal_type(x12) # revealed: list[list[Literal[1]]] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is because we're now using |
||
|
|
||
| x13: list[tuple[Literal[1], Literal[2], Literal[3]]] = [(1, 2, 3)] | ||
| reveal_type(x13) # revealed: list[tuple[Literal[1], Literal[2], Literal[3]]] | ||
|
|
||
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.
This is an interesting nuance:
Callable[[A], B]creates a signature containing positional-only parameters, so we have to make the signature here positional-only as well to makeidentitypass the assignability check for it to be a valid argument to thefnparameter.(That said, I'm not convinced that's...correct? Are we mixing up the ordering of the assignability check operands somewhere?)
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.
Yeah, this should work, it does when there aren't any type variables involved (https://play.ty.dev/1663d70f-4daa-492f-84d7-c35c9009fbba):
Uh oh!
There was an error while loading. Please reload this page.
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.
Ok, I looked into this a bit more and what I'm seeing is that:
In
infer_map_impl, the formal signature isCallable[[A], B]and the actual signature is theidentityfunction and you invokeformal_signature.when_constraint_set_assignable_to(..., actual_signature, ...)which means we're checking when isCallable[[A], B]assignable toidentityfunction which leads to checking when is the positional-only parameter (A) is assignable to positional-or-keyword parameter (a: int) which is never. This is because you cannot substitute a callable that takes a positional-or-keyword parameter with a callable that takes a positional-only parameter (https://play.ty.dev/7b31e7a0-19c8-4d2b-8bf6-507764372c0d).This can be tested using
Protocolto define a callable that takes a positional-or-keyword parameter:Calling
testusingpositional_onlywould fail at runtime.