Skip to content

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

Closed

Description

@pkch
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.

Activity

  1. ilevkivskyi commented on Apr 10, 2017

    @ilevkivskyi
    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.

  2. pkch commented on Apr 10, 2017

    @pkch
    ContributorAuthor

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

  3. 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
  4. ilevkivskyi commented on Nov 12, 2019

    @ilevkivskyi
    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions