Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Always return Iterator, not Iterable for __iter__ #8361

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 1 commit into from
Jul 22, 2022

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jul 22, 2022

After #8358 I went looking for other cases of __iter__ returing Iterable. I found two:

  1. https://github.com/python/cpython/blob/934b25dcc492dcbca4da9d63d0d71dc940fc0375/Lib/wsgiref/validate.py#L216-L221 which clearly is an Iterator
  2. https://github.com/google/python-gflags/blob/4f06c3d0d6cbe9b1fb90ee9fb1c082b3bf9285f6/gflags/flagvalues.py#L666-L667 which is also an Iterator

According to the docs:

object.__iter__(self)
This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container.

Maybe this can be added as a new flake8-pyi rule.

@AlexWaygood
Copy link
Member

Maybe this can be added as a new flake8-pyi rule.

@hauntsaninja's already filed PyCQA/flake8-pyi#247 :)

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks!

@github-actions
Copy link
Contributor

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

@AlexWaygood AlexWaygood merged commit fab1b59 into python:master Jul 22, 2022
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.

2 participants