You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Append Any to unparametrized generics in upper bounds of type variables #3153
T = TypeVar('T')
class X(Generic[T]):
v: T
U = TypeVar('U', bound=X)
def f(x: U, v: T) -> None:
x.v = v # Incompatible types in assignment (expression has type "T", variable has type "T")
The fact that the error is reported is perfectly fine, but the error message is a mini-puzzle 😆
I don't have an opinion about the best message, but one option is to reuse the notation followed by reveal_type:
reveal_type(x.v) # Revealed type is 'T`1'
reveal_type(v) # Revealed type is 'T`-2'
While somewhat cryptic, it already appears in reveal_type messages anyway, so it might be worth adding the explanation of this notation to the docs. Of course, the user won't know the internal type ids, so he still won't know what T`1 and T`-2 correspond to exactly, but this will at least send him on the right path.
There is a different problem here: bound=X should be "translated" to bound=X[Any] per PEP 484, but it is not. If I manually write X[Any], then there is no error.
What I really meant was U = TypeVar('U', bound=X[T]), but it is correctly disallowed by mypy (although PEP 484 doesn't explicitly disallow that, I'm sure it intended to).
changed the title [-]Disambiguate different instances of type variables in error messages[/-][+]Append Any to unparametrized generics in upper bounds of type variables[/+]on Apr 10, 2017
Activity
ilevkivskyi commentedon Apr 10, 2017
There is a different problem here:
bound=Xshould be "translated" tobound=X[Any]per PEP 484, but it is not. If I manually writeX[Any], then there is no error.pkch commentedon Apr 10, 2017
You're right.
What I really meant was
U = TypeVar('U', bound=X[T]), but it is correctly disallowed by mypy (although PEP 484 doesn't explicitly disallow that, I'm sure it intended to).[-]Disambiguate different instances of type variables in error messages[/-][+]Append Any to unparametrized generics in upper bounds of type variables[/+]ilevkivskyi commentedon Nov 12, 2019
This now doesn't generate an error, so there is no action item here.