Skip to content

Commit 926a8f5

Browse files
Kludexeuri10
andauthored
♻️ Replace _types module by asgiref.typing (encode#1044)
* ♻️ Replace _types module by asgiref.typing * ♻️ Use explicit hinting instead of TypeDict constructor * Update uvicorn/lifespan/on.py Co-authored-by: euri10 <[email protected]> Co-authored-by: euri10 <[email protected]>
1 parent d28f683 commit 926a8f5

File tree

5 files changed

+32
-74
lines changed

5 files changed

+32
-74
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def get_packages(package):
4444
env_marker_below_38 = "python_version < '3.8'"
4545

4646
minimal_requirements = [
47+
"asgiref>=3.3.4",
4748
"click>=7.*",
4849
"h11>=0.8",
4950
"typing-extensions;" + env_marker_below_38,

uvicorn/_types.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

uvicorn/config.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
import socket
88
import ssl
99
import sys
10-
from typing import List, Tuple
10+
from typing import List, Tuple, Union
11+
12+
if sys.version_info < (3, 8):
13+
from typing_extensions import Literal
14+
else:
15+
from typing import Literal
1116

1217
import click
1318

@@ -219,7 +224,7 @@ def __init__(
219224
self.forwarded_allow_ips = forwarded_allow_ips
220225

221226
@property
222-
def asgi_version(self) -> str:
227+
def asgi_version(self) -> Union[Literal["2.0"], Literal["3.0"]]:
223228
return {"asgi2": "2.0", "asgi3": "3.0", "wsgi": "3.0"}[self.interface]
224229

225230
@property

uvicorn/lifespan/on.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
import asyncio
22
import logging
33
from asyncio import Queue
4+
from typing import Union
5+
6+
from asgiref.typing import (
7+
LifespanScope,
8+
LifespanShutdownCompleteEvent,
9+
LifespanShutdownEvent,
10+
LifespanShutdownFailedEvent,
11+
LifespanStartupCompleteEvent,
12+
LifespanStartupEvent,
13+
LifespanStartupFailedEvent,
14+
)
415

516
from uvicorn import Config
6-
from uvicorn._types import LifespanReceiveMessage, LifespanScope, LifespanSendMessage
17+
18+
LifespanReceiveMessage = Union[LifespanStartupEvent, LifespanShutdownEvent]
19+
LifespanSendMessage = Union[
20+
LifespanStartupFailedEvent,
21+
LifespanShutdownFailedEvent,
22+
LifespanStartupCompleteEvent,
23+
LifespanShutdownCompleteEvent,
24+
]
725

826
STATE_TRANSITION_ERROR = "Got invalid state transition on lifespan protocol."
927

@@ -30,8 +48,8 @@ async def startup(self) -> None:
3048
main_lifespan_task = loop.create_task(self.main()) # noqa: F841
3149
# Keep a hard reference to prevent garbage collection
3250
# See https://github.com/encode/uvicorn/pull/972
33-
34-
await self.receive_queue.put({"type": "lifespan.startup"})
51+
startup_event: LifespanStartupEvent = {"type": "lifespan.startup"}
52+
await self.receive_queue.put(startup_event)
3553
await self.startup_event.wait()
3654

3755
if self.startup_failed or (self.error_occured and self.config.lifespan == "on"):
@@ -44,7 +62,8 @@ async def shutdown(self) -> None:
4462
if self.error_occured:
4563
return
4664
self.logger.info("Waiting for application shutdown.")
47-
await self.receive_queue.put({"type": "lifespan.shutdown"})
65+
shutdown_event: LifespanShutdownEvent = {"type": "lifespan.shutdown"}
66+
await self.receive_queue.put(shutdown_event)
4867
await self.shutdown_event.wait()
4968

5069
if self.shutdown_failed or (

uvicorn/protocols/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import urllib
33
from typing import Optional, Tuple
44

5-
from uvicorn._types import WWWScope
5+
from asgiref.typing import WWWScope
66

77

88
def get_remote_addr(transport: asyncio.Transport) -> Optional[Tuple[str, int]]:

0 commit comments

Comments
 (0)