Skip to content

Commit 14cb546

Browse files
committed
Improve use of Literal type for ASGI interfaces
https://mypy.readthedocs.io/en/stable/literal_types.html This commit will retain the use of literals to define ASGI protocol versions, but improve and correct the use of literal types. As described in the mypy docs, `Literal["2.0", "3.0"]` is a simpler way to write `Union[Literal["2.0"], Literal["3.0"]]`.
1 parent 9c8e70d commit 14cb546

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

uvicorn/config.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def __init__(
146146
log_level: Optional[Union[str, int]] = None,
147147
access_log: bool = True,
148148
use_colors: Optional[bool] = None,
149-
interface: str = "auto",
149+
interface: Literal["auto", "asgi3", "asgi2", "wsgi"] = "auto",
150150
debug: bool = False,
151151
reload: bool = False,
152152
reload_dirs: Optional[List[str]] = None,
@@ -237,8 +237,13 @@ def __init__(
237237
self.forwarded_allow_ips = forwarded_allow_ips
238238

239239
@property
240-
def asgi_version(self) -> str:
241-
return {"asgi2": "2.0", "asgi3": "3.0", "wsgi": "3.0"}[self.interface]
240+
def asgi_version(self) -> Literal["2.0", "3.0"]:
241+
mapping: Dict[str, Literal["2.0", "3.0"]] = {
242+
"asgi2": "2.0",
243+
"asgi3": "3.0",
244+
"wsgi": "3.0",
245+
}
246+
return mapping[self.interface]
242247

243248
@property
244249
def is_ssl(self) -> bool:

0 commit comments

Comments
 (0)