Skip to content
Discussion options

You must be logged in to vote

Does this meet your needs?

from typing import Callable, Generic, ParamSpec, Protocol, TypeVar, overload

SelfT = TypeVar("SelfT", contravariant=True)
P = ParamSpec("P")
R = TypeVar("R", covariant=True)
S = TypeVar("S")

class MethodProto(Protocol[SelfT, P, R]):
    def __call__(_self, self: SelfT, *args: P.args, **kwargs: P.kwargs) -> R:
        ...

class MethodType_(Generic[SelfT, P, R]):
    __self__: SelfT
    __func__: Callable[P, R]

    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...

    @overload
    def __get__(self, __instance: None, __type: type) -> "MethodProto[SelfT, P, R]": ...
    @overload
    def __get__(self: Callable[P, R], __instance: S, __type: type[S]) 

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@bzoracler
Comment options

@erictraut
Comment options

Answer selected by bzoracler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants