Skip to content

Commit bdd375a

Browse files
authored
Merge pull request #10 from rhooper/main
add type hints - fixes issue #9
2 parents db0bf16 + a53f585 commit bdd375a

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

adafruit_wsgi/__init__.py

Whitespace-only changes.

adafruit_wsgi/request.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111
"""
1212
import re
1313

14+
try:
15+
from typing import Dict
16+
except ImportError:
17+
pass
18+
1419

1520
class Request:
1621
"""
1722
An incoming HTTP request.
1823
A higher level abstraction of the raw WSGI Environ dictionary.
1924
"""
2025

21-
def __init__(self, environ):
26+
def __init__(self, environ: Dict[str, str]) -> None:
2227
self._method = environ["REQUEST_METHOD"]
2328
self._path = environ["PATH_INFO"]
2429
self._query_params = self.__parse_query_params(environ.get("QUERY_STRING", ""))
@@ -27,29 +32,29 @@ def __init__(self, environ):
2732
self._wsgi_environ = environ
2833

2934
@property
30-
def method(self):
35+
def method(self) -> str:
3136
"""
3237
the HTTP Method Type of this request
3338
"""
3439
return self._method
3540

3641
@property
37-
def path(self):
42+
def path(self) -> str:
3843
"""
3944
the path this request was made to
4045
"""
4146
return self._path
4247

4348
@property
44-
def query_params(self):
49+
def query_params(self) -> Dict[str, str]:
4550
"""
4651
Request query parameters, represented as a dictionary of
4752
param name to param value
4853
"""
4954
return self._query_params
5055

5156
@property
52-
def headers(self):
57+
def headers(self) -> Dict[str, str]:
5358
"""
5459
Request headers, represented as a dictionary of
5560
header name to header value
@@ -64,14 +69,14 @@ def body(self):
6469
return self._body
6570

6671
@property
67-
def wsgi_environ(self):
72+
def wsgi_environ(self) -> Dict[str, str]:
6873
"""
6974
The raw WSGI Environment dictionary representation of the request
7075
"""
7176
return self._wsgi_environ
7277

7378
@staticmethod
74-
def __parse_query_params(query_string):
79+
def __parse_query_params(query_string: str) -> Dict[str, str]:
7580
param_list = query_string.split("&")
7681
params = {}
7782
for param in param_list:
@@ -81,7 +86,7 @@ def __parse_query_params(query_string):
8186
return params
8287

8388
@staticmethod
84-
def __parse_headers(environ):
89+
def __parse_headers(environ: Dict[str, str]) -> Dict[str, str]:
8590
headers = {}
8691

8792
# Content Type and Content Length headers

adafruit_wsgi/wsgi_app.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828

2929
import re
3030

31+
try:
32+
from typing import Any, AnyStr, Callable, Dict, List, Optional, Sequence, Tuple
33+
except ImportError:
34+
pass
35+
3136
from adafruit_wsgi.request import Request
3237

3338
__version__ = "0.0.0-auto.0"
@@ -43,7 +48,7 @@ def __init__(self):
4348
self._routes = []
4449
self._variable_re = re.compile("^<([a-zA-Z]+)>$")
4550

46-
def __call__(self, environ, start_response):
51+
def __call__(self, environ: Dict[str, str], start_response: Callable):
4752
"""
4853
Called whenever the server gets a request.
4954
The environ dict has details about the request per wsgi specification.
@@ -66,7 +71,7 @@ def __call__(self, environ, start_response):
6671
start_response(status, headers)
6772
return resp_data
6873

69-
def on_request(self, methods, rule, request_handler):
74+
def on_request(self, methods: List[str], rule: str, request_handler: Callable):
7075
"""
7176
Register a Request Handler for a particular HTTP method and path.
7277
request_handler will be called whenever a matching HTTP request is received.
@@ -95,7 +100,7 @@ def on_request(self, methods, rule, request_handler):
95100
(re.compile(regex), {"methods": methods, "func": request_handler})
96101
)
97102

98-
def route(self, rule, methods=None):
103+
def route(self, rule: str, methods: Optional[List[str]] = None):
99104
"""
100105
A decorator to register a route rule with an endpoint function.
101106
if no methods are provided, default to GET
@@ -104,9 +109,11 @@ def route(self, rule, methods=None):
104109
methods = ["GET"]
105110
return lambda func: self.on_request(methods, rule, func)
106111

107-
def _match_route(self, path, method):
112+
def _match_route(
113+
self, path: str, method: str
114+
) -> Optional[Tuple[Sequence[AnyStr], Dict[str, Any]]]:
108115
for matcher, route in self._routes:
109116
match = matcher.match(path)
110117
if match and method in route["methods"]:
111-
return (match.groups(), route)
118+
return match.groups(), route
112119
return None

0 commit comments

Comments
 (0)