Skip to content

Instantly share code, notes, and snippets.

@ooliver1
Created March 4, 2023 15:50
Show Gist options
  • Save ooliver1/3e7607568eecd9082937d09d18163b50 to your computer and use it in GitHub Desktop.
Save ooliver1/3e7607568eecd9082937d09d18163b50 to your computer and use it in GitHub Desktop.
Python callable typehints with defined initial arguments and undefined (...) arguments after.
from collections.abc import Callable
from typing import Any, Concatenate, ParamSpec, TypeVar
from typing_extensions import reveal_type
P = ParamSpec("P")
T = TypeVar("T")
Call = Callable[P, Any]
CallEllipsis = Call[...] # `...` can be assigned to `P`
reveal_type(CallEllipsis) # (...) -> Any
Call2 = Callable[Concatenate[int, P], Any]
Call2Ellipsis = Call2[...] # `...` can be assigned to `P` still
reveal_type(Call2Ellipsis) # (int, ...) -> Any
Call3 = Callable[Concatenate[T, P], Any]
Call3Ellipsis = Call3[T, ...] # `T` assigned to `T` as passthrough, `...` assigned to `P`
reveal_type(Call3Ellipsis) # (T@Call3Ellipsis, ...) -> Any
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment