Skip to content
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

Fix Any inference when unpacking iterators that don't directly inherit from typing.Iterator #14821

Merged
merged 2 commits into from
Mar 3, 2023

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Mar 2, 2023

Fixes #14819.

Mypy currently silently infers an Any type when unpacking an iterator that doesn't explicitly inherit from typing.Iterator (i.e., an iterator that's a structural subtype of typing.Iterator, but not a nominal subtype):

from typing import TypeVar

T = TypeVar("T")

class Foo:
    count: int
    def __init__(self) -> None:
        self.count = 0
    def __iter__(self: T) -> T:
        return self
    def __next__(self) -> int:
        self.count += 1
        if self.count > 3:
            raise StopIteration
        return self.count

a, b, c = Foo()
reveal_type(a)  # note: Revealed type is "Any"

However, we have enough information here to infer that the type of a should really be int. This PR fixes that bug.

There's discussion on the issue thread about an alternative solution that would involve changing some mypy behaviour that's been established for around 10 years. For now, I haven't gone for that solution.

…on't directly inherit from `typing.Iterator`
@AlexWaygood AlexWaygood changed the title Fix Any inference when unpacking iterators that return self and don't directly inherit from typing.Iterator Fix Any inference when unpacking iterators that don't directly inherit from typing.Iterator Mar 2, 2023
@github-actions

This comment has been minimized.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Nice to see this ad-hoc code removed!

@@ -380,6 +380,8 @@ class Nums(Iterable[int]):
def __iter__(self): pass
def __next__(self): pass
a, b = Nums()
reveal_type(a) # N: Revealed type is "builtins.int"
Copy link
Member

Choose a reason for hiding this comment

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

The other option here is to infer Any, because of def __iter__(self): pass

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, but as discussed in the issue, two long-standing tests fail if I make that change, so I'd rather not make that behaviour change in this PR 👍

test-data/unit/check-inference.test Outdated Show resolved Hide resolved
@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2023

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

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks!

@JukkaL JukkaL merged commit 456dcbd into python:master Mar 3, 2023
@AlexWaygood AlexWaygood deleted the self-iter-unpack branch March 3, 2023 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants