What is signal type? #1042
-
The code is to exit a program when ^C is pressed. How to type this code?
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
In typeshed, Your function is passed as a You are passing in the function, so we can ignore everything except the import typing
from types import FrameType
def signal_handler(signal: int, frame: Optional[FrameType]) -> None:
print('program terminated') |
Beta Was this translation helpful? Give feedback.
-
Thank-you. My first time here: So, is process to check typeshed first? Secondly, are questions like this for General Discussion or Q&A? |
Beta Was this translation helpful? Give feedback.
In typeshed,
stdlib/signal.pyi
definessignal.signal()
here: https://github.com/python/typeshed/blob/3a22bf2411c7357bffb2fe1dc675486a8da97177/stdlib/signal.pyi#L68Your function is passed as a
_HANDLER
, and the definition of_HANDLER
just above is:Union[Callable[[int, Optional[FrameType]], Any], int, Handlers, None]
You are passing in the function, so we can ignore everything except the
Callable
, and your function should matchCallable[[int, Optional[FrameType]], Any]
. This means a function that takes anint
as first argument, andOptional[FrameType]
as second argument, and can return anything it wants.