python / python/mypy

`dict(...)` hides incompatible assignment when source has `Any` key type

Aperta
#10,530 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

Using dict(...) around an otherwise incompatible mapping structure leads mypy to omit errors about the type incompatibility. My original code called dict on a collections.defaultdict, though that doesn't seem to be required to reproduce this.

To Reproduce

from typing import Any, Dict

foo: Dict[str, str]
bar: Dict[Any, int] = {}
foo = bar  # Incompatible types in assignment (expected, correct)
foo = dict(bar)  # no error (unexpected, IMO incorrect)

This seems to be related to the fact that the key type is Any. Changing the key to int causes an error on both assignment lines (even if the value type is Any).

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: none needed, though notable that --strict doesn't affect this
  • Python version used: 3.7.10
  • Operating system and version: Ubuntu 20.04

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

Nel report non sono indicati né un file sorgente né un test. Inizia eseguendo la riproduzione fornita con mypy 0.812, quindi traccia il modo in cui dict(...) inferisce i mapping quando il tipo della chiave sorgente è Any. Aggiungi un test di regressione che copra l'assegnazione segnalata e verifica che l'assegnazione incompatibile venga diagnosticata.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.