Add custom typing to config and ctx (#2785)
This commit is contained in:
10
tests/typing/samples/app_custom_config.py
Normal file
10
tests/typing/samples/app_custom_config.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from sanic import Sanic
|
||||
from sanic.config import Config
|
||||
|
||||
|
||||
class CustomConfig(Config):
|
||||
pass
|
||||
|
||||
|
||||
app = Sanic("test", config=CustomConfig())
|
||||
reveal_type(app)
|
||||
9
tests/typing/samples/app_custom_ctx.py
Normal file
9
tests/typing/samples/app_custom_ctx.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from sanic import Sanic
|
||||
|
||||
|
||||
class Foo:
|
||||
pass
|
||||
|
||||
|
||||
app = Sanic("test", ctx=Foo())
|
||||
reveal_type(app)
|
||||
5
tests/typing/samples/app_default.py
Normal file
5
tests/typing/samples/app_default.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from sanic import Sanic
|
||||
|
||||
|
||||
app = Sanic("test")
|
||||
reveal_type(app)
|
||||
14
tests/typing/samples/app_fully_custom.py
Normal file
14
tests/typing/samples/app_fully_custom.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from sanic import Sanic
|
||||
from sanic.config import Config
|
||||
|
||||
|
||||
class CustomConfig(Config):
|
||||
pass
|
||||
|
||||
|
||||
class Foo:
|
||||
pass
|
||||
|
||||
|
||||
app = Sanic("test", config=CustomConfig(), ctx=Foo())
|
||||
reveal_type(app)
|
||||
17
tests/typing/samples/request_custom_ctx.py
Normal file
17
tests/typing/samples/request_custom_ctx.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from sanic import Request, Sanic
|
||||
from sanic.config import Config
|
||||
|
||||
|
||||
class Foo:
|
||||
pass
|
||||
|
||||
|
||||
app = Sanic("test")
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def handler(request: Request[Sanic[Config, SimpleNamespace], Foo]):
|
||||
reveal_type(request.ctx)
|
||||
reveal_type(request.app)
|
||||
19
tests/typing/samples/request_custom_sanic.py
Normal file
19
tests/typing/samples/request_custom_sanic.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from sanic import Request, Sanic
|
||||
from sanic.config import Config
|
||||
|
||||
|
||||
class CustomConfig(Config):
|
||||
pass
|
||||
|
||||
|
||||
app = Sanic("test", config=CustomConfig())
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def handler(
|
||||
request: Request[Sanic[CustomConfig, SimpleNamespace], SimpleNamespace]
|
||||
):
|
||||
reveal_type(request.ctx)
|
||||
reveal_type(request.app)
|
||||
34
tests/typing/samples/request_fully_custom.py
Normal file
34
tests/typing/samples/request_fully_custom.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from sanic import Request, Sanic
|
||||
from sanic.config import Config
|
||||
|
||||
|
||||
class CustomConfig(Config):
|
||||
pass
|
||||
|
||||
|
||||
class Foo:
|
||||
pass
|
||||
|
||||
|
||||
class RequestContext:
|
||||
foo: Foo
|
||||
|
||||
|
||||
class CustomRequest(Request[Sanic[CustomConfig, Foo], RequestContext]):
|
||||
@staticmethod
|
||||
def make_context() -> RequestContext:
|
||||
ctx = RequestContext()
|
||||
ctx.foo = Foo()
|
||||
return ctx
|
||||
|
||||
|
||||
app = Sanic(
|
||||
"test", config=CustomConfig(), ctx=Foo(), request_class=CustomRequest
|
||||
)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def handler(request: CustomRequest):
|
||||
reveal_type(request)
|
||||
reveal_type(request.ctx)
|
||||
reveal_type(request.app)
|
||||
Reference in New Issue
Block a user