-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[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
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
f23ae75
f82b3f1
9a37861
4f7ad7b
b1ede88
a892be3
bfde3e4
f624bfd
c6a4e1c
4bcca58
c85f102
73acf0a
2950af4
2fd7a7d
e476624
690310c
c94fbe2
99ec0be
25a6690
e906526
068eb1f
8069064
8871fdd
ddcd76c
3c811c1
b9ecab1
8655598
86271d6
e583cb7
bdaf8e5
a4a3aff
d223f64
49ca97a
5a8a950
92894d3
649c7bc
1f34f43
7e2ea8b
da31e13
ccb03d3
019db2a
358185b
63c75d8
88eb5eb
7f4893d
d942975
1dd3cf0
cba45ac
f8a5d04
dfcdbcf
94b4dd8
42185b6
483f342
a914071
2614be3
26c847c
4e3dd58
2897d49
06a02fc
44256de
75b8516
e79986a
18ac8e6
5b01dba
0cc5e03
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 |
|---|---|---|
|
|
@@ -503,7 +503,8 @@ class C[**P]: | |
| def __init__(self, f: Callable[P, int]) -> None: | ||
| self.f = f | ||
|
|
||
| def f(x: int, y: str) -> bool: | ||
| # Note that the return type must match exactly, since C is invariant on the return type of C.f. | ||
| def f(x: int, y: str) -> int: | ||
| return True | ||
|
|
||
| c = C(f) | ||
|
|
@@ -618,6 +619,22 @@ reveal_type(foo.method) # revealed: bound method Foo[(int, str, /)].method(int, | |
| reveal_type(foo.method(1, "a")) # revealed: str | ||
| ``` | ||
|
|
||
| ### Gradual types propagate through `ParamSpec` inference | ||
|
|
||
| ```py | ||
| from typing import Callable | ||
|
|
||
| def callable_identity[**P, R](func: Callable[P, R]) -> Callable[P, R]: | ||
| return func | ||
|
|
||
| @callable_identity | ||
| def f(env: dict) -> None: | ||
| pass | ||
|
|
||
| # revealed: (env: dict[Unknown, Unknown]) -> None | ||
| reveal_type(f) | ||
| ``` | ||
|
|
||
| ### Overloads | ||
|
|
||
| `overloaded.pyi`: | ||
|
|
@@ -662,7 +679,7 @@ reveal_type(change_return_type(int_int)) # revealed: Overload[(x: int) -> str, | |
| reveal_type(change_return_type(int_str)) # revealed: Overload[(x: int) -> str, (x: str) -> str] | ||
|
|
||
| # error: [invalid-argument-type] | ||
| reveal_type(change_return_type(str_str)) # revealed: Overload[(x: int) -> str, (x: str) -> str] | ||
| reveal_type(change_return_type(str_str)) # revealed: (...) -> str | ||
|
Contributor
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. Hmm, why do we lose the parameters here?
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 an example where we're now collectively taking into account all of the constraints across the entire call site. In this case, we're correctly flagging |
||
|
|
||
| # TODO: Both of these shouldn't raise an error | ||
| # error: [invalid-argument-type] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -883,7 +883,7 @@ reveal_type(C[int]().y) # revealed: int | |
| class D[T = T]: | ||
| x: T | ||
|
|
||
| reveal_type(D().x) # revealed: T@D | ||
| reveal_type(D().x) # revealed: Unknown | ||
|
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 an error condition anyway, and I find it better to infer |
||
| ``` | ||
|
|
||
| [pep 695]: https://peps.python.org/pep-0695/ | ||
| 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 TODO is not also removed because we end up inferring this constraint set when comparing
headtoCallable[[A], B]:We then try to remove
T@headfrom the constraint set by calculatingWe should be able to pick
T@head = B@invokeand simplify that towhich 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.