Skip to content

Instantly share code, notes, and snippets.

@AlexWaygood
Created May 21, 2025 18:53
Show Gist options
  • Save AlexWaygood/250fb97a731b162f2ab57f6156d1954a to your computer and use it in GitHub Desktop.
Save AlexWaygood/250fb97a731b162f2ab57f6156d1954a to your computer and use it in GitHub Desktop.
`functools.partial` typing
import sys
from types import GenericAlias
from typing import Any, Callable, Concatenate, overload
class partial[**P1, **P2, T, R, *Ts]:
@overload
def __new__(cls, func: Callable[P1, R], /) -> partial[P1, P1, Any, R]: ...
@overload
def __new__(cls, func: Callable[Concatenate[*Ts, P2], R], /, *args: *Ts) -> partial[Concatenate[*Ts, P2], P2, Any, R, *Ts]: ...
@overload
def __new__(cls, func: Callable[P1, R], /, *args: *Ts, **kwargs: T) -> partial[P1, ..., T, R, *Ts]: ...
def __call__(self, *args: P2.args, **kwargs: P2.kwargs) -> R: ...
@property
def func(self) -> Callable[P1, R]: ...
@property
def args(self) -> tuple[*Ts]: ...
@property
def keywords(self) -> dict[str, T]: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment