From 3c794b384b4183c390e86e5802b50c1dda4b4ed2 Mon Sep 17 00:00:00 2001 From: Marco Burstein Date: Sat, 9 Dec 2017 12:55:41 -0800 Subject: [PATCH] mypy: Add annotations for Yoda Bot. --- tools/run-mypy | 2 ++ zulip_bots/zulip_bots/bots/yoda/test_yoda.py | 7 +++---- zulip_bots/zulip_bots/bots/yoda/yoda.py | 20 ++++++++++---------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/tools/run-mypy b/tools/run-mypy index a19c2a2..f5bd5e6 100755 --- a/tools/run-mypy +++ b/tools/run-mypy @@ -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.") diff --git a/zulip_bots/zulip_bots/bots/yoda/test_yoda.py b/zulip_bots/zulip_bots/bots/yoda/test_yoda.py index a7eca7c..173a448 100644 --- a/zulip_bots/zulip_bots/bots/yoda/test_yoda.py +++ b/zulip_bots/zulip_bots/bots/yoda/test_yoda.py @@ -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.", diff --git a/zulip_bots/zulip_bots/bots/yoda/yoda.py b/zulip_bots/zulip_bots/bots/yoda/yoda.py index 6fe8396..5236479 100644 --- a/zulip_bots/zulip_bots/bots/yoda/yoda.py +++ b/zulip_bots/zulip_bots/bots/yoda/yoda.py @@ -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':