Skip to content

Conversation

altescy
Copy link
Contributor

@altescy altescy commented Dec 12, 2023

This pull request modifies the type hint for __iter__ to return Iterator instead of Iterable 1.
Mypy raises the error if __iter__ returns Iterable like below:

from typing import Iterable

class Foo:
    def __iter__(self) -> Iterable[str]:
        return iter("abc")

for value in Foo(): # E: "Iterable[str]" has no attribute "__next__"  [attr-defined]
    ...

Footnotes

  1. iterator.__iter__

Copy link

cla-checker-service bot commented Dec 12, 2023

💚 CLA has been signed

@altescy altescy force-pushed the fix-iter-type-hints branch from f03672a to a5d866f Compare December 12, 2023 06:50
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

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

Thanks! Great catch. LGTM.

@pquentin pquentin merged commit 6baac50 into elastic:main Dec 13, 2023
github-actions bot pushed a commit that referenced this pull request Dec 13, 2023
Co-authored-by: Quentin Pradet <[email protected]>
(cherry picked from commit 6baac50)
pquentin added a commit that referenced this pull request Dec 13, 2023
* Fix __iter__ return type to Iterator (#129)

Co-authored-by: Yasuhiro Yamaguchi <[email protected]>
Co-authored-by: Quentin Pradet <[email protected]>
@altescy altescy deleted the fix-iter-type-hints branch December 13, 2023 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants