You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the same way that my first example returns a List which is a subtype of Iterable, I'd expect my function to be correctly typed if I advertise an Iterable implemented as an Iterator. What am I missing?
>>> def a():
yield from [1,2,3]
>>> b = a()
>>> dir(b)
[..., '__iter__', ..., '__next__',...] # wrapped
Both, 'yield' and 'yield from' have next (iterator) and iter (iterable), so, changing whan I've said in my previous comment, seems correct to allow iterable subtypes.
Activity
rockneurotiko commentedon Apr 13, 2015
Hi!
Like you say, 'yield from' check the return type and allow it if it's a subtype of 'Iterable' like you can see here:
https://github.com/JukkaL/mypy/blob/master/mypy/checker.py#L1384
The 'yield' word only allows 'Iterator' like you can see here:
https://github.com/JukkaL/mypy/blob/master/mypy/checker.py#L1357
Actually, yield and yield from, explained in the PEPs (225 and 380), return an Iterator (because both have a next function).
So, I think that the change should be change 'yield from' to only allow an Iterator as return type more than let 'yield' allow Iterable subclass.
fcolas commentedon Apr 13, 2015
But the thing is that
Iteratoris already anIterable:In the same way that my first example returns a
Listwhich is a subtype ofIterable, I'd expect my function to be correctly typed if I advertise anIterableimplemented as anIterator. What am I missing?rockneurotiko commentedon Apr 13, 2015
Yes, it's a subclass, and I've rediscovered why I wrote the 'yield from' support to allow Iterable subtype.
Both, 'yield' and 'yield from' have next (iterator) and iter (iterable), so, changing whan I've said in my previous comment, seems correct to allow iterable subtypes.