-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Make sure to keep type variables even if unused. #15248
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
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 |
---|---|---|
|
@@ -3105,7 +3105,8 @@ T = TypeVar('T') | |
|
||
def f(x: Optional[T] = None) -> Callable[..., T]: ... | ||
|
||
x = f() # E: Need type annotation for "x" | ||
# Question: Is this alright? | ||
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. No, this test specifically tests the error will be shown. |
||
x: Callable[..., T] = f() | ||
y = x | ||
|
||
[case testDontNeedAnnotationForCallable] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1520,3 +1520,28 @@ def identity(func: Callable[P, None]) -> Callable[P, None]: ... | |
@identity | ||
def f(f: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ... | ||
[builtins fixtures/paramspec.pyi] | ||
|
||
[case testParamSpecificationSurvivesCall] | ||
# https://github.com/python/mypy/issues/12595 | ||
from typing import TypeVar, Callable | ||
from typing_extensions import Concatenate, ParamSpec | ||
|
||
T = TypeVar('T') | ||
U = TypeVar('U') | ||
P = ParamSpec('P') | ||
|
||
def t(f: Callable[P, T]) -> Callable[P, T]: ... | ||
|
||
def pop_off( | ||
transform: Callable[Concatenate[T, P], U] | ||
) -> Callable[P, Callable[[T], U]]: ... | ||
|
||
u = pop_off(t) | ||
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. TBH I don't think this situation is what is going on in the issue. IIUC you tried to simplify the example, but it seems to me you may have simplified too much. |
||
reveal_type(u) # N: Revealed type is "def [P, T] () -> def (def (*P.args, **P.kwargs) -> T`-2) -> def (*P.args, **P.kwargs) -> T`-2" | ||
|
||
@u() | ||
def f(x: int) -> None: ... | ||
|
||
reveal_type(f) # N: Revealed type is "def (x: builtins.int)" | ||
f(0) | ||
[builtins fixtures/paramspec.pyi] |
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 don't like this. A type
def [T] () -> <anything with T>
doesn't make sense, and we should figure out why it was inferred/constructed in the first place. This is really just sweeping dust under the carpet.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 can try to explain why this happens:
Have a function that takes a
Callable[Concatenate[T, P], ...]
and then returns aCallable[P, Callable[[T], ...]]
. This could be some utility function that curries (in reverse), for example.Now, the problem is that if you use this on
def [T] (T, int) -> ...
then this will turn into...def [T] (int) -> (T) -> ...
. At least, as currently implemented. Thus, I see two solutions:def [T] (T, T) -> ...
intodef [T] (T) -> T -> ...
.Sorry if this is a confusing explanation!
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.
Ah just to be clear, the problem with
def [T] (int) -> (T) -> ...
is we'll infer anUninhabitedType
forT
, meaning that we can't actually trust the return type anymore.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.
Interesting. I am actually working on a (tangentially related) PR that will likely fix this (or at least will allow a more principled solution). I will post a link here when it is ready.