generics are removed from function signatures that are assigned to `Callable` types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
from typing import TypeVar, Callable
T = TypeVar("T")
def asdf(fn: Callable[[], T]) -> Callable[[], T]: ...
@asdf
def foo() -> list[T]: ...
reveal_type(foo) # "def () -> list[Never]", should be "def [T] () -> list[T]"
use case
i'm making a @NoInstance decorator that bans usages of classmethods on instances:
from typing import TypeVar, ParamSpec, Concatenate, Callable, Generic, overload, Never
out_T = TypeVar("out_T", covariant=True)
P = ParamSpec("P")
out_R = TypeVar("out_R", covariant=True)
class NoInstance(Generic[P, out_R]):
def __init__(self, function: Callable[Concatenate[type[out_T], P], out_R]):
...
@overload
def __get__(self, instance: None, owner: type[object]) -> Callable[P, out_R]:
...
@overload
def __get__(self, instance: object, owner: type[object]) -> Never:
...
def __get__(self, instance: object, owner: type[object]) -> object:
...
class Foo:
@NoInstance
@classmethod
def foo(cls):
...
Foo.foo() # allowed
Foo().foo() # not allowed
but the primary use case for class methods that should not be called on instances are constructor methods, which means the resulting signature only has the typevar on the return type (in this case, Self):
class Foo:
@NoInstance
@classmethod
def from_int(cls) -> Self:
...
reveal_type(Foo.from_int) # `"def () -> Never"`, because the `Self` generic was removed by the `Concatenate`. should be `def () -> Foo` instead
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo gli esempi minimi nel mypy playground collegato e confronta i tipi rivelati con le firme generiche previste. Traccia l'inferenza dei tipi e la gestione di Callable/Concatenate coinvolte in questi esempi; il lavoro è completo quando il T generico o Self viene preservato invece di essere ridotto a Never.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 42/100