When making a dynamic member descriptor that can also behave as a Wrapper Why Does Self Not get deleted from ParamSpec?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
I've bumped into one of the largest and most frustrating puzzles I've ever encountered but I wanted to understand what the thought process behind ParamSpec was for class functions because I think if Self was ignored from ParamSpec dynamically based on weather or not a class function was in use it would not only fix lru_cache but it would also allow async_lru library to be type-hinted as well. No matter what I do, nothing works. I've tried multiple search engines to no avail so I go here now as my final resort since it's not a mypy problem because pyright is also plagued by the same rules.
from typing import ParamSpec, TypeVar, Callable, Protocol
P = ParamSpec("P")
T = TypeVar("T")
I = TypeVar("I")
# I made a class of my own for callable to illistrate the problem at hand.
class CallableProto(Protocol[P, T]):
def __call__(self, *args:P.args, **kwds:P.kwargs) -> T:...
class RespectingCallable(CallableProto[P, T]):
def __init__(self, func:Callable[P, T]):
self.func = func
# My question is Why does Self inside of a class function still get preserved in
# ParamSpec when it's just a class function? Shouldn't it get removed?
# Also Concatenate doesn't work I've tried that one already with no success...
def __get__(self, instance:I, obj:type[I]) -> CallableProto[P, T]:...
class A:
def __init__(self) -> None:
pass
@RespectingCallable
def coro(self, val:int) -> str:
return f"{val}"
g = A()
# It appears as (self: A, val: int) -> str instead of (val: int) -> str
# (mypy and pyright are also both are plagued by these rules)
g.coro(1)
@RespectingCallable
def func(i:int) -> str:
return f"{i}"
# Somehow this is fine?
func(1)
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
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
Beginnen Sie mit dem bereitgestellten Python-Beispiel und überprüfen Sie die Regeln für ParamSpec, Self, Concatenate und die Bindung von Deskriptoren. Vergleichen Sie, wie mypy und pyright das Beispiel interpretieren, und bestimmen Sie anschließend, ob das beabsichtigte Ergebnis eine Klarstellung der Spezifikation oder eine Dokumentationsänderung erfordert; als abgeschlossen gilt die Aufgabe, wenn das Verhalten und die erwarteten Annotationen eindeutig festgelegt sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- developer-experience
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100