← Back to DAGDA | Support | Source: https://github.com/python/typeshed/issues/6030
Skip to content

Return type for iter() and aiter() too strict #6030

Closed

Description

@brettcannon

The definition of builtins.iter() has it returning typing.Iterator:

def iter(__iterable: Iterable[_T]) -> Iterator[_T]: ...

The problem is typing.Iterator requires both __iter__ and __next__:

typeshed/stdlib/typing.pyi

Lines 191 to 194 in 4c6e98e

class Iterator(Iterable[_T_co], Protocol[_T_co]):
@abstractmethod
def __next__(self) -> _T_co: ...
def __iter__(self) -> Iterator[_T_co]: ...

The docs for iter() does not suggest that the returned iterator must define __iter__. And if you look at the implementation of iter() -- as well as PyObject_GetIter() and PyIter_Check() which iter() uses -- there is no requirement that the returned iterator define __iter__.

All of this also applies to aiter() in regards to __aiter__.

Activity

  1. brettcannon commented on Sep 13, 2021

    @brettcannon
    MemberAuthor

    I didn't dig into it, but this may also affect the parameters for next() and anext().

  2. JelleZijlstra commented on Sep 13, 2021

    @JelleZijlstra
    Member

    Maybe we should change Iterator instead? Making iter() not return an Iterator sounds very counterintuitive.

  3. srittau commented on Sep 13, 2021

    @srittau
    Collaborator

    Iterator is clearly defined to have both __iter__ and __next__. I think we should just use a custom protocol here.

  4. Akuli commented on Sep 13, 2021

    @Akuli
    Collaborator

    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?

  5. brettcannon commented on Sep 13, 2021

    @brettcannon
    MemberAuthor

    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.

  6. Akuli commented on Sep 13, 2021

    @Akuli
    Collaborator

    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.

    >>> class BadIterator:
    ...     def __next__(self):
    ...             return 1
    ... 
    >>> for x in BadIterator(): print(x)
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'BadIterator' object is not iterable

    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.

  7. brettcannon commented on Sep 13, 2021

    @brettcannon
    MemberAuthor

    @Akuli Your code example isn't getting at the issue ,though, as it isn't the iterable that is lacking __iter__(), but the iterator:

    class Iterator:
        def __init__(self):
            self.items = [1]
    
        # No __iter__.
    
        def __next__(self):
            try:
                return self.items.pop()
            except KeyError:
                raise StopIteration
    
    class Iterable:
        def __iter__(self):
            return Iterator() 
    
    
    for x in Iterable():
        print(x)

    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()). 😄

  8. Akuli commented on Sep 14, 2021

    @Akuli
    Collaborator

    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.

  9. Akuli commented on Sep 14, 2021

    @Akuli
    Collaborator

    for _ in iter(BadIterator())

    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:

    class _SupportsBadIter(Protocol, Generic[_T]):
        def __iter__(self) -> SupportsNext[_T]: ...
    
    @overload
    def iter(__iterable: Iterable[_T]) -> Iterator[_T]: ...
    @overload
    def iter(__iterable: _SupportsBadIter[_T]) -> SupportNext[_T]: ...
    
    def next(__nextable: SupportsNext[_T]) -> _T: ...

    I still can't see why this would be good, but it can be done without destroying working code or people's expectations.

  10. srittau commented on Sep 14, 2021

    @srittau
    Collaborator

    The docs for iter() does not suggest that the returned iterator must define __iter__.

    Actually it does. It says:

    Return an iterator object. [...]

    And the linked definition for "iterator" says:

    [...] 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.

  11. srittau commented on Sep 14, 2021

    @srittau
    Collaborator

    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:

    class Foo:
        def __iter__(self):
            return self
        def bar(self): ...
    
    foo = iter(Foo()).bar()

    primer output also looks promising (except for primer itself 😁).

  12. brettcannon commented on Sep 14, 2021

    @brettcannon
    MemberAuthor

    @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.

  13. brettcannon commented on Sep 14, 2021

    @brettcannon
    MemberAuthor
  14. srittau commented on Dec 21, 2021

    @srittau
    Collaborator

    Fixed in #6035.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions