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

added typing to h11_impl.py module #1017

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
155 changes: 152 additions & 3 deletions uvicorn/_types.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import sys
from typing import Dict, Iterable, Optional, Tuple, Union
from typing import Awaitable, Callable, Dict, Iterable, Optional, Tuple, Type, Union

if sys.version_info < (3, 8):
from typing_extensions import Literal, TypedDict
from typing_extensions import Literal, Protocol, TypedDict
else:
from typing import Literal, TypedDict
from typing import Literal, Protocol, TypedDict


class ASGISpecInfo(TypedDict):
Expand Down Expand Up @@ -65,3 +65,152 @@ class WebsocketScope(TypedDict):

WWWScope = Union[HTTPScope, WebsocketScope]
Scope = Union[HTTPScope, WebsocketScope, LifespanScope]


class HTTPRequestEvent(TypedDict):
type: Literal["http.request"]
body: bytes
more_body: bool


class HTTPResponseStartEvent(TypedDict):
type: Literal["http.response.start"]
status: int
headers: Iterable[Tuple[bytes, bytes]]


class HTTPResponseBodyEvent(TypedDict):
type: Literal["http.response.body"]
body: bytes
more_body: bool


class HTTPServerPushEvent(TypedDict):
type: Literal["http.response.push"]
path: str
headers: Iterable[Tuple[bytes, bytes]]


class HTTPDisconnectEvent(TypedDict):
type: Literal["http.disconnect"]


class WebsocketConnectEvent(TypedDict):
type: Literal["websocket.connect"]


class WebsocketAcceptEvent(TypedDict):
type: Literal["websocket.accept"]
subprotocol: Optional[str]
headers: Iterable[Tuple[bytes, bytes]]


class WebsocketReceiveEvent(TypedDict):
type: Literal["websocket.receive"]
bytes: Optional[bytes]
text: Optional[str]


class WebsocketSendEvent(TypedDict):
type: Literal["websocket.send"]
bytes: Optional[bytes]
text: Optional[str]


class WebsocketResponseStartEvent(TypedDict):
type: Literal["websocket.http.response.start"]
status: int
headers: Iterable[Tuple[bytes, bytes]]


class WebsocketResponseBodyEvent(TypedDict):
type: Literal["websocket.http.response.body"]
body: bytes
more_body: bool


class WebsocketDisconnectEvent(TypedDict):
type: Literal["websocket.disconnect"]
code: int


class WebsocketCloseEvent(TypedDict):
type: Literal["websocket.close"]
code: int
reason: Optional[str]


class LifespanStartupEvent(TypedDict):
type: Literal["lifespan.startup"]


class LifespanShutdownEvent(TypedDict):
type: Literal["lifespan.shutdown"]


class LifespanStartupCompleteEvent(TypedDict):
type: Literal["lifespan.startup.complete"]


class LifespanStartupFailedEvent(TypedDict):
type: Literal["lifespan.startup.failed"]
message: str


class LifespanShutdownCompleteEvent(TypedDict):
type: Literal["lifespan.shutdown.complete"]


class LifespanShutdownFailedEvent(TypedDict):
type: Literal["lifespan.shutdown.failed"]
message: str


ASGIReceiveEvent = Union[
HTTPRequestEvent,
HTTPDisconnectEvent,
WebsocketConnectEvent,
WebsocketReceiveEvent,
WebsocketDisconnectEvent,
LifespanStartupEvent,
LifespanShutdownEvent,
]


ASGISendEvent = Union[
HTTPResponseStartEvent,
HTTPResponseBodyEvent,
HTTPServerPushEvent,
HTTPDisconnectEvent,
WebsocketAcceptEvent,
WebsocketSendEvent,
WebsocketResponseStartEvent,
WebsocketResponseBodyEvent,
WebsocketCloseEvent,
LifespanStartupCompleteEvent,
LifespanStartupFailedEvent,
LifespanShutdownCompleteEvent,
LifespanShutdownFailedEvent,
]


ASGIReceiveCallable = Callable[[], Awaitable[ASGIReceiveEvent]]
ASGISendCallable = Callable[[ASGISendEvent], Awaitable[None]]


class ASGI2Protocol(Protocol):
def __init__(self, scope: Scope) -> None:
...

async def __call__(
self, receive: ASGIReceiveCallable, send: ASGISendCallable
) -> None:
...


ASGI2Application = Type[ASGI2Protocol]
ASGI3Application = Callable[
[Scope, ASGIReceiveCallable, ASGISendCallable],
Awaitable[None],
]
ASGIApplication = Union[ASGI2Application, ASGI3Application]