mypy: Add annotations for xkcd Bot.

This commit is contained in:
Marco Burstein 2017-12-09 12:35:16 -08:00 committed by Steve Howell
parent e01ecc9fde
commit 0467f83314
3 changed files with 13 additions and 10 deletions

View file

@ -60,6 +60,8 @@ force_include = [
"zulip_bots/zulip_bots/bots/encrypt/test_encrypt.py", "zulip_bots/zulip_bots/bots/encrypt/test_encrypt.py",
"zulip_bots/zulip_bots/bots/chess/chess.py", "zulip_bots/zulip_bots/bots/chess/chess.py",
"zulip_bots/zulip_bots/bots/chess/test_chess.py", "zulip_bots/zulip_bots/bots/chess/test_chess.py",
"zulip_bots/zulip_bots/bots/xkcd/xkcd.py",
"zulip_bots/zulip_bots/bots/xkcd/test_xkcd.py",
] ]
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.") parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")

View file

@ -7,7 +7,7 @@ from zulip_bots.test_lib import StubBotTestCase
class TestXkcdBot(StubBotTestCase): class TestXkcdBot(StubBotTestCase):
bot_name = "xkcd" bot_name = "xkcd"
def test_latest_command(self): def test_latest_command(self) -> None:
bot_response = ("#1866: **Russell's Teapot**\n" bot_response = ("#1866: **Russell's Teapot**\n"
"[Unfortunately, NASA regulations state that Bertrand Russell-related " "[Unfortunately, NASA regulations state that Bertrand Russell-related "
"payloads can only be launched within launch vehicles which do not launch " "payloads can only be launched within launch vehicles which do not launch "
@ -15,7 +15,7 @@ class TestXkcdBot(StubBotTestCase):
with self.mock_http_conversation('test_latest'): with self.mock_http_conversation('test_latest'):
self.verify_reply('latest', bot_response) self.verify_reply('latest', bot_response)
def test_random_command(self): def test_random_command(self) -> None:
bot_response = ("#1800: **Chess Notation**\n" bot_response = ("#1800: **Chess Notation**\n"
"[I've decided to score all my conversations using chess win-loss " "[I've decided to score all my conversations using chess win-loss "
"notation. (??)](https://imgs.xkcd.com/comics/chess_notation.png)") "notation. (??)](https://imgs.xkcd.com/comics/chess_notation.png)")
@ -27,14 +27,14 @@ class TestXkcdBot(StubBotTestCase):
randint.return_value = mock_rand_value.return_value randint.return_value = mock_rand_value.return_value
self.verify_reply('random', bot_response) self.verify_reply('random', bot_response)
def test_numeric_comic_id_command_1(self): def test_numeric_comic_id_command_1(self) -> None:
bot_response = ("#1: **Barrel - Part 1**\n[Don't we all.]" bot_response = ("#1: **Barrel - Part 1**\n[Don't we all.]"
"(https://imgs.xkcd.com/comics/barrel_cropped_(1).jpg)") "(https://imgs.xkcd.com/comics/barrel_cropped_(1).jpg)")
with self.mock_http_conversation('test_specific_id'): with self.mock_http_conversation('test_specific_id'):
self.verify_reply('1', bot_response) self.verify_reply('1', bot_response)
@mock.patch('logging.exception') @mock.patch('logging.exception')
def test_invalid_comic_ids(self, mock_logging_exception): def test_invalid_comic_ids(self, mock_logging_exception: mock.Mock) -> None:
invalid_id_txt = "Sorry, there is likely no xkcd comic strip with id: #" invalid_id_txt = "Sorry, there is likely no xkcd comic strip with id: #"
for comic_id, fixture in (('0', 'test_not_existing_id_2'), for comic_id, fixture in (('0', 'test_not_existing_id_2'),
@ -42,7 +42,7 @@ class TestXkcdBot(StubBotTestCase):
with self.mock_http_conversation(fixture): with self.mock_http_conversation(fixture):
self.verify_reply(comic_id, invalid_id_txt + comic_id) self.verify_reply(comic_id, invalid_id_txt + comic_id)
def test_help_responses(self): def test_help_responses(self) -> None:
help_txt = "xkcd bot supports these commands:" help_txt = "xkcd bot supports these commands:"
err_txt = "xkcd bot only supports these commands, not `{}`:" err_txt = "xkcd bot only supports these commands, not `{}`:"
commands = ''' commands = '''

View file

@ -2,6 +2,7 @@ import random
import logging import logging
import requests import requests
from zulip_bots.lib import ExternalBotHandler
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'
@ -19,7 +20,7 @@ class XkcdHandler(object):
'description': 'Fetches comic strips from https://xkcd.com.', 'description': 'Fetches comic strips from https://xkcd.com.',
} }
def usage(self): def usage(self) -> str:
return ''' return '''
This plugin allows users to fetch a comic strip provided by This plugin allows users to fetch a comic strip provided by
https://xkcd.com. Users should preface the command with "@mention-bot". https://xkcd.com. Users should preface the command with "@mention-bot".
@ -32,7 +33,7 @@ class XkcdHandler(object):
`<comic_id>`, e.g `@mention-bot 1234`. `<comic_id>`, e.g `@mention-bot 1234`.
''' '''
def handle_message(self, message, bot_handler): def handle_message(self, message: dict, bot_handler: ExternalBotHandler) -> None:
xkcd_bot_response = get_xkcd_bot_response(message) xkcd_bot_response = get_xkcd_bot_response(message)
bot_handler.send_reply(message, xkcd_bot_response) bot_handler.send_reply(message, xkcd_bot_response)
@ -47,7 +48,7 @@ class XkcdNotFoundError(Exception):
class XkcdServerError(Exception): class XkcdServerError(Exception):
pass pass
def get_xkcd_bot_response(message): def get_xkcd_bot_response(message: dict) -> str:
original_content = message['content'].strip() original_content = message['content'].strip()
command = original_content.strip() command = original_content.strip()
@ -82,7 +83,7 @@ def get_xkcd_bot_response(message):
fetched['alt'], fetched['alt'],
fetched['img'])) fetched['img']))
def fetch_xkcd_query(mode, comic_id=None): def fetch_xkcd_query(mode: int, comic_id: str = None) -> dict:
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
@ -111,7 +112,7 @@ def fetch_xkcd_query(mode, comic_id=None):
xkcd_json = fetched.json() xkcd_json = fetched.json()
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
logging.warning(e) logging.exception("Connection Error")
raise raise
return xkcd_json return xkcd_json