diff --git a/tools/run-mypy b/tools/run-mypy index a40b9e2..2e1d417 100755 --- a/tools/run-mypy +++ b/tools/run-mypy @@ -42,6 +42,8 @@ force_include = [ "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/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.") diff --git a/zulip_bots/zulip_bots/bots/googlesearch/googlesearch.py b/zulip_bots/zulip_bots/bots/googlesearch/googlesearch.py index 14ac74b..ef490ae 100644 --- a/zulip_bots/zulip_bots/bots/googlesearch/googlesearch.py +++ b/zulip_bots/zulip_bots/bots/googlesearch/googlesearch.py @@ -7,7 +7,9 @@ import requests 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} # Gets the page page = requests.get('http://www.google.com/search', params=query) @@ -38,7 +40,7 @@ def google_search(keywords): results.append(result) 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, \ followed by what you want to search for. If \ found, Zulip will return the first search result \ @@ -65,13 +67,13 @@ def get_google_result(search_keywords): except AttributeError as a_err: # google.search query failed and urls is of object # 'NoneType' - logging.exception(a_err) + logging.exception(str(a_err)) return "Error: Google search failed with a NoneType result. {}.".format(a_err) except TypeError as t_err: # google.search query failed and returned None # This technically should not happen but the prior # error check assumed this behavior - logging.exception(t_err) + logging.exception(str(t_err)) return "Error: Google search function failed. {}.".format(t_err) except Exception as e: return 'Error: Search failed. {}.'.format(e) @@ -85,7 +87,7 @@ class GoogleSearchHandler(object): with @mentioned-bot. ''' - def usage(self): + def usage(self: Any) -> str: return ''' This plugin will allow users to search for a given search term on Google from @@ -95,7 +97,7 @@ class GoogleSearchHandler(object): @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'] result = get_google_result(original_content) bot_handler.send_reply(message, result) diff --git a/zulip_bots/zulip_bots/bots/googlesearch/test_googlesearch.py b/zulip_bots/zulip_bots/bots/googlesearch/test_googlesearch.py index 680abfa..858219f 100644 --- a/zulip_bots/zulip_bots/bots/googlesearch/test_googlesearch.py +++ b/zulip_bots/zulip_bots/bots/googlesearch/test_googlesearch.py @@ -9,16 +9,18 @@ from six.moves.urllib import error from zulip_bots.test_lib import BotTestCase +from typing import Any + class TestGoogleSearchBot(BotTestCase): bot_name = 'googlesearch' # Simple query - def test_normal(self): + def test_normal(self: Any) -> None: 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') # 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, \ followed by what you want to search for. If \ 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') # 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, \ followed by what you want to search for. If \ found, Zulip will return the first search result \ @@ -41,6 +43,6 @@ class TestGoogleSearchBot(BotTestCase): '@mentioned-bot how to create a chatbot'." 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'): self.assert_bot_response({'content': 'no res'}, {'content': 'Found no results.'}, 'send_reply')