Skip to content

threading: ExceptHookArgs funkiness #4767

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

Closed
bluetech opened this issue Nov 13, 2020 · 2 comments · Fixed by #4768
Closed

threading: ExceptHookArgs funkiness #4767

bluetech opened this issue Nov 13, 2020 · 2 comments · Fixed by #4768

Comments

@bluetech
Copy link
Contributor

The threading.excepthook variable (added in Python 3.8) accepts a threading.ExceptHookArgs argument (available at runtime). The typing around it in typeshed is not straightforward:

if sys.version_info >= (3, 8):
import _thread
# don't ask...
_ExceptHookArgs = _thread.ExceptHookArgs
ExceptHookArgs = _thread._ExceptHookArgs
excepthook: Callable[[_ExceptHookArgs], Any]

Sorry but I guess I have to ask 😀

The _thread part is:

if sys.version_info >= (3, 8):
def get_native_id() -> int: ... # only available on some platforms
class ExceptHookArgs(NamedTuple):
exc_type: Type[BaseException]
exc_value: Optional[BaseException]
exc_traceback: Optional[TracebackType]
thread: Optional[Thread]
def _ExceptHookArgs(args: Any) -> ExceptHookArgs: ...
_excepthook: Callable[[ExceptHookArgs], Any]

My practical issue is that this:

import threading

def my_hook(args: threading.ExceptHookArgs) -> None:
    ...

results in

Function "_thread._ExceptHookArgs" is not valid as a type  [valid-type]
@hauntsaninja
Copy link
Collaborator

I believe this goes back to #3358 , cc @srittau

@srittau
Copy link
Collaborator

srittau commented Nov 13, 2020

What I a useful comment I left there. The stubs try to follow the implementation (starting here) and I guess I wanted to express my consternation about the weird renaming going on there. That said, I don't think the stubs are accurate. _threading contains the namedtuple _ExceptHookArgs (although the docstrings refer to it as ExceptHookArgs). This gets imported into typing.pyi as ExceptHookArgs` (making the docstrings correct).

(In case _thread is not available, threading does some weird things, but I think we should ignore that.)

What we should probably do:

  • _thread.pyi:
    • Remove _ExceptHookArgs.
    • Rename ExceptHookArgs to _ExceptHookArgs.
    • Change the argument type of _excepthook to _ExceptHookArgs.
  • threading.pyi:
    • Remove the import of _thread.ExceptHookArgs.
    • Change the argument type of excepthook to _ExceptHookArgs`.

bluetech added a commit to bluetech/typeshed that referenced this issue Nov 14, 2020
The previous typing meant `threading.ExceptHookArgs` could not be used
to type a value.

The new typing follows what cpython does in the happy path (`_thread`
exists rather than the pure-python fallback being used).

Fixes python#4767.
bluetech added a commit to bluetech/typeshed that referenced this issue Nov 14, 2020
The previous typing meant `threading.ExceptHookArgs` could not be used
to type a value.

The new typing follows what cpython does in the happy path (`_thread`
exists rather than the pure-python fallback being used).

Fixes python#4767.
srittau pushed a commit that referenced this issue Nov 14, 2020
The previous typing meant `threading.ExceptHookArgs` could not be used
to type a value.

The new typing follows what cpython does in the happy path (`_thread`
exists rather than the pure-python fallback being used).

Fixes #4767.
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.

3 participants