Skip to content

Commit

Permalink
#183: add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed Nov 4, 2018
1 parent f008cec commit 3360b22
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions onlinejudge/implementation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ast
import time
from typing import *
from typing.io import *

default_data_dir = os.path.join(os.environ.get('XDG_DATA_HOME') or os.path.expanduser('~/.local/share'), 'onlinejudge')
html_parser = 'lxml'
Expand All @@ -37,13 +38,13 @@ def parcentformat(s: str, table: Dict[str, str]) -> str:
def describe_status_code(status_code: int) -> str:
return '{} {}'.format(status_code, http.client.responses[status_code])

def previous_sibling_tag(tag):
def previous_sibling_tag(tag: bs4.Tag) -> bs4.Tag:
tag = tag.previous_sibling
while tag and not isinstance(tag, bs4.Tag):
tag = tag.previous_sibling
return tag

def next_sibling_tag(tag):
def next_sibling_tag(tag: bs4.Tag) -> bs4.Tag:
tag = tag.next_sibling
while tag and not isinstance(tag, bs4.Tag):
tag = tag.next_sibling
Expand Down Expand Up @@ -94,30 +95,30 @@ def get(self) -> List[TestCase]:
return self.data

class FormSender(object):
def __init__(self, form, url):
def __init__(self, form: bs4.Tag, url: str):
assert isinstance(form, bs4.Tag)
assert form.name == 'form'
self.form = form
self.url = url
self.payload = {}
self.files = {}
self.payload: Dict[str, str] = {}
self.files: Dict[str, IO[Any]] = {}
for input in self.form.find_all('input'):
log.debug('input: %s', str(input))
if input.attrs.get('type') in [ 'checkbox', 'radio' ]:
continue
if 'name' in input.attrs and 'value' in input.attrs:
self.payload[input['name']] = input['value']

def set(self, key, value):
def set(self, key: str, value: str) -> None:
self.payload[key] = value

def get(self):
def get(self) -> Dict[str, str]:
return self.payload

def set_file(self, key, value):
def set_file(self, key: str, value: IO[Any]) -> None:
self.files[key] = value

def request(self, session, action=None, **kwargs):
def request(self, session: requests.Session, action: Optional[str] = None, **kwargs) -> requests.Response:
action = action or self.form['action']
url = urllib.parse.urljoin(self.url, action)
method = self.form['method'].upper()
Expand Down

0 comments on commit 3360b22

Please sign in to comment.