Skip to content

Commit 7c54e88

Browse files
authored
Merge pull request #7 from studio3104/typehinting
Apply type hints
2 parents ca6c01a + 312f350 commit 7c54e88

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

pytest_chalice/fixtures.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# -*- coding: utf-8 -*-
22

3+
from typing import Iterator
4+
35
import pytest
46

7+
from chalice import Chalice
8+
59
from .handlers import RequestHandler
610

711

812
@pytest.fixture
913
def client(app):
14+
# type: (Chalice) -> Iterator[Chalice]
1015
yield RequestHandler(app)

pytest_chalice/handlers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# -*- coding: utf-8 -*-
22

3+
from typing import Any, Callable, Dict
4+
35
import json
46
from logging import getLogger
57
import re
68

9+
from chalice import Chalice
710
from chalice.config import Config
811
from chalice.local import LocalGateway
912

@@ -16,18 +19,22 @@
1619

1720
class InternalLocalGateway(LocalGateway):
1821
def __init__(self, *args, **kwargs):
22+
# type: (Any, Any) -> None
1923
self.custom_context = {}
2024
super(InternalLocalGateway, self).__init__(*args, **kwargs)
2125

2226
@property
2327
def custom_context(self):
28+
# type: () -> Dict[str, Any]
2429
return self.__custom_context
2530

2631
@custom_context.setter
2732
def custom_context(self, context):
33+
# type: (Dict[str, Any]) -> None
2834
self.__custom_context = context
2935

3036
def _generate_lambda_event(self, *args, **kwargs):
37+
# type: (Any, Any) -> Dict[str, Any]
3138
event = super(InternalLocalGateway, self)._generate_lambda_event(*args, **kwargs)
3239
event['requestContext'].update(self.custom_context)
3340
return event
@@ -38,6 +45,7 @@ def _generate_lambda_event(self, *args, **kwargs):
3845

3946
class ResponseHandler(object):
4047
def __init__(self, values):
48+
# type: (Dict[str, Any]) -> None
4149
self.values = {}
4250

4351
for key, value in values.items():
@@ -46,10 +54,11 @@ def __init__(self, values):
4654

4755
try:
4856
self.values['json'] = json.loads(self.values['body'])
49-
except JSONDecodeError:
57+
except JSONDecodeError: # type: ignore
5058
logger.info('Response body is NOT JSON decodable: {}'.format(self.values['body']))
5159

5260
def __getattr__(self, key):
61+
# type: (str) -> Any
5362
try:
5463
return self.values[key]
5564
except KeyError:
@@ -60,10 +69,12 @@ class RequestHandler(object):
6069
METHODS = ('get', 'head', 'post', 'options', 'put', 'delete', 'trace', 'patch', 'link', 'unlink')
6170

6271
def __init__(self, app):
72+
# type: (Chalice) -> None
6373
self.local_gateway = InternalLocalGateway(app, Config())
6474

6575
@property
6676
def custom_context(self):
77+
# type: () -> Dict[str, Any]
6778
return self.local_gateway.custom_context
6879

6980
# As of Chalice version 1.8.0,
@@ -75,13 +86,16 @@ def custom_context(self):
7586
# Not only for this purpose, it's an interface provided to allow custom contexts in unit tests.
7687
@custom_context.setter
7788
def custom_context(self, context):
89+
# type: (Dict[str, Any]) -> None
7890
self.local_gateway.custom_context = context
7991

8092
def __getattr__(self, method):
93+
# type: (str) -> Callable
8194
if method not in self.METHODS:
8295
raise AttributeError("'{}' object has no attribute '{}'".format(self.__class__.__name__, method))
8396

8497
def request(path, headers={}, body=''):
98+
# type: (str, Dict[str, str], str) -> ResponseHandler
8599
response = self.local_gateway.handle_request(method=method.upper(), path=path, headers=headers, body=body)
86100
return ResponseHandler(response)
87101

pytest_chalice/plugin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# -*- coding: utf-8 -*-
22

3+
from _pytest.config import Config
4+
35
from .fixtures import client # noqa
46

57

68
def pytest_configure(config):
9+
# type: (Config) -> None
710
config.addinivalue_line(
811
'markers',
912
'app: A Chalice application object',

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
from typing import Any, Dict, Iterator, Union
4+
35
import pytest
46

57
from chalice import Chalice
@@ -9,19 +11,23 @@
911

1012
@pytest.fixture
1113
def app():
14+
# type: () -> Iterator[Union[Iterator, Iterator[Chalice]]]
1215
app = Chalice(__name__)
1316

1417
@app.route('/', methods=('GET', 'HEAD', 'POST', 'OPTIONS', 'PUT', 'DELETE', 'TRACE', 'PATCH', 'LINK', 'UNLINK'))
1518
def index():
19+
# type: () -> Dict[str, str]
1620
return {'hello': 'world'}
1721

1822
@app.route('/context')
1923
def context():
24+
# type: () -> Dict[str, Dict[str, Any]]
2025
context = app.current_request.context
2126
return {'context': context}
2227

2328
@app.route('/string')
2429
def string():
30+
# type: () -> str
2531
return 'Foo'
2632

2733
yield app

tests/test_chalice.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import pytest
4+
from pytest_chalice.handlers import RequestHandler
45

56

67
class TestRequest:
@@ -13,16 +14,19 @@ def test_json_response(self, method, client):
1314
assert response.json == {'hello': 'world'}
1415

1516
def test_invalid_method(self, client):
17+
# type: (RequestHandler) -> None
1618
with pytest.raises(AttributeError, match=r' object has no attribute '):
1719
client.invalid_method('/')
1820

1921
def test_string_response_dont_have_json_attribute(self, client):
22+
# type: (RequestHandler) -> None
2023
response = client.get('/string')
2124
assert not hasattr(response, 'json')
2225

2326

2427
class TestCustomContext:
2528
def test_check_default_context(self, client):
29+
# type: (RequestHandler) -> None
2630
response = client.get('/context')
2731
assert response.json == {
2832
'context': {
@@ -34,6 +38,7 @@ def test_check_default_context(self, client):
3438
}
3539

3640
def test_custom_context(self, client):
41+
# type: (RequestHandler) -> None
3742
client.custom_context = {
3843
'authorizer': {'claims': {}},
3944
}

0 commit comments

Comments
 (0)