python / python/mypy

Parametrized type alias union of callables unsubstitued using unannotated functions

Aperta
#14,498 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

Descrizione

Bug Report

A parametrized type alias of union of callable types doesn't substitute the type var parameters properly when using unannotated functions.

To Reproduce

from typing import Union, Callable, TypeVar, List


T = TypeVar("T")
T2 = TypeVar("T2")

F = Union[Callable[[T, int], T2], Callable[[T], T2]]


def foo(l: List[T], f: F[T, T2]) ->  T2:
    ...


reveal_type(foo([1, 2], lambda x: x + 1))  # Revealed type is "Any"
reveal_type(foo([1, 2], lambda x, y: x + y))  # Revealed type is "Any"

Expected Behavior

Both expressions in reveal_type should have builtins.int type as I think it has enough information to solve the type variables there.
As a workaround this works well:

from typing import List, Callable, TypeVar, overload


T = TypeVar("T")
T2 = TypeVar("T2")

@overload
def bar(l: List[T], f: Callable[[T, int], T2]) ->  T2:
    ...

@overload
def bar(l: List[T], f: Callable[[T], T2]) ->  T2:
    ...

def bar(l, f):
    ...

reveal_type(bar([1, 2], lambda x: x + 1))  # Revealed type is "builtins.int"
reveal_type(bar([1, 2], lambda x, y: x + y))  # Revealed type is "builtins.int"

But in my understanding I would expect the previous code to work the same

It also works fine with type annotated function:

from typing import Union, Callable, TypeVar, List


T = TypeVar("T")
T2 = TypeVar("T2")

F = Union[Callable[[T, int], T2], Callable[[T], T2]]


def foo(l: List[T], f: F[T, T2]) ->  T2:
    ...

def bar(x: int) -> int:
    ...

reveal_type(foo([1, 2], bar))  # Revealed type is "builtins.int"

Actual Behavior

Mypy doesn't substitute the type variables properly and reveal the type as Any

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11

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

Riproduci il problema con gli esempi forniti in Python 3.11 usando mypy 0.991 e conferma che entrambe le chiamate a lambda non annotate rivelano Any invece di int. Traccia la sostituzione della variabile di tipo per l’unione parametrizzata di tipi Callable, quindi aggiungi una copertura di regressione che mostri che entrambe le chiamate inferiscono builtins.int senza modificare il caso della funzione annotata.

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.