mypy: Add annotations for Wikipedia Bot.

This commit is contained in:
Marco Burstein 2017-12-09 12:44:11 -08:00 committed by Steve Howell
parent 0467f83314
commit 8f64405bae
3 changed files with 10 additions and 6 deletions

View file

@ -62,6 +62,8 @@ force_include = [
"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/xkcd.py",
"zulip_bots/zulip_bots/bots/xkcd/test_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.") parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")

View file

@ -5,7 +5,7 @@ from zulip_bots.test_lib import StubBotTestCase
class TestWikipediaBot(StubBotTestCase): class TestWikipediaBot(StubBotTestCase):
bot_name = "wikipedia" bot_name = "wikipedia"
def test_bot(self): def test_bot(self) -> None:
# Single-word query # Single-word query
bot_request = 'happy' bot_request = 'happy'

View file

@ -4,6 +4,8 @@ import requests
import logging import logging
import re import re
from six.moves import urllib from six.moves import urllib
from zulip_bots.lib import ExternalBotHandler
from typing import Optional
# See readme.md for instructions on running this code. # 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.', 'description': 'Searches Wikipedia for a term and returns the top 3 articles.',
} }
def usage(self): def usage(self) -> str:
return ''' return '''
This plugin will allow users to directly search This plugin will allow users to directly search
Wikipedia for a specific key term and get the top 3 Wikipedia for a specific key term and get the top 3
@ -32,11 +34,11 @@ class WikipediaHandler(object):
should preface searches with "@mention-bot". should preface searches with "@mention-bot".
@mention-bot <name of article>''' @mention-bot <name of article>'''
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_response = self.get_bot_wiki_response(message, bot_handler)
bot_handler.send_reply(message, bot_response) 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.''' '''This function returns the URLs of the requested topic.'''
help_text = 'Please enter your search term after @mention-bot' help_text = 'Please enter your search term after @mention-bot'
@ -54,12 +56,12 @@ class WikipediaHandler(object):
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
logging.error('broken link') logging.error('broken link')
return return None
# Checking if the bot accessed the link. # Checking if the bot accessed the link.
if data.status_code != 200: if data.status_code != 200:
logging.error('Page not found.') logging.error('Page not found.')
return return None
new_content = 'For search term:' + query + '\n' new_content = 'For search term:' + query + '\n'
# Checking if there is content for the searched term # Checking if there is content for the searched term