Created
March 4, 2023 15:50
-
-
Save ooliver1/3e7607568eecd9082937d09d18163b50 to your computer and use it in GitHub Desktop.
Python callable typehints with defined initial arguments and undefined (...) arguments after.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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