Skip to content

Instantly share code, notes, and snippets.

@flying-sheep
Last active December 16, 2024 16:19
Show Gist options
  • Save flying-sheep/a057ebf61f9082d524f6f678fbb18fdc to your computer and use it in GitHub Desktop.
Save flying-sheep/a057ebf61f9082d524f6f678fbb18fdc to your computer and use it in GitHub Desktop.
Jupyter reply types
MT = TypeVar("MT", bound=LiteralString)
C = TypeVar("C", bound="_ContentOkBase")
class _ReplyBase(TypedDict, Generic[MT, C]):
"""Jupyter reply message."""
header: ReplyHeader[MT]
content: C | ContentError | ContentAbort
class ReplyHeader(TypedDict, Generic[MT]):
"""Jupyter reply message header."""
msg_type: MT
class _ContentOkBase(TypedDict):
"""Jupyter successful message reply content."""
status: Literal["ok"]
class ContentAbort(TypedDict):
"""Jupyter abort message reply content."""
status: Literal["abort"]
class ContentError(TypedDict):
"""Jupyter error message reply content."""
status: Literal["error"]
ename: str
evalue: str
traceback: list[str]
class ExecuteReplyContent(_ContentOkBase):
execution_count: int
ExecuteReply = _ReplyBase[Literal["execute_result"], ExecuteReplyContent]
class StreamContent(_ContentOkBase):
name: Literal["stdout", "stderr"]
text: str
class DisplayDataContent(_ContentOkBase):
data: dict[str, str]
metadata: dict[str, str]
class ExecuteResultContent(DisplayDataContent):
execution_count: int
Stream = _ReplyBase[Literal["stream"], StreamContent]
DisplayData = _ReplyBase[Literal["display_data"], DisplayDataContent]
ExecuteResult = _ReplyBase[Literal["execute_result"], ExecuteResultContent]
IoPubMsg = Stream | DisplayData | ExecuteResult
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment