Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Any from template annotations. #343

Merged
merged 6 commits into from
Oct 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions aiohttp_jinja2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import warnings
import jinja2
from collections.abc import Mapping
from typing import Any, Awaitable, Callable, Dict, Iterable, Optional, cast
from typing import Any, Awaitable, Callable, Dict, Iterable, Optional, Type, Union, cast, overload
from aiohttp import web
from aiohttp.abc import AbstractView
from .helpers import GLOBAL_HELPERS
Expand All @@ -18,6 +18,16 @@
APP_KEY = 'aiohttp_jinja2_environment'
REQUEST_CONTEXT_KEY = 'aiohttp_jinja2_context'

_SimpleHandler = Callable[[web.Request], Awaitable[web.StreamResponse]]
_MethodHandler = Callable[[Any, web.Request], Awaitable[web.StreamResponse]]
_ViewHandler = Callable[[Type[AbstractView]], Awaitable[web.StreamResponse]]
_HandlerType = Union[_SimpleHandler, _MethodHandler, _ViewHandler]
_TemplateReturnType = Awaitable[Union[web.StreamResponse, Mapping[str, Any]]]
_SimpleTemplateHandler = Callable[[web.Request], _TemplateReturnType]
_MethodTemplateHandler = Callable[[Any, web.Request], _TemplateReturnType]
_ViewTemplateHandler = Callable[[AbstractView], _TemplateReturnType]
_TemplateHandler = Union[_SimpleTemplateHandler, _MethodTemplateHandler, _ViewTemplateHandler]


def setup(
app: web.Application,
Expand Down Expand Up @@ -55,7 +65,7 @@ def get_env(
def render_string(
template_name: str,
request: web.Request,
context: Dict[str, Any],
context: Mapping[str, Any],
*,
app_key: str = APP_KEY
) -> str:
Expand Down Expand Up @@ -86,7 +96,7 @@ def render_string(
def render_template(
template_name: str,
request: web.Request,
context: Dict[str, Any],
context: Mapping[str, Any],
*,
app_key: str = APP_KEY,
encoding: str = 'utf-8',
Expand All @@ -108,11 +118,20 @@ def template(
app_key: str = APP_KEY,
encoding: str = 'utf-8',
status: int = 200
) -> Any:

def wrapper(func: Any) -> Any:
) -> Callable[[_TemplateHandler], _HandlerType]:

def wrapper(func: _TemplateHandler) -> _HandlerType:
@overload
async def wrapped(request: web.Request) -> web.StreamResponse:
...
@overload
async def wrapped(view: AbstractView) -> web.StreamResponse:
...
@overload
async def wrapped(_self: Any, request: web.Request) -> web.StreamResponse:
...
@functools.wraps(func)
async def wrapped(*args: Any) -> web.StreamResponse:
async def wrapped(*args):
if asyncio.iscoroutinefunction(func):
coro = func
else:
Expand Down