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
{{ message }}
Repository navigation
Return type for iter() and aiter() too strict #6030
It is good practice for iterators to have an __iter__ method that returns the iterator itself, even if it's technically not required. Is there a real-life example of an object that has a __next__ method but doesn't have an __iter__ method?
As @srittau pointed out, the issue is that collections.abc.Iterator, which typing.Iterator mirrors, includes __iter__ as part of its implementation even though it isn't required as part of the protocol. My guess is a HasNext protocol or something is the best option here to mirror Python's expectation appropriately.
@Akuli There is no way the current 10 million Python developers all included __iter__ for every iterator they have ever written 😄 (heck I know I personally haven't when writing tests). This isn't even considering historical code since the introduction of iterators in Python way back when.
There is no way the current 10 million Python developers all included __iter__ for every iterator they have ever written ... This isn't even considering historical code since the introduction of iterators in Python way back when.
Even though we have a "prefer false negatives" policy, it doesn't mean that type checkers can't emit errors for bad/legacy code that just happens to work due to some weird property of how the runtime is implemented. So if the code doesn't type check because someone forgot to define __iter__(), I think that's a good thing, because it points out a genuine problem: you can't actually loop over an "iterator" that doesn't have an __iter__() method.
It also seems like "fixing" this would lead to other problems, regardless of how it is "fixed":
If iter() doesn't guarantee (in the stubs) that the returned object has __iter__(), then you can't for-loop over the object that iter() returns. This would be just insane: iter() would not return an iterable.
If we change the definition of Iterator in typeshed, this will also affect Iterable, and we have two choices:
Do not require Iterable to have __iter__, so that not all iterables can be for-looped over. Insane.
Make Iterator not be a subclass of Iterable, so we have iterators that are not iterables. Again, this is just unintuitive, and goes against documentation and people's expectations.
Changing the implementation of iter() to check for the existence of __iter__() would be just an unnecessary step that I guess would make a very critical thing slower, and potentially break existing non-type-checked production code. I'm not a CPython developer, so I don't really know the implications, but they seem bad.
This brings me back to my original question: is there some actual real-life non-toy-example problem that this causes? I really don't think we should sacrifice practicality for purity here.
That said, I like the idea of changing the types of next() and anext(), since their purpose is to just give the next value, and that doesn't need to have anything to do with looping.
It's what __iter__returns which can lack __iter__ that's the concern.
But having typeshed make a call of what should be done is also making a call for nearly all type checkers as they all use typeshed. So making that decision on behalf of all type checkers doesn't seem right to me. Getting iterators to define __iter__ seems like a linter concern more than a type checker concern. And in terms of passing something into iter() which can't be iterated in a for loop, that's one of the points of type checking and thus should be discoverable if you were to do for _ in iter(BadIterator()). 😄
Getting iterators to define __iter__ seems like a linter concern more than a type checker concern.
What linter are you referring to? How would a linter know that something is an "iterator" without __next__, unless it knows the type of every object, and therefore is actually a type checker? Nevermind. I assume you meant a linter complains when defining a class that has __next__ but no __iter__.
making that decision on behalf of all type checkers doesn't seem right to me.
That's what we do at typeshed. If we have a choice of doing something in typeshed once and for all type checkers, versus having each type checker special-case something (and in practice have most of them forget about it until a user complains), we generally prefer to just do it in typeshed.
So you are saying that it would be preferable to have next(BadIterator()) and iter(BadIterable()) succeed at type checking time, as they do at runtime, but have iter(BadIterator()) fail when type checking as it does at runtime? We can do that with overloads:
[...] Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. [...]
That said, we tend to follow implementation, not documentation in typeshed. Looking at #6035, the change seems quite disruptive, though, so I'm not sure we should do this.
I have tried another, promising looking approach in #6035 now: This returns the iter returned from __iter__() directly, like in the implementation. This allows things like that weren't possible before:
@srittau yeah, the docs around all of this are a bit messy. While the definition of an iterator may say that __iter__() should be defined, that doesn't mean the person linking to the glossary definition from iter() realized that's what it said.
Activity
brettcannon commentedon Sep 13, 2021
I didn't dig into it, but this may also affect the parameters for
next()andanext().JelleZijlstra commentedon Sep 13, 2021
Maybe we should change Iterator instead? Making
iter()not return an Iterator sounds very counterintuitive.srittau commentedon Sep 13, 2021
Iteratoris clearly defined to have both__iter__and__next__. I think we should just use a custom protocol here.Akuli commentedon Sep 13, 2021
It is good practice for iterators to have an
__iter__method that returns the iterator itself, even if it's technically not required. Is there a real-life example of an object that has a__next__method but doesn't have an__iter__method?brettcannon commentedon Sep 13, 2021
As @srittau pointed out, the issue is that
collections.abc.Iterator, whichtyping.Iteratormirrors, includes__iter__as part of its implementation even though it isn't required as part of the protocol. My guess is aHasNextprotocol or something is the best option here to mirror Python's expectation appropriately.@Akuli There is no way the current 10 million Python developers all included
__iter__for every iterator they have ever written 😄 (heck I know I personally haven't when writing tests). This isn't even considering historical code since the introduction of iterators in Python way back when.Akuli commentedon Sep 13, 2021
Even though we have a "prefer false negatives" policy, it doesn't mean that type checkers can't emit errors for bad/legacy code that just happens to work due to some weird property of how the runtime is implemented. So if the code doesn't type check because someone forgot to define
__iter__(), I think that's a good thing, because it points out a genuine problem: you can't actually loop over an "iterator" that doesn't have an__iter__()method.It also seems like "fixing" this would lead to other problems, regardless of how it is "fixed":
iter()doesn't guarantee (in the stubs) that the returned object has__iter__(), then you can't for-loop over the object thatiter()returns. This would be just insane:iter()would not return an iterable.Iteratorin typeshed, this will also affectIterable, and we have two choices:Iterableto have__iter__, so that not all iterables can be for-looped over. Insane.Iteratornot be a subclass ofIterable, so we have iterators that are not iterables. Again, this is just unintuitive, and goes against documentation and people's expectations.iter()to check for the existence of__iter__()would be just an unnecessary step that I guess would make a very critical thing slower, and potentially break existing non-type-checked production code. I'm not a CPython developer, so I don't really know the implications, but they seem bad.This brings me back to my original question: is there some actual real-life non-toy-example problem that this causes? I really don't think we should sacrifice practicality for purity here.
That said, I like the idea of changing the types of
next()andanext(), since their purpose is to just give the next value, and that doesn't need to have anything to do with looping.brettcannon commentedon Sep 13, 2021
@Akuli Your code example isn't getting at the issue ,though, as it isn't the iterable that is lacking
__iter__(), but the iterator:It's what
__iter__returns which can lack__iter__that's the concern.But having typeshed make a call of what should be done is also making a call for nearly all type checkers as they all use typeshed. So making that decision on behalf of all type checkers doesn't seem right to me. Getting iterators to define
__iter__seems like a linter concern more than a type checker concern. And in terms of passing something intoiter()which can't be iterated in aforloop, that's one of the points of type checking and thus should be discoverable if you were to dofor _ in iter(BadIterator()). 😄Akuli commentedon Sep 14, 2021
What linter are you referring to? How would a linter know that something is an "iterator" withoutNevermind. I assume you meant a linter complains when defining a class that has__next__, unless it knows the type of every object, and therefore is actually a type checker?__next__but no__iter__.That's what we do at typeshed. If we have a choice of doing something in typeshed once and for all type checkers, versus having each type checker special-case something (and in practice have most of them forget about it until a user complains), we generally prefer to just do it in typeshed.
Akuli commentedon Sep 14, 2021
So you are saying that it would be preferable to have
next(BadIterator())anditer(BadIterable())succeed at type checking time, as they do at runtime, but haveiter(BadIterator())fail when type checking as it does at runtime? We can do that with overloads:I still can't see why this would be good, but it can be done without destroying working code or people's expectations.
srittau commentedon Sep 14, 2021
Actually it does. It says:
And the linked definition for "iterator" says:
That said, we tend to follow implementation, not documentation in typeshed. Looking at #6035, the change seems quite disruptive, though, so I'm not sure we should do this.
srittau commentedon Sep 14, 2021
I have tried another, promising looking approach in #6035 now: This returns the iter returned from
__iter__()directly, like in the implementation. This allows things like that weren't possible before:primer output also looks promising (except for primer itself 😁).
brettcannon commentedon Sep 14, 2021
@srittau yeah, the docs around all of this are a bit messy. While the definition of an iterator may say that
__iter__()should be defined, that doesn't mean the person linking to the glossary definition fromiter()realized that's what it said.brettcannon commentedon Sep 14, 2021
I opened https://mail.python.org/archives/list/python-dev@python.org/thread/3W7TDX5KNVQVGT5CUHBK33M7VNTP25DZ/ to try and clarify all of this.
srittau commentedon Dec 21, 2021
Fixed in #6035.