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
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.
The text was updated successfully, but these errors were encountered:
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).
pkch
changed the title
Disambiguate different instances of type variables in error messages
Append Any to unparametrized generics in upper bounds of type variables
Apr 10, 2017
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
: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 whatT`1
andT`-2
correspond to exactly, but this will at least send him on the right path.The text was updated successfully, but these errors were encountered: