Similar to Python type hint Callable with one known positional type and then *args and **kwargs, I want to type hint a Callable
for which is known:
- It must have at least 1 positional input.
- The first positional input must be
int
. - It must return
None
.
Apart from that, any signature is valid. I tried to do the following, but it doesn't work. So, is it possible to do it in python 3.10/3.11 at all?
from typing import TypeAlias, ParamSpec, Concatenate, Callable
P = ParamSpec("P")
intfun: TypeAlias = Callable[Concatenate[int, P], None]
def foo(i: int) -> None:
pass
a: intfun = foo # ✘ Incompatible types in assignment
# expression has type "Callable[[int], None]",
# variable has type "Callable[[int, VarArg(Any), KwArg(Any)], None]")
https://mypy-play.net/?mypy=latest&python=3.11&gist=f4c26907bfc0ae0118b90c1fa5a79fe8
I am using mypy==1.0.0
.
Context: I want to type hint a dict
hat holds key-value pairs where the value could be any Callable
satisfying properties 1,2,3.