python / python/mypy

Return type hint fails with multiple supplied type vars

Aperta
#14,714 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-join-v-union
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

I'm not 100% sure how to word this but mypy fails to detect the proper return type if multiple types are supplied. It works just fine if only a single type is specified.

This scenario works just fine with pyright/pylance.

To Reproduce

import typing as t


class BaseClass:
    ...


class Sub1(BaseClass):
    ...


class Sub2(BaseClass):
    ...


ClassType = t.TypeVar("ClassType", bound=BaseClass)


def testing_union() -> t.Union[Sub1, Sub2]:
    res = sub_method(Sub1, Sub2)
    reveal_type(res)

    return res


def testing_single() -> Sub1:
    res = sub_method(Sub1)

    return res


def sub_method(*output_type: t.Type[ClassType]) -> ClassType:
    return  # type: ignore[return-value]

Expected Behavior

I expect mypy to work for both testing_union() and testing_single().

Actual Behavior

A return-value occurs in testing_union as it thinks the value is the bound base class. Using reveal_type we can see that mypy detects the type as BaseClass whereas pylance/pyright detects it as the expect3ed Sub1 | Sub2 type.

image

The output from the mypy check is

mypy-test.py:21: note: Revealed type is "mypy-test.BaseClass"
mypy-test.py:23: error: Incompatible return value type (got "BaseClass", expected "Union[Sub1, Sub2]")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.0.0 (compiled: yes)
  • Mypy command-line flags: python -m mypy file.py
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: Python 3.11.0

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 il reproducer fornito con mypy 1.0.0 e confronta il tipo rivelato per più argomenti di tipo con quello per un singolo argomento. Traccia il comportamento dell’inferenza dei tipi per gli argomenti variadici TypeVar di sub_method; il lavoro è completato quando il caso di unione rivela Sub1 | Sub2 e supera il controllo del tipo restituito dichiarato senza indebolire il caso con un singolo tipo.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.