Skip to content

Commit adf4c70

Browse files
committed
Change: add typehint for api/auth, app, scripts{apply_ramdom, draw}
1 parent 6f066c1 commit adf4c70

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

api/auth.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import json
77
from api.time_management import get_current_datetime
88
from api.error import error_response
9+
# typehints imports {{{
10+
from typehint import Optional, Tuple, Callable, Dict
11+
from flask import Response
12+
# }}}
913

1014

1115
class UserNotFoundError(Exception):
@@ -55,18 +59,17 @@ def decrypt_token(token: str) -> dict:
5559
return json.loads(decrypted.decode())
5660

5761

58-
# TODO: is this returns anything?
59-
def login_required(*required_authority: str):
62+
def login_required(*required_authority: str) -> Callable:
6063
"""
6164
a decorder to require login
6265
Args:
6366
*required_authority (str): required authorities
6467
if this is blank, no requirement of authority
6568
"""
66-
def login_required_impl(f):
69+
def login_required_impl(f: Callable) -> Callable:
6770
@wraps(f)
68-
def decorated_function(*args, **kwargs):
69-
def auth_error(code, headm=None):
71+
def decorated_function(*args: Tuple, **kwargs: Dict) -> Callable:
72+
def auth_error(code: int, headm: Optional[str]=None) -> Tuple[Response , int]:
7073
resp, http_code = error_response(code)
7174
if headm:
7275
resp.headers['WWW-Authenticate'] = 'Bearer ' + headm

app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
app = create_app()
44

55

6+
# TODO: I don't know whether this function returns anything
67
@app.cli.command("initdb")
78
def initdb_():
89
initdb()
910

1011

12+
# TODO: I don't know whether this function returns anything
1113
@app.cli.command("generate")
1214
def generate_():
1315
generate()

scripts/apply_ramdom.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
from pathlib import Path
99
import json
1010

11+
# typehints imports {{{
12+
from typehint import Dict, Optional
13+
# }}}
14+
1115

1216
class client():
13-
def get(self, _url, _json=None, follow_redirects=False, headers=None):
17+
def get(self, _url: str, _json: Optional[Dict]=None, follow_redirects: bool=False, headers: Optional[Dict]=None) -> Optional[Dict]:
1418
default_headers = {"Content-Type": "application/json"}
1519
if headers:
1620
default_headers.update(headers)
@@ -29,7 +33,7 @@ def get(self, _url, _json=None, follow_redirects=False, headers=None):
2933
response.close()
3034
return json.loads(response_body)
3135

32-
def post(self, _url, _json=None, follow_redirects=False, headers=None):
36+
def post(self, _url: str, _json: Optional[Dict]=None, follow_redirects: bool=False, headers: Optional[Dict]=None) -> Optional[Dict]:
3337
default_headers = {"Content-Type": "application/json"}
3438
if headers:
3539
default_headers.update(headers)
@@ -49,7 +53,7 @@ def post(self, _url, _json=None, follow_redirects=False, headers=None):
4953
return json.loads(response_body)
5054

5155

52-
def login(client, secret_id, rresp):
56+
def login(client: client, secret_id: int, rresp: str) -> Optional[Dict]:
5357
return client.post('/auth', _json={
5458
"id": secret_id,
5559
"g-recaptcha-response": rresp

scripts/draw.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from urllib.request import Request, urlopen
1313
from urllib.error import HTTPError
1414
import hashlib
15+
# typehints imports {{{
16+
from typehint import Dict, Optional
17+
# }}}
1518

1619
parser = argparse.ArgumentParser(
1720
description='Draw currently available lotteries')
@@ -32,7 +35,7 @@
3235
admin_ids = next(cred for cred in id_list if cred['authority'] == 'admin')
3336

3437

35-
def post_json(path, data=None, token=None):
38+
def post_json(path: str, data: Optional[Dict]=None, token: Optional[str]=None) -> Optional[Dict]:
3639
headers = {"Content-Type": "application/json"}
3740
if token:
3841
headers['Authorization'] = 'Bearer ' + token

0 commit comments

Comments
 (0)