Skip to content

Typing information lost while calling __iter__Β #12446

@JulienPalard

Description

@JulienPalard

With the following example:

from typing import Literal

dest: Literal["tata", "tutu"]

for src in "tata", "tutu":
    dest = src

I'm getting:

error: Incompatible types in assignment (expression has type "str", variable has type "Literal['http', 'https']")

Trying to dig with reveal_type:

t = "tata", "tutu"
reveal_type(t)
reveal_type(t.__iter__())

I get:

test.py:2: note: Revealed type is "Tuple[builtins.str, builtins.str]"
test.py:3: note: Revealed type is "typing.Iterator[builtins.str*]"

So it looks OK, but digging with pdb, in mypy/checker.py:analyze_iterable_item_type(), after calling:

iterator = echk.check_method_call_by_name('__iter__', iterable, [], [], expr)[0]

I see:

iterable == Tuple[Literal['foo']?, Literal['bar']?]
iterator == typing.Iterator[builtins.str*])

I think some information is lost here, should iterator get: typing.Iterator[Literal['foo', 'bar']] instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotationstopic-literal-types

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions