Skip to content

Incorrect inference of type parameter for a generic container #3183

Open
@pkch

Description

@pkch
from typing import *

T = TypeVar('T', bound='A')

class X: ...

class Y(X): ...

class A(Iterable[X]):
    def __iter__(self) -> Iterator[X]: ...

class B(A, Iterable[Y]):
    def __iter__(self) -> Iterator[Y]: ...

b = B()
for x in b:
    reveal_type(x)  # 'Y*'

y = list(b)
reveal_type(y)  # 'builtins.list[X*]'

The first reveal_type is correct.
The second is wrong: it should be builtins.list[Y*]. Somehow the presence of A in the bases of B causes this mistake.

In addition, even a workaround doesn't work:

z = List[Y](b)  # Argument 1 to "list" has incompatible type "B"; expected Iterable[Y]
reveal_type(z)  # 'builtins.list[Y*]'

The revealed type is now correct, but the constructor doesn't pass the type check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions