Skip to content

Uncaught Error in Assignment of type Iterator[int] to type Iterator[str] #19675

@didi64

Description

@didi64

Bug Report

Mypy allows the assignment
it: Iterator[str] = iter(d) where d is dict[int, str].

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=05c55a654acd175b763be1c73567a107

from typing import Iterator


d: dict[int, str] = {1: 'A'}
it: Iterator[str] = iter(d)  # mypy does not complain, although iter(d) has type Iterator[int]!
s = next(it) + 'foo'  # TypeError: unsupported operand type(s) for +: 'int' and 'str'

Expected Behavior

Mypy raises an error such as
error: Argument 1 to "iter" has incompatible type "dict[int, str]"; expected "SupportsIter[Iterator[str]]"

Actual Behavior

Success: no issues found in 1 source file

Your Environment

mypy Playground

  • Mypy version used: mypy latest 1.17.1
  • Mypy command-line flags: --strict
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-contextType context / bidirectional inference

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions