Skip to content

zip.__iter__ and reversed.__iter__ return self #7123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2022
Merged

Conversation

JelleZijlstra
Copy link
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
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
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
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
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
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