Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect type annotations for signal handlers #5621

Closed
9999years opened this issue Jun 11, 2021 · 0 comments · Fixed by #5622
Closed

Incorrect type annotations for signal handlers #5621

9999years opened this issue Jun 11, 2021 · 0 comments · Fixed by #5622

Comments

@9999years
Copy link
Contributor

In signal.pyi:83, signal handlers are typed as:

_HANDLER = Union[Callable[[Signals, FrameType], Any], int, Handlers, None]

But I think this might be more accurate:

_HANDLER = Union[Callable[[int, Optional[FrameType]], Any], int, Handlers, None]

The documentation for signal.signal points out that the current annotation might be wrong (emphasis my own):

The handler is called with two arguments: the signal number and the current stack frame (None or a frame object; for a description of frame objects, see the description in the type hierarchy or see the attribute descriptions in the inspect module).

And when we use them, we can see that the signal number is passed as an int, not a signal.Signals member:

import signal

def handler(signal_number, frame):
    print("In signal handler!")
    print("Signal number:", signal_number, type(signal_number))
    print("Stack frame:  ", frame, type(frame))

# Set signal handler:
signal.signal(signal.SIGHUP, handler)

# Use it:
signal.raise_signal(signal.SIGHUP)

Which prints:

In signal handler!
Signal number: 1 <class 'int'>
Stack frame:   <frame at 0x7f804402abe0, file '<stdin>', line 12, code <module>> <class 'frame'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant