Type mismatch a2wsgi.asgi_typing.*Scope and starlette.types.Scope
mvdbeek opened this issue · comments
Hey there, we're using a2wsgi and it helped us a lot, but with starlette 0.34.0 we've started seeing
lib/galaxy/webapps/galaxy/fast_app.py:184: error: Argument 2 to "mount" of "Starlette" has
incompatible type "WSGIMiddleware"; expected
"Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]]"
[arg-type]
app.mount("/", wsgi_handler)
^~~~~~~~~~~~
lib/galaxy/webapps/galaxy/fast_app.py:184: note: "WSGIMiddleware.__call__" has type "Callable[[Arg(HTTPScope | WebSocketScope | LifespanScope, 'scope'), Arg(Callable[[], Awaitable[HTTPRequestEvent | HTTPDisconnectEvent | WebSocketConnectEvent | WebSocketReceiveEvent | WebSocketDisconnectEvent | LifespanStartupEvent | LifespanShutdownEvent]], 'receive'), Arg(Callable[[HTTPResponseStartEvent | HTTPResponseBodyEvent | HTTPDisconnectEvent | WebSocketAcceptEvent | WebSocketSendEvent | WebSocketCloseEvent | LifespanStartupCompleteEvent | LifespanStartupFailedEvent | LifespanShutdownCompleteEvent | LifespanShutdownFailedEvent], Awaitable[None]], 'send')], Coroutine[Any, Any, None]]"
I believe this is because a2wsgi uses TypedDict
while starlette uses MutableMapping, which are not compatible. I'm not sure what the solution would be, maybe this is something I should raise at starlette, but it seems like one could in principle add arbitrary keys which would make TypedDict
the wrong choice ?
It's neither Starlette nor a2wsgi's problem, it's because Python currently doesn't handle extra keyword arguments gracefully. You can use type: ignore to work around it. The reason a2wsgi has detailed type annotations is that a2wsgi is supposed to work between frameworks that conform to those specifications. Detailed type annotations help to check for compatibility issues. As for web frameworks, they may prefer the looser MutableMapping since users tend to pass many non-spec keys and values.