Skip to content

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

Merged
JukkaL merged 2 commits into
python:masterfrom
AlexWaygood:self-iter-unpack
Mar 3, 2023
Merged

JukkaL merged 2 commits into
python:masterfrom
AlexWaygood:self-iter-unpack

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Mar 2, 2023 •

Copy link
Copy Markdown
Member

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.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

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!

def __iter__(self): pass
def __next__(self): pass
a, b = Nums()
reveal_type(a) # N: Revealed type is "builtins.int"

Copy link
Copy Markdown
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
Copy Markdown
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 👍

Comment thread test-data/unit/check-inference.test Outdated
@github-actions

github-actions Bot commented Mar 3, 2023

Copy link
Copy Markdown
Contributor

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

@AlexWaygood
AlexWaygood requested a review from sobolevn March 3, 2023 10:58

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

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

Development

Successfully merging this pull request may close these issues.

Unpacking an iterable with an __iter__ method that returns self leads to variable being silently inferred as Any

3 participants