From 8f64405bae3e2da3658168c25a57e342dfdd2106 Mon Sep 17 00:00:00 2001 From: Marco Burstein Date: Sat, 9 Dec 2017 12:44:11 -0800 Subject: [PATCH] mypy: Add annotations for Wikipedia Bot. --- tools/run-mypy | 2 ++ .../zulip_bots/bots/wikipedia/test_wikipedia.py | 2 +- zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py | 12 +++++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tools/run-mypy b/tools/run-mypy index c924ed9..a19c2a2 100755 --- a/tools/run-mypy +++ b/tools/run-mypy @@ -62,6 +62,8 @@ force_include = [ "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", + "zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py", + "zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py", ] parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.") diff --git a/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py b/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py index c1c5644..11d4f08 100755 --- a/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py +++ b/zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py @@ -5,7 +5,7 @@ from zulip_bots.test_lib import StubBotTestCase class TestWikipediaBot(StubBotTestCase): bot_name = "wikipedia" - def test_bot(self): + def test_bot(self) -> None: # Single-word query bot_request = 'happy' diff --git a/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py b/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py index f176b28..b74fb38 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py +++ b/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py @@ -4,6 +4,8 @@ import requests import logging import re from six.moves import urllib +from zulip_bots.lib import ExternalBotHandler +from typing import Optional # See readme.md for instructions on running this code. @@ -24,7 +26,7 @@ class WikipediaHandler(object): 'description': 'Searches Wikipedia for a term and returns the top 3 articles.', } - def usage(self): + def usage(self) -> str: return ''' This plugin will allow users to directly search Wikipedia for a specific key term and get the top 3 @@ -32,11 +34,11 @@ class WikipediaHandler(object): should preface searches with "@mention-bot". @mention-bot ''' - def handle_message(self, message, bot_handler): + def handle_message(self, message: dict, bot_handler: ExternalBotHandler) -> None: bot_response = self.get_bot_wiki_response(message, bot_handler) bot_handler.send_reply(message, bot_response) - def get_bot_wiki_response(self, message, bot_handler): + def get_bot_wiki_response(self, message: dict, bot_handler: ExternalBotHandler) -> Optional[str]: '''This function returns the URLs of the requested topic.''' help_text = 'Please enter your search term after @mention-bot' @@ -54,12 +56,12 @@ class WikipediaHandler(object): except requests.exceptions.RequestException: logging.error('broken link') - return + return None # Checking if the bot accessed the link. if data.status_code != 200: logging.error('Page not found.') - return + return None new_content = 'For search term:' + query + '\n' # Checking if there is content for the searched term