python / python/mypy

Can't infer T where Optional[T] is Optional[MyUnion]

Aperta
#6,751 5 commenti 5 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug false-positive priority-1-normal topic-type-variables topic-union-types
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

  • Are you reporting a bug, or opening a feature request?

Bug

  • 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 Optional, Union, TypeVar

MyUnion = Union[int, str]
T = TypeVar("T")


def not_none(x: Optional[T]) -> T:
    assert x is not None
    return x


# Works
def f(i: Optional[int]) -> int:
    return not_none(i)


# Doesn't work
def g(u: Optional[MyUnion]) -> MyUnion:
    return not_none(u)
  • What is the actual behavior/output?

a.py:19: error: Incompatible return value type (got "object", expected "Union[int, str]")

  • What is the behavior/output you expect?

No error.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

mypy --version is 0.710+dev.70a4c9858fb3e736dbf4bc6cc9243c5d49bf2e5a
py --version is Python 3.7.2
Still see the issue after installing from master.

  • What are the mypy flags you are using? (For example --strict-optional)

None

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 eseguendo mypy sul repro minimo in a.py e conferma che f() abbia esito positivo, mentre g() segnali object invece di Union[int, str]. Traccia l’inferenza dei tipi per not_none(Optional[MyUnion]); il lavoro è completato quando il repro non produce errori e conserva il tipo restituito previsto.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.