python / python/mypy

(inter-)dependent variable types

Offen
#7,422 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature needs discussion priority-2-low
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

I'm not sure how to entitle this, but I think it's a dependent type.

  • Are you reporting a bug, or opening a feature request? yes
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Tuple, Union

def x() -> Union[Tuple[int, int], Tuple[str, str]]:
    ...

a, b = x()

if isinstance(b, int):
    reveal_type(a) # can only be int but is reported as Union[int, str]

  • What is the actual behavior/output?
    type is reported as Union[int, str]
  • What is the behavior/output you expect?
    type of a can only be int given the return type signature
  • What are the versions of mypy and Python you are using? 7.20
    Do you see the same issue after installing mypy from Git master? yes
  • What are the mypy flags you are using? (For example --strict-optional)
    python_version=3.7

I'm assuming this would be a difficult change, but I figured I'd open an issue for the sake of having a record, and in the meantime I can use assert isinstance()

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

Beginne mit der minimalen Python-Reproduktion im Issue und überprüfe die aktuelle reveal_type-Ausgabe unter der angegebenen Python-3.7-Konfiguration. Die Änderung wäre abgeschlossen, wenn das Einschränken von b auf int auch a auf int einschränkt, während der entsprechende str-Fall und das bestehende Verhalten für nicht verwandte Unions erhalten bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

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