Annotate URLPath protocol parameter with Literal#3285
Conversation
Replace the plain `str` annotation on `URLPath.__new__` and `URLPath.__init__` with a `ASGIProtocol` Literal type (`"http" | "websocket" | ""`), so static type checkers can catch invalid values at analysis time. The runtime `assert` is kept as a safety net.
|
Where is empty string passed? |
|
The empty string "" is the default value for the protocol parameter. It's used when callers omit protocol, for example in Mount.url_path_for and Host.url_path_for: starlette/starlette/routing.py Lines 420 to 427 in 348f86d starlette/starlette/routing.py Lines 486 to 492 in 348f86d In these cases the protocol is unknown at that point β it gets resolved later when a child route (like Route or WebSocketRoute) provides "http" or "websocket". |
|
Kludex
left a comment
There was a problem hiding this comment.
I don't think there's much benefit from this, but I guess it's fine.
|
Thanks anyway. π |
ASGIProtocol = Literal["http", "websocket", ""]type alias instarlette/types.py.strannotation onURLPath.__new__andURLPath.__init__'sprotocolparameter withASGIProtocol, so static type checkers can catch invalid values at analysis time.assertis kept as a safety net for dynamic usage.