python / python/mypy

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

Offen
#10,530 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Im Bericht wird keine Quelldatei und kein Test genannt. Führe zunächst die bereitgestellte Reproduktion mit mypy 0.812 aus und verfolge dann, wie dict(...) Zuordnungen ableitet, wenn der Typ des Quellschlüssels Any ist. Füge einen Regressionstest hinzu, der die gemeldete Zuweisung abdeckt, und überprüfe, dass die inkompatible Zuweisung diagnostiziert wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.