Hello!
I’m using Sanic with custom generics, and this works fine:
app = Sanic[MyConfig, AppCtx](
'test',
ctx=AppCtx(),
config=MyConfig(),
request_class=Request[Sanic[Config, SimpleNamespace], SimpleNamespace]
)
However, when I try to use a custom request context that inherits from SimpleNamespace
, I get a type error:
class RequestCtx(SimpleNamespace):
pass
app = Sanic[MyConfig, AppCtx](
'test',
ctx=AppCtx(),
config=MyConfig(),
request_class=Request[Sanic[Config, SimpleNamespace], RequestCtx]
)
The error from mypy
is:
Argument “request_class” to “Sanic” has incompatible type “type[Request[Sanic[Config, SimpleNamespace], RequestCtx]]”; expected “type[Request[Sanic[Config, SimpleNamespace], SimpleNamespace]] | None”
This happens because the second generic parameter ctx_type
in the Request
class does not accept covariant subtypes — even though RequestCtx
is clearly a subtype of SimpleNamespace
.
Is this behavior expected? Or is it something that could/should be relaxed in Sanic’s generic typing?
Thanks!