Skip to content

Commit

Permalink
#183: add mypy to travis.yml and some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed Nov 3, 2018
1 parent f4391e7 commit c3edfdb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ language: python
python:
- 3.5
install:
- pip install mypy
- pip install .
script:
- mypy --ignore-missing-imports oj
- python setup.py test
branches:
only:
Expand Down
16 changes: 10 additions & 6 deletions onlinejudge/dispatch.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# Python Version: 3.x
import onlinejudge.implementation.logging as log
import onlinejudge.submissions as Submission
import onlinejudge.problems as Problem
import onlinejudge.services as Service
from typing import List, Type

submissions = []
def submission_from_url(s):
submissions: List[Type[Submission]] = []
def submission_from_url(s: str) -> Submission:
for cls in submissions:
it = cls.from_url(s)
if it is not None:
log.status('submission recognized: %s: %s', str(it), s)
return it
log.failure('unknown submission: %s', s)

problems = []
def problem_from_url(s):
problems: List[Type[Problem]] = []
def problem_from_url(s: str) -> Problem:
for cls in problems:
it = cls.from_url(s)
if it is not None:
Expand All @@ -22,8 +26,8 @@ def problem_from_url(s):
return it.get_problem()
log.failure('unknown problem: %s', s)

services = []
def service_from_url(s):
services: List[Type[Service]] = []
def service_from_url(s: str) -> Service:
for cls in services:
it = cls.from_url(s)
if it is not None:
Expand Down
13 changes: 9 additions & 4 deletions onlinejudge/implementation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
import sys
import os
import os.path
from typing import List, Optional

def main(args=None):

# argparse
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description='Tools for online judge services')
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-c', '--cookie', help='path to cookie. (default: {})'.format(utils.default_cookie_path))
Expand Down Expand Up @@ -289,8 +288,10 @@ def main(args=None):
subparser.add_argument('url')
subparser.add_argument('-f', '--format', choices=[ 'csv', 'tsv', 'json' ], default='tsv', help='default: tsv')

args = parser.parse_args(args=args)
return parser


def run_program(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None:
# logging
log_level = log.logging.INFO
if args.verbose:
Expand Down Expand Up @@ -327,3 +328,7 @@ def main(args=None):
sys.exit(1)


def main(args: Optional[List[str]] = None) -> None:
parser = get_parser()
namespace = parser.parse_args(args=args)
run_program(namespace, parser=parser)
10 changes: 6 additions & 4 deletions onlinejudge/service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Python Version: 3.x
import requests
from typing import Callable, Optional, Tuple

class Service(object):
def login(self, get_credentials, session=None):
def login(self, get_credentials: Callable[[], Tuple[str, str]], session: Optional[requests.Session] = None):
raise NotImplementedError
def get_url(self):
def get_url(self) -> str:
raise NotImplementedError
def get_name(self):
def get_name(self) -> str:
raise NotImplementedError
@classmethod
def from_url(self, s):
def from_url(self, s: str) -> str:
pass

0 comments on commit c3edfdb

Please sign in to comment.