Skip to content

type signature in generic subclass #5664

Closed

Description

@oraluben
from typing import Generic, Callable, TypeVar, Iterator

T = TypeVar('T')


class Box(Generic[T]):
    @classmethod
    def wrap(cls, generator: Callable[[], T]) -> 'Box[T]':
        pass


class IteratorBox(Box[Iterator[T]],
                  Generic[T]):
    pass


def g() -> int:
    return 0


r = IteratorBox.wrap(g)

b: IteratorBox = IteratorBox
reveal_type(b.wrap)
reveal_type(IteratorBox.wrap)
reveal_type(IteratorBox.wrap(g))

output of mypy is

as type "IteratorBox[Any]")
test.py:24: error: Revealed type is 'def (generator: def () -> typing.Iterator*[Any]) -> test.Box[typing.Iter
ator*[Any]]'
test.py:25: error: Revealed type is 'def [T] (generator: def () -> T`1) -> test.Box[T`1]'
test.py:26: error: Revealed type is 'test.Box[builtins.int*]'

I was expecting that mypy will find type mismatch to call IteratorBox.wrap(g), which it didn't.
And reveal_type(IteratorBox.wrap) just return the type of the method in base class.

Activity

  1. ilevkivskyi commented on Sep 24, 2018

    @ilevkivskyi
    Member

    This is a known issue, see #3645, but I don't want to close this because I want to collect all the use cases for type variables in class methods, so that we can understand what is actually desired and test our solution properly.

  2. oraluben commented on Sep 24, 2018

    @oraluben
    Author

    Thanks for your reply! I have tried the PR and found a code may get wrong checking result. If it's ok to put my repro in the PR?

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