-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
refine asyncio call_exception_handler context type #6981
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
refine asyncio call_exception_handler context type #6981
Conversation
I don't think we're ready to use PEP 655 in typeshed yet, unfortunately. #6301. |
This comment has been minimized.
This comment has been minimized.
@JelleZijlstra looks like we can use intermediate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's see how much I can do in review suggestions
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Oops, I made pytype unhappy:
But I |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for more TypedDicts. A few things I noticed below.
class _BaseContext(TypedDict): | ||
message: str | ||
|
||
class _Context(_BaseContext, total=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking forward to PEP 655.
default_exception_handler()
also adds a handle_trackback
field to the context:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And Handle._run()
adds source_traceback
under some conditions:
_Context = dict[str, Any] | ||
|
||
class _BaseContext(TypedDict): | ||
message: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really required? At least default_exception_handler()
uses .get()
to access it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is quite disruptive and I don't think we should merge. Because this type doesn't exist at runtime, I think it means in practice that any time you're not passing a dict literal, you'll get a type error, with no good way to refer to the type.
This no longer breaks pytype, so I've removed the "deferred" label. We now just need to decide whether it's a good idea or not. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: anyio (https://github.com/agronholm/anyio)
+ src/anyio/_backends/_asyncio.py:1910: error: Argument 1 to "set_exception_handler" of "AbstractEventLoop" has incompatible type "Callable[[AbstractEventLoop, Dict[str, Any]], None]"; expected "Optional[Callable[[AbstractEventLoop, _Context], Any]]" [arg-type]
aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/connector.py:108: error: Argument 1 to "call_exception_handler" of "AbstractEventLoop" has incompatible type "Dict[str, object]"; expected "_Context" [arg-type]
+ aiohttp/connector.py:258: error: Argument 1 to "call_exception_handler" of "AbstractEventLoop" has incompatible type "Dict[str, object]"; expected "_Context" [arg-type]
+ aiohttp/client.py:306: error: Argument 1 to "call_exception_handler" of "AbstractEventLoop" has incompatible type "Dict[str, object]"; expected "_Context" [arg-type]
+ aiohttp/web.py:455: error: Incompatible types (expression has type "Optional[BaseException]", TypedDict item "exception" has type "BaseException") [typeddict-item]
core (https://github.com/home-assistant/core)
+ homeassistant/runner.py:67: error: Argument 1 to "set_exception_handler" of "AbstractEventLoop" has incompatible type "Callable[[Any, Dict[str, Any]], None]"; expected "Optional[Callable[[AbstractEventLoop, _Context], Any]]" [arg-type]
+ homeassistant/runner.py:156: error: Incompatible types (expression has type "Optional[BaseException]", TypedDict item "exception" has type "BaseException") [typeddict-item]
boostedblob (https://github.com/hauntsaninja/boostedblob)
+ boostedblob/globals.py:231: error: Argument 1 to "default_exception_handler" of "AbstractEventLoop" has incompatible type "Dict[str, Any]"; expected "_Context"
+ boostedblob/globals.py:233: error: Argument 1 to "set_exception_handler" of "AbstractEventLoop" has incompatible type "Callable[[AbstractEventLoop, Dict[str, Any]], None]"; expected "Optional[Callable[[AbstractEventLoop, _Context], Any]]"
aioredis (https://github.com/aio-libs/aioredis)
+ aioredis/client.py:1076: error: Argument 1 to "call_exception_handler" of "AbstractEventLoop" has incompatible type "Dict[str, object]"; expected "_Context" [arg-type]
python-chess (https://github.com/niklasf/python-chess)
+ chess/engine.py:1261: error: Incompatible types (expression has type "Optional[SubprocessTransport]", TypedDict item "transport" has type "BaseTransport")
dragonchain (https://github.com/dragonchain/dragonchain)
+ dragonchain/broadcast_processor/broadcast_processor.py:367:42: error: Argument 1 to "set_exception_handler" of "AbstractEventLoop" has incompatible type "Callable[[AbstractEventLoop, Dict[Any, Any]], None]"; expected "Optional[Callable[[AbstractEventLoop, _Context], Any]]"
+ dragonchain/contract_invoker/contract_invoker.py:137:42: error: Argument 1 to "set_exception_handler" of "AbstractEventLoop" has incompatible type "Callable[[AbstractEventLoop, Dict[Any, Any]], None]"; expected "Optional[Callable[[AbstractEventLoop, _Context], Any]]"
|
I'm closing this PR: as @hauntsaninja says, this would be quite disruptive, and it would be complicated trying to access this The |
No description provided.