-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Type substitutions in Callable ignore TypeVar bounds #8922
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
Comments
When substituting values for type variables in generic type aliases, mypy doesn't check that the value is compatible with the bound. Here's a simplified repro: from typing import Callable, TypeVar, List
N = TypeVar('N', bound='int')
F = Callable[[], N]
L = List[N]
x: F[str] # No error, but should be error
y: L[str] # No error, but should be error |
The |
What does In your simplified repro,
Combining both implications, this means that for any type So even if we don't know which concrete type is passed to Note that the |
I opened a separate issue about the handling of |
Should this be the case though? Would it not make more sense to default to whichever value is specified as |
Deserves separate issue. |
Still happens in 0.931. |
Mypy now checks for type variable bounds in type aliases. |
When I run mypy on the following code:
It outputs:
All of the things mypy warns about are valid. But there are also statements it doesn't warn about that are incorrect code.
The most clear one is the type annotation of
fOther
(last line of the test case): while the functionf
does indeed return an object of typeOther
, that type gets substituted forBT
in the definition ofFactory
andBT
hasBase
as its bound, which is a type unrelated toOther
.A similar but more complex case can be made for
fAny
, which doesn't provide a type argument. However, there cannot exist a type argument that would satisfy both the bound ofBT
and the signature off
.I have limited knowledge of the internals of mypy, but it seems feasible that the
fOther
case could be reported. If thefAny
case can be reported as well that would be great, but I can imagine that determining that no valid type argument can exist is a lot harder than validating a given type argument.I'm using mypy 0.770 on Python 3.8.2.
The text was updated successfully, but these errors were encountered: