generics are removed from function signatures that are assigned to `Callable` types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die minimalen Beispiele im verlinkten mypy-Playground auszuführen, und vergleiche die angezeigten Typen mit den erwarteten generischen Signaturen. Verfolge die Typinferenz und die Behandlung von Callable/Concatenate in diesen Beispielen; die Aufgabe ist abgeschlossen, wenn das generische T oder Self erhalten bleibt, statt auf Never reduziert zu werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 42/100