Skip to content

Commit 5c791bd

Browse files
Remove Any from template annotations. (#343)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
1 parent 24fa8c8 commit 5c791bd

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

aiohttp_jinja2/__init__.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import warnings
44
import jinja2
55
from collections.abc import Mapping
6-
from typing import Any, Awaitable, Callable, Dict, Iterable, Optional, cast
6+
from typing import Any, Awaitable, Callable, Dict, Iterable, Optional, Type, Union, cast, overload
77
from aiohttp import web
88
from aiohttp.abc import AbstractView
99
from .helpers import GLOBAL_HELPERS
@@ -19,6 +19,16 @@
1919
APP_KEY = 'aiohttp_jinja2_environment'
2020
REQUEST_CONTEXT_KEY = 'aiohttp_jinja2_context'
2121

22+
_SimpleHandler = Callable[[web.Request], Awaitable[web.StreamResponse]]
23+
_MethodHandler = Callable[[Any, web.Request], Awaitable[web.StreamResponse]]
24+
_ViewHandler = Callable[[Type[AbstractView]], Awaitable[web.StreamResponse]]
25+
_HandlerType = Union[_SimpleHandler, _MethodHandler, _ViewHandler]
26+
_TemplateReturnType = Awaitable[Union[web.StreamResponse, Mapping[str, Any]]]
27+
_SimpleTemplateHandler = Callable[[web.Request], _TemplateReturnType]
28+
_MethodTemplateHandler = Callable[[Any, web.Request], _TemplateReturnType]
29+
_ViewTemplateHandler = Callable[[AbstractView], _TemplateReturnType]
30+
_TemplateHandler = Union[_SimpleTemplateHandler, _MethodTemplateHandler, _ViewTemplateHandler]
31+
2232

2333
def setup(
2434
app: web.Application,
@@ -56,7 +66,7 @@ def get_env(
5666
def render_string(
5767
template_name: str,
5868
request: web.Request,
59-
context: Dict[str, Any],
69+
context: Mapping[str, Any],
6070
*,
6171
app_key: str = APP_KEY
6272
) -> str:
@@ -87,7 +97,7 @@ def render_string(
8797
def render_template(
8898
template_name: str,
8999
request: web.Request,
90-
context: Dict[str, Any],
100+
context: Mapping[str, Any],
91101
*,
92102
app_key: str = APP_KEY,
93103
encoding: str = 'utf-8',
@@ -109,11 +119,20 @@ def template(
109119
app_key: str = APP_KEY,
110120
encoding: str = 'utf-8',
111121
status: int = 200
112-
) -> Any:
113-
114-
def wrapper(func: Any) -> Any:
122+
) -> Callable[[_TemplateHandler], _HandlerType]:
123+
124+
def wrapper(func: _TemplateHandler) -> _HandlerType:
125+
@overload
126+
async def wrapped(request: web.Request) -> web.StreamResponse:
127+
...
128+
@overload
129+
async def wrapped(view: AbstractView) -> web.StreamResponse:
130+
...
131+
@overload
132+
async def wrapped(_self: Any, request: web.Request) -> web.StreamResponse:
133+
...
115134
@functools.wraps(func)
116-
async def wrapped(*args: Any) -> web.StreamResponse:
135+
async def wrapped(*args):
117136
if asyncio.iscoroutinefunction(func):
118137
coro = func
119138
else:

0 commit comments

Comments
 (0)