How to specify ParamSpec
signature in assert_type
?
#1465
Answered
by
Daverball
randolf-scholz
asked this question in
Q&A
-
I am writing some type-hints for pytorch. The P = ParamSpec("P")
@overload
def script( # type: ignore[misc]
obj: Callable[P, R],
optimize: Optional[bool] = None,
_frames_up: int = 0,
_rcb: Optional[ResolutionCallback] = None,
example_inputs: Union[List[Tuple], Dict[Callable, List[Tuple]], None] = None,
) -> ScriptFunction[P, R]: and class ScriptFunction(Callable[P, R]):
def __call__(self, *args: P.Args, **kwargs: P.kwargs) -> R: ... Now, I want to add a test: assert_type(jit.script(nn.functional.relu), Callable[[Tensor, bool], Tensor])
assert_type(jit.script(nn.functional.relu), ScriptFunction[[Tensor, bool], Tensor]) which results in
How can I specify the correct signature in |
Beta Was this translation helpful? Give feedback.
Answered by
Daverball
Sep 8, 2023
Replies: 1 comment 1 reply
-
The best I could come up with is relu: Callable[[Tensor, bool], Tensor] = nn.functional.relu # forget argument names
assert_type(jit.script(relu), ScriptFunction[[Tensor, bool], Tensor]) There really ought to be a way to reference the signature of another callable, like: relu_siganture = signature[relu.__call__]
assert_type(jit.script(relu), ScriptFunction[relu_signature.params, relu_signature.return]) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The point of
assert_type
is to be as strict as possible and only allow exact matches, your first statement already sort of does the check you want to make, since a static type checker would complain if the signature wasn't compatible with the positional only version.I agree that it would be nice to have a way to specify all the different kinds of arguments in a ParamSpec and mypy has an old unsupported representation of those arguments, however it will not allow them to be used in ParamSpec, even if it still sort of generates them and uses them internally.
I think the following is perfectly fine for testing what you wanted to test. It won't catch
Any
, so it's not a perfect replacement for…