python / python/mypy

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

Aperta
#19,675 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-type-context
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il riproduttore collegato di mypy-play.net e l’espressione iter(d), usando mypy 1.17.1 con --strict su Python 3.12. Traccia il modo in cui il type checker inferisce il tipo dell’iteratore per un dict[int, str] e aggiungi un test di regressione che mostri che l’assegnazione a Iterator[str] viene rifiutata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.