mypy: Improve typing of bots & API; enforce no_implicit_optional.
MODIFY
mypy.ini
mypy.ini
check_untyped_defs = True
disallow_any_generics = True
strict_optional = True
+no_implicit_optional = True
incremental = True
scripts_are_modules = True

MODIFY
zulip/zulip/__init__.py
zulip/zulip/__init__.py
@@ -286,7 +286,7 @@ class Client(object):
site=None, client=None,
cert_bundle=None, insecure=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:
client = _default_client()

MODIFY
zulip_bots/zulip_bots/bots/idonethis/idonethis.py
zulip_bots/zulip_bots/bots/idonethis/idonethis.py
@@ -3,7 +3,7 @@ import requests
import logging
import re
-from typing import Any, Dict
+from typing import Any, Dict, Optional
API_BASE_URL = "https://beta.idonethis.com/api/v2"
@@ -25,7 +25,7 @@ class UnknownCommandSyntax(Exception):
class UnspecifiedProblemException(Exception):
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}
if method == "GET":
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:
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:
return make_API_request("/entries?team_id={}".format(team_id))
else:

MODIFY
zulip_bots/zulip_bots/bots/xkcd/xkcd.py
zulip_bots/zulip_bots/bots/xkcd/xkcd.py
@@ -3,7 +3,7 @@ import random
import logging
import requests
-from typing import Any, Dict
+from typing import Any, Dict, Optional
XKCD_TEMPLATE_URL = 'https://xkcd.com/%s/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['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:
if mode == XkcdBotCommand.LATEST: # Fetch the latest comic strip.
url = LATEST_XKCD_URL

MODIFY
zulip_bots/zulip_bots/bots/yoda/test_yoda.py
zulip_bots/zulip_bots/bots/yoda/test_yoda.py
from zulip_bots.bots.yoda.yoda import ServiceUnavailableError
from zulip_bots.test_lib import BotTestCase
+from typing import Optional
+
class TestYodaBot(BotTestCase):
bot_name = "yoda"
@@ -18,7 +20,7 @@ class TestYodaBot(BotTestCase):
@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'}):
if fixture is not None:
with self.mock_http_conversation(fixture):

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