-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
bugmypy got something wrongmypy got something wrong
Description
Bug Report
Sorry for the bad title, I've tried to come up with something short and descriptive here.
The issue I am facing occurs when specializing the type of a self argument on a generic class.
To Reproduce
import typing as t
T = t.TypeVar('T')
def concat(its: t.Iterable[t.Iterable[T]]) -> t.Iterable[T]:
for x in its:
for y in x:
yield y
v = ['abc', 'def']
reveal_type(v) # Revealed type is 'builtins.list[builtins.str*]'
print(''.join(concat(v))) # works!
class Stream(t.Generic[T]):
def __init__(self, it: t.Iterable[T]) -> None:
self.it = iter(it)
def __iter__(self) -> t.Iterator[T]:
return self.it
def concat(self: 'Stream[t.Iterable[T]]') -> 'Stream[T]':
return Stream(concat(self))
s = Stream(v)
reveal_type(s) # Revealed type is 'test.Stream[builtins.str*]'
reveal_type(s.it) # Revealed type is 'typing.Iterator[builtins.str*]'
print(''.join(concat(s))) # works!
print(''.join(s.concat())) # Invalid self argument "Stream[str]" to attribute function "concat" with type "Callable[[Stream[Iterable[T]]], Stream[T]]"Expected Behavior
I expect the s.concat() call to be accepted by MyPy.
It actually works when you decorate the self argument with t.Iterable[t.Iterable[T]] instead.
def concat(self: 't.Iterable[t.Iterable[T]]') -> 'Stream[T]':
return Stream(concat(self))Your Environment
- Mypy version used: 0.812
- Mypy command-line flags:
mypy test.py - Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: 3.8.4
- Operating system and version: WSL2 Ubuntu 20
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong