mypy: Improve typing of bots & API; enforce no_implicit_optional.

This commit is contained in:
neiljp (Neil Pilgrim) 2018-03-30 23:21:11 -07:00 committed by showell
parent 7dc0703397
commit 55aff6f64b
5 changed files with 10 additions and 7 deletions

View file

@ -2,6 +2,7 @@
check_untyped_defs = True check_untyped_defs = True
disallow_any_generics = True disallow_any_generics = True
strict_optional = True strict_optional = True
no_implicit_optional = True
incremental = True incremental = True
scripts_are_modules = True scripts_are_modules = True

View file

@ -286,7 +286,7 @@ class Client(object):
site=None, client=None, site=None, client=None,
cert_bundle=None, insecure=None, cert_bundle=None, insecure=None,
client_cert=None, client_cert_key=None): client_cert=None, client_cert_key=None):
# type: (Optional[str], Optional[str], Optional[str], bool, bool, Optional[str], Optional[str], Optional[str], bool, Optional[str], Optional[str]) -> None # type: (Optional[str], Optional[str], Optional[str], bool, bool, Optional[str], Optional[str], Optional[str], Optional[bool], Optional[str], Optional[str]) -> None
if client is None: if client is None:
client = _default_client() client = _default_client()

View file

@ -3,7 +3,7 @@ import requests
import logging import logging
import re import re
from typing import Any, Dict from typing import Any, Dict, Optional
API_BASE_URL = "https://beta.idonethis.com/api/v2" API_BASE_URL = "https://beta.idonethis.com/api/v2"
@ -25,7 +25,7 @@ class UnknownCommandSyntax(Exception):
class UnspecifiedProblemException(Exception): class UnspecifiedProblemException(Exception):
pass pass
def make_API_request(endpoint: str, method: str="GET", body: Dict[str, str]=None) -> Any: def make_API_request(endpoint: str, method: str="GET", body: Optional[Dict[str, str]]=None) -> Any:
headers = {'Authorization': 'Token ' + api_key} headers = {'Authorization': 'Token ' + api_key}
if method == "GET": if method == "GET":
r = requests.get(API_BASE_URL + endpoint, headers=headers) r = requests.get(API_BASE_URL + endpoint, headers=headers)
@ -52,7 +52,7 @@ def api_show_team(hash_id: str) -> Any:
def api_show_users(hash_id: str) -> Any: def api_show_users(hash_id: str) -> Any:
return make_API_request("/teams/{}/members".format(hash_id)) return make_API_request("/teams/{}/members".format(hash_id))
def api_list_entries(team_id: str=None) -> Any: def api_list_entries(team_id: Optional[str]=None) -> Any:
if team_id: if team_id:
return make_API_request("/entries?team_id={}".format(team_id)) return make_API_request("/entries?team_id={}".format(team_id))
else: else:

View file

@ -3,7 +3,7 @@ import random
import logging import logging
import requests import requests
from typing import Any, Dict from typing import Any, Dict, Optional
XKCD_TEMPLATE_URL = 'https://xkcd.com/%s/info.0.json' XKCD_TEMPLATE_URL = 'https://xkcd.com/%s/info.0.json'
LATEST_XKCD_URL = 'https://xkcd.com/info.0.json' LATEST_XKCD_URL = 'https://xkcd.com/info.0.json'
@ -84,7 +84,7 @@ def get_xkcd_bot_response(message: Dict[str, str]) -> str:
fetched['alt'], fetched['alt'],
fetched['img'])) fetched['img']))
def fetch_xkcd_query(mode: int, comic_id: str = None) -> Dict[str, str]: def fetch_xkcd_query(mode: int, comic_id: Optional[str]=None) -> Dict[str, str]:
try: try:
if mode == XkcdBotCommand.LATEST: # Fetch the latest comic strip. if mode == XkcdBotCommand.LATEST: # Fetch the latest comic strip.
url = LATEST_XKCD_URL url = LATEST_XKCD_URL

View file

@ -1,6 +1,8 @@
from zulip_bots.bots.yoda.yoda import ServiceUnavailableError from zulip_bots.bots.yoda.yoda import ServiceUnavailableError
from zulip_bots.test_lib import BotTestCase from zulip_bots.test_lib import BotTestCase
from typing import Optional
class TestYodaBot(BotTestCase): class TestYodaBot(BotTestCase):
bot_name = "yoda" bot_name = "yoda"
@ -18,7 +20,7 @@ class TestYodaBot(BotTestCase):
@mention-bot You will learn how to speak like me someday. @mention-bot You will learn how to speak like me someday.
''' '''
def _test(self, message: str, response: str, fixture: str=None) -> None: def _test(self, message: str, response: str, fixture: Optional[str]=None) -> None:
with self.mock_config_info({'api_key': '12345678'}): with self.mock_config_info({'api_key': '12345678'}):
if fixture is not None: if fixture is not None:
with self.mock_http_conversation(fixture): with self.mock_http_conversation(fixture):