mypy: Add annotations for Yoda Bot.

This commit is contained in:
Marco Burstein 2017-12-09 12:55:41 -08:00 committed by Steve Howell
parent 8f64405bae
commit 3c794b384b
3 changed files with 15 additions and 14 deletions

View file

@ -64,6 +64,8 @@ force_include = [
"zulip_bots/zulip_bots/bots/xkcd/test_xkcd.py",
"zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py",
"zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py",
"zulip_bots/zulip_bots/bots/yoda/yoda.py",
"zulip_bots/zulip_bots/bots/yoda/test_yoda.py",
]
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")

View file

@ -17,7 +17,7 @@ class TestYodaBot(StubBotTestCase):
@mention-bot You will learn how to speak like me someday.
'''
def _test(self, message, response, fixture=None):
def _test(self, message: str, response: str, fixture: str=None) -> None:
with self.mock_config_info({'api_key': '12345678'}):
if fixture is not None:
with self.mock_http_conversation(fixture):
@ -26,11 +26,10 @@ class TestYodaBot(StubBotTestCase):
self.verify_reply(message, response)
# Override default function in StubBotTestCase
def test_bot_responds_to_empty_message(self):
def test_bot_responds_to_empty_message(self) -> None:
self._test('', self.help_text)
def test_bot(self):
def test_bot(self) -> None:
# Test normal sentence (1).
self._test('You will learn how to speak like me someday.',
"Learn how to speak like me someday, you will. Yes, hmmm.",

View file

@ -4,6 +4,7 @@ from __future__ import print_function
import logging
import ssl
import sys
from zulip_bots.lib import ExternalBotHandler
try:
import requests
except ImportError as e:
@ -37,10 +38,10 @@ class YodaSpeakHandler(object):
This bot will allow users to translate a sentence into 'Yoda speak'.
It looks for messages starting with '@mention-bot'.
'''
def initialize(self, bot_handler):
def initialize(self, bot_handler: ExternalBotHandler) -> None:
self.api_key = bot_handler.get_config_info('yoda')['api_key']
def usage(self):
def usage(self) -> str:
return '''
This bot will allow users to translate a sentence into
'Yoda speak'.
@ -54,10 +55,10 @@ class YodaSpeakHandler(object):
@mention-bot You will learn how to speak like me someday.
'''
def handle_message(self, message, bot_handler):
def handle_message(self, message: dict, bot_handler: ExternalBotHandler) -> None:
self.handle_input(message, bot_handler)
def send_to_yoda_api(self, sentence):
def send_to_yoda_api(self, sentence: str) -> str:
# function for sending sentence to api
response = requests.get("https://yoda.p.mashape.com/yoda?sentence=" + sentence,
headers={
@ -73,22 +74,21 @@ class YodaSpeakHandler(object):
if response.status_code == 503:
raise ServiceUnavailableError
else:
error_message = response.text['message']
error_message = response.json()['message']
logging.error(error_message)
error_code = response.status_code
error_message = error_message + 'Error code: ' + error_code +\
' Did you follow the instructions in the `readme.md` file?'
return error_message
def format_input(self, original_content):
def format_input(self, original_content: str) -> str:
# gets rid of whitespace around the edges, so that they aren't a problem in the future
message_content = original_content.strip()
# replaces all spaces with '+' to be in the format the api requires
sentence = message_content.replace(' ', '+')
return sentence
def handle_input(self, message, bot_handler):
def handle_input(self, message: dict, bot_handler: ExternalBotHandler) -> None:
original_content = message['content']
if self.is_help(original_content) or (original_content == ""):
@ -113,7 +113,7 @@ class YodaSpeakHandler(object):
bot_handler.send_reply(message, reply_message)
def send_message(self, bot_handler, message, stream, subject):
def send_message(self, bot_handler: ExternalBotHandler, message: str, stream: str, subject: str) -> None:
# function for sending a message
bot_handler.send_message(dict(
type='stream',
@ -122,7 +122,7 @@ class YodaSpeakHandler(object):
content=message
))
def is_help(self, original_content):
def is_help(self, original_content: str) -> bool:
# gets rid of whitespace around the edges, so that they aren't a problem in the future
message_content = original_content.strip()
if message_content == 'help':