Skip to content

Commit 312f350

Browse files
committed
Apply type hinting for the actual logic
1 parent a9fa9ea commit 312f350

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-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',

0 commit comments

Comments
 (0)