Skip to content

zip.__iter__ and reversed.__iter__ return self#7123

Merged
Akuli merged 2 commits into
masterfrom
JelleZijlstra-patch-1
Feb 5, 2022
Merged

zip.__iter__ and reversed.__iter__ return self#7123
Akuli merged 2 commits into
masterfrom
JelleZijlstra-patch-1

Conversation

@JelleZijlstra
Copy link
Copy Markdown
Member

(On 3.9)

>>> class X(reversed): pass
... 
>>> x = X("abc")
>>> type(x)
<class '__main__.X'>
>>> type(iter(x))
<class '__main__.X'>
>>> class Y(zip):
...     pass
... 
>>> y = Y("abc", "def")
>>> type(y)
<class '__main__.Y'>
>>> type(iter(y))
<class '__main__.Y'>

(On 3.9)
```
>>> class X(reversed): pass
... 
>>> x = X("abc")
>>> type(x)
<class '__main__.X'>
>>> type(iter(x))
<class '__main__.X'>
>>> class Y(zip):
...     pass
... 
>>> y = Y("abc", "def")
>>> type(y)
<class '__main__.Y'>
>>> type(iter(y))
<class '__main__.Y'>
```
@JelleZijlstra
Copy link
Copy Markdown
Member Author

Hold on, this isn't true if you're reversing a list (thanks @AlexWaygood):

% ./python3.10 -c '''class X(reversed): pass           
print(type(iter(X([1, 2, 3]))))'''
<class 'list_reverseiterator'>

@JelleZijlstra JelleZijlstra marked this pull request as draft February 3, 2022 16:00
@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Copy Markdown
Member

I think we should make the change to zip but leave reversed as it is and add a # noqa: Y034 comment. It will be the only Y034 noqa in typeshed.

@JelleZijlstra
Copy link
Copy Markdown
Member Author

I looked into the C code and realized this PR is right after all. The problem isn't that iter(reversed([])) doesn't return a reversed object, it's that reversed([]) doesn't return a reversed object in the first place, but a list_reverseiterator, because list implements the __reversed__ special method. We could model that in typeshed by overloading __new__, but I'd like to defer that until we actually need it.

@JelleZijlstra JelleZijlstra marked this pull request as ready for review February 5, 2022 05:16
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 5, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@AlexWaygood
Copy link
Copy Markdown
Member

I looked into the C code and realized this PR is right after all. The problem isn't that iter(reversed([])) doesn't return a reversed object, it's that reversed([]) doesn't return a reversed object in the first place, but a list_reverseiterator, because list implements the __reversed__ special method. We could model that in typeshed by overloading __new__, but I'd like to defer that until we actually need it.

Huh. Bizarre :)

@Akuli Akuli merged commit c1e3be1 into master Feb 5, 2022
@Akuli Akuli deleted the JelleZijlstra-patch-1 branch February 5, 2022 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants