mypy: Add annotations for googlesearch.

This commit is contained in:
fredfishgames 2017-12-07 19:40:36 +00:00 committed by showell
parent b7dbe7af41
commit 40c09b4b16
3 changed files with 16 additions and 10 deletions

View file

@ -42,6 +42,8 @@ force_include = [
"zulip_bots/zulip_bots/bots/giphy/test_giphy.py", "zulip_bots/zulip_bots/bots/giphy/test_giphy.py",
"zulip_bots/zulip_bots/bots/github_detail/github_detail.py", "zulip_bots/zulip_bots/bots/github_detail/github_detail.py",
"zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py", "zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py",
"zulip_bots/zulip_bots/bots/googlesearch/googlesearch.py",
"zulip_bots/zulip_bots/bots/googlesearch/test_googlesearch.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,9 @@ import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
def google_search(keywords): from typing import Dict, Any, Union, List
def google_search(keywords: str) -> List[Dict[str, str]]:
query = {'q': keywords} query = {'q': keywords}
# Gets the page # Gets the page
page = requests.get('http://www.google.com/search', params=query) page = requests.get('http://www.google.com/search', params=query)
@ -38,7 +40,7 @@ def google_search(keywords):
results.append(result) results.append(result)
return results return results
def get_google_result(search_keywords): def get_google_result(search_keywords: str) -> str:
help_message = "To use this bot, start messages with @mentioned-bot, \ help_message = "To use this bot, start messages with @mentioned-bot, \
followed by what you want to search for. If \ followed by what you want to search for. If \
found, Zulip will return the first search result \ found, Zulip will return the first search result \
@ -65,13 +67,13 @@ def get_google_result(search_keywords):
except AttributeError as a_err: except AttributeError as a_err:
# google.search query failed and urls is of object # google.search query failed and urls is of object
# 'NoneType' # 'NoneType'
logging.exception(a_err) logging.exception(str(a_err))
return "Error: Google search failed with a NoneType result. {}.".format(a_err) return "Error: Google search failed with a NoneType result. {}.".format(a_err)
except TypeError as t_err: except TypeError as t_err:
# google.search query failed and returned None # google.search query failed and returned None
# This technically should not happen but the prior # This technically should not happen but the prior
# error check assumed this behavior # error check assumed this behavior
logging.exception(t_err) logging.exception(str(t_err))
return "Error: Google search function failed. {}.".format(t_err) return "Error: Google search function failed. {}.".format(t_err)
except Exception as e: except Exception as e:
return 'Error: Search failed. {}.'.format(e) return 'Error: Search failed. {}.'.format(e)
@ -85,7 +87,7 @@ class GoogleSearchHandler(object):
with @mentioned-bot. with @mentioned-bot.
''' '''
def usage(self): def usage(self: Any) -> str:
return ''' return '''
This plugin will allow users to search This plugin will allow users to search
for a given search term on Google from for a given search term on Google from
@ -95,7 +97,7 @@ class GoogleSearchHandler(object):
@mentioned-bot. @mentioned-bot.
''' '''
def handle_message(self, message, bot_handler): def handle_message(self: Any, message: Dict[str, str], bot_handler: Any) -> None:
original_content = message['content'] original_content = message['content']
result = get_google_result(original_content) result = get_google_result(original_content)
bot_handler.send_reply(message, result) bot_handler.send_reply(message, result)

View file

@ -9,16 +9,18 @@ from six.moves.urllib import error
from zulip_bots.test_lib import BotTestCase from zulip_bots.test_lib import BotTestCase
from typing import Any
class TestGoogleSearchBot(BotTestCase): class TestGoogleSearchBot(BotTestCase):
bot_name = 'googlesearch' bot_name = 'googlesearch'
# Simple query # Simple query
def test_normal(self): def test_normal(self: Any) -> None:
with self.mock_http_conversation('test_normal'): with self.mock_http_conversation('test_normal'):
self.assert_bot_response({'content': 'zulip'}, {'content': 'Found Result: [Zulip](https://www.google.com/url?url=https%3A%2F%2Fzulipchat.com%2F)'}, 'send_reply') self.assert_bot_response({'content': 'zulip'}, {'content': 'Found Result: [Zulip](https://www.google.com/url?url=https%3A%2F%2Fzulipchat.com%2F)'}, 'send_reply')
# Help without typing anything # Help without typing anything
def test_bot_help_none(self): def test_bot_help_none(self: Any) -> None:
help_message = "To use this bot, start messages with @mentioned-bot, \ help_message = "To use this bot, start messages with @mentioned-bot, \
followed by what you want to search for. If \ followed by what you want to search for. If \
found, Zulip will return the first search result \ found, Zulip will return the first search result \
@ -30,7 +32,7 @@ class TestGoogleSearchBot(BotTestCase):
self.assert_bot_response({'content': ''}, {'content': help_message}, 'send_reply') self.assert_bot_response({'content': ''}, {'content': help_message}, 'send_reply')
# Help from typing 'help' # Help from typing 'help'
def test_bot_help(self): def test_bot_help(self: Any) -> None:
help_message = "To use this bot, start messages with @mentioned-bot, \ help_message = "To use this bot, start messages with @mentioned-bot, \
followed by what you want to search for. If \ followed by what you want to search for. If \
found, Zulip will return the first search result \ found, Zulip will return the first search result \
@ -41,6 +43,6 @@ class TestGoogleSearchBot(BotTestCase):
'@mentioned-bot how to create a chatbot'." '@mentioned-bot how to create a chatbot'."
self.assert_bot_response({'content': 'help'}, {'content': help_message}, 'send_reply') self.assert_bot_response({'content': 'help'}, {'content': help_message}, 'send_reply')
def test_bot_no_results(self): def test_bot_no_results(self: Any) -> None:
with self.mock_http_conversation('test_no_result'): with self.mock_http_conversation('test_no_result'):
self.assert_bot_response({'content': 'no res'}, {'content': 'Found no results.'}, 'send_reply') self.assert_bot_response({'content': 'no res'}, {'content': 'Found no results.'}, 'send_reply')