Skip to content
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

Append Any to unparametrized generics in upper bounds of type variables #3153

Closed
pkch opened this issue Apr 10, 2017 · 3 comments
Closed

Append Any to unparametrized generics in upper bounds of type variables #3153

pkch opened this issue Apr 10, 2017 · 3 comments

Comments

@pkch
Copy link
Contributor

pkch commented Apr 10, 2017

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.

@ilevkivskyi
Copy link
Member

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.

@pkch
Copy link
Contributor Author

pkch commented 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).

@pkch 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
@ilevkivskyi
Copy link
Member

This now doesn't generate an error, so there is no action item here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants