Skip to content

Commit fcce6a7

Browse files
authored
Add type annotations (#285)
1 parent 386ea1e commit fcce6a7

File tree

7 files changed

+67
-21
lines changed

7 files changed

+67
-21
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ coverage/
5858
.pytest_cache
5959

6060
# code editors
61-
.vscode
61+
.vscode
62+
63+
.mypy_cache

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ include CHANGES.txt
22
include LICENSE
33
include README.rst
44
include Makefile
5+
include requirements-dev.txt
6+
include pytest.ini
7+
include .coveragerc
58
graft aiohttp_jinja2
69
graft docs
710
graft examples

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
flake:
44
flake8 aiohttp_jinja2 tests
5+
mypy --strict aiohttp_jinja2
56

67
test: flake
78
py.test -s ./tests/

aiohttp_jinja2/__init__.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import functools
33
import warnings
44
import jinja2
5-
from collections import Mapping
5+
from collections.abc import Mapping
6+
from typing import Any, Awaitable, Callable, Dict, Iterable, Optional, cast
67
from aiohttp import web
78
from aiohttp.abc import AbstractView
89
from .helpers import GLOBAL_HELPERS
@@ -18,10 +19,17 @@
1819
REQUEST_CONTEXT_KEY = 'aiohttp_jinja2_context'
1920

2021

21-
def setup(app, *args, app_key=APP_KEY, context_processors=(),
22-
filters=None, default_helpers=True, autoescape=True,
23-
**kwargs):
24-
env = jinja2.Environment(*args, autoescape=autoescape, **kwargs)
22+
def setup(
23+
app: web.Application,
24+
*args: Any,
25+
app_key: str = APP_KEY,
26+
context_processors: Iterable[Callable[[web.Request], Dict[str, Any]]] = (),
27+
filters: Optional[Iterable[Callable[..., str]]] = None,
28+
default_helpers: bool = True,
29+
**kwargs: Any
30+
) -> jinja2.Environment:
31+
kwargs.setdefault("autoescape", True)
32+
env = jinja2.Environment(*args, **kwargs)
2533
if default_helpers:
2634
env.globals.update(GLOBAL_HELPERS)
2735
if filters is not None:
@@ -36,11 +44,21 @@ def setup(app, *args, app_key=APP_KEY, context_processors=(),
3644
return env
3745

3846

39-
def get_env(app, *, app_key=APP_KEY):
40-
return app.get(app_key)
47+
def get_env(
48+
app: web.Application,
49+
*,
50+
app_key: str = APP_KEY
51+
) -> jinja2.Environment:
52+
return cast(jinja2.Environment, app.get(app_key))
4153

4254

43-
def render_string(template_name, request, context, *, app_key=APP_KEY):
55+
def render_string(
56+
template_name: str,
57+
request: web.Request,
58+
context: Dict[str, Any],
59+
*,
60+
app_key: str = APP_KEY
61+
) -> str:
4462
env = request.config_dict.get(app_key)
4563
if env is None:
4664
text = ("Template engine is not initialized, "
@@ -65,8 +83,15 @@ def render_string(template_name, request, context, *, app_key=APP_KEY):
6583
return text
6684

6785

68-
def render_template(template_name, request, context, *,
69-
app_key=APP_KEY, encoding='utf-8', status=200):
86+
def render_template(
87+
template_name: str,
88+
request: web.Request,
89+
context: Dict[str, Any],
90+
*,
91+
app_key: str = APP_KEY,
92+
encoding: str = 'utf-8',
93+
status: int = 200
94+
) -> web.Response:
7095
response = web.Response(status=status)
7196
if context is None:
7297
context = {}
@@ -77,11 +102,17 @@ def render_template(template_name, request, context, *,
77102
return response
78103

79104

80-
def template(template_name, *, app_key=APP_KEY, encoding='utf-8', status=200):
105+
def template(
106+
template_name: str,
107+
*,
108+
app_key: str = APP_KEY,
109+
encoding: str = 'utf-8',
110+
status: int = 200
111+
) -> Any:
81112

82-
def wrapper(func):
113+
def wrapper(func: Any) -> Any:
83114
@functools.wraps(func)
84-
async def wrapped(*args):
115+
async def wrapped(*args: Any) -> web.StreamResponse:
85116
if asyncio.iscoroutinefunction(func):
86117
coro = func
87118
else:
@@ -107,7 +138,10 @@ async def wrapped(*args):
107138

108139

109140
@web.middleware
110-
async def context_processors_middleware(request, handler):
141+
async def context_processors_middleware(
142+
request: web.Request,
143+
handler: Callable[[web.Request], Awaitable[web.StreamResponse]]
144+
) -> web.StreamResponse:
111145

112146
if REQUEST_CONTEXT_KEY not in request:
113147
request[REQUEST_CONTEXT_KEY] = {}
@@ -116,5 +150,5 @@ async def context_processors_middleware(request, handler):
116150
return await handler(request)
117151

118152

119-
async def request_processor(request):
153+
async def request_processor(request: web.Request) -> Dict[str, web.Request]:
120154
return {'request': request}

aiohttp_jinja2/helpers.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
useful context functions, see
33
http://jinja.pocoo.org/docs/dev/api/#jinja2.contextfunction
44
"""
5+
from typing import Any, Dict, cast
6+
57
import jinja2
8+
from aiohttp import web
9+
from yarl import URL
610

711

8-
@jinja2.contextfunction
9-
def url_for(context, __route_name, **parts):
12+
@jinja2.contextfunction # type: ignore
13+
def url_for(context: Dict[str, Any], __route_name: str, **parts: Any) -> URL:
1014
"""Filter for generating urls.
1115
1216
Usage: {{ url('the-view-name') }} might become "/path/to/view" or
1317
{{ url('item-details', id=123, query={'active': 'true'}) }}
1418
might become "/items/1?active=true".
1519
"""
16-
app = context['app']
20+
app = cast(web.Application, context['app'])
1721

1822
query = None
1923
if 'query_' in parts:
@@ -39,8 +43,8 @@ def url_for(context, __route_name, **parts):
3943
return url
4044

4145

42-
@jinja2.contextfunction
43-
def static_url(context, static_file_path):
46+
@jinja2.contextfunction # type: ignore
47+
def static_url(context: Dict[str, Any], static_file_path: str) -> str:
4448
"""Filter for generating urls for static files.
4549
4650
NOTE: you'll need

aiohttp_jinja2/py.typed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# marker

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ yarl==1.3.0
1111
multidict==4.5.2
1212
pytest-aiohttp==0.3.0
1313
pytest-sugar==0.9.2
14+
mypy==0.740
1415
-e .

0 commit comments

Comments
 (0)