Uncaught Error in Assignment of type Iterator[int] to type Iterator[str]
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked mypy-play.net reproducer and the iter(d) expression, using mypy 1.17.1 with --strict on Python 3.12. Trace how the type checker infers the iterator type for a dict[int, str] and add a regression test showing that assignment to Iterator[str] is rejected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100