bots/giphy: Use Python 3 type mypy annotations.
This commit is contained in:
parent
95b8ea4751
commit
77e9be0783
|
@ -38,6 +38,8 @@ force_include = [
|
|||
"zulip_bots/zulip_bots/bots/helloworld/helloworld.py",
|
||||
"zulip_bots/zulip_bots/bots/followup/followup.py",
|
||||
"zulip_bots/zulip_bots/bots/followup/test_followup.py",
|
||||
"zulip_bots/zulip_bots/bots/giphy/giphy.py",
|
||||
"zulip_bots/zulip_bots/bots/giphy/test_giphy.py",
|
||||
]
|
||||
|
||||
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import print_function
|
||||
from six.moves.configparser import SafeConfigParser
|
||||
from typing import Dict, Any, Union
|
||||
import requests
|
||||
import logging
|
||||
import sys
|
||||
|
@ -20,17 +21,17 @@ class GiphyHandler(object):
|
|||
and responds with a message with the GIF based on provided keywords.
|
||||
It also responds to private messages.
|
||||
'''
|
||||
def usage(self):
|
||||
def usage(self: Any) -> str:
|
||||
return '''
|
||||
This plugin allows users to post GIFs provided by Giphy.
|
||||
Users should preface keywords with the Giphy-bot @mention.
|
||||
The bot responds also to private messages.
|
||||
'''
|
||||
|
||||
def initialize(self, bot_handler):
|
||||
def initialize(self: Any, bot_handler: Any) -> None:
|
||||
self.config_info = bot_handler.get_config_info('giphy')
|
||||
|
||||
def handle_message(self, message, bot_handler):
|
||||
def handle_message(self: Any, message: Dict[str, str], bot_handler: Any) -> None:
|
||||
bot_response = get_bot_giphy_response(
|
||||
message,
|
||||
bot_handler,
|
||||
|
@ -43,7 +44,7 @@ class GiphyNoResultException(Exception):
|
|||
pass
|
||||
|
||||
|
||||
def get_url_gif_giphy(keyword, api_key):
|
||||
def get_url_gif_giphy(keyword: str, api_key: str) -> Union[int, str]:
|
||||
# Return a URL for a Giphy GIF based on keywords given.
|
||||
# In case of error, e.g. failure to fetch a GIF URL, it will
|
||||
# return a number.
|
||||
|
@ -52,7 +53,7 @@ def get_url_gif_giphy(keyword, api_key):
|
|||
try:
|
||||
data = requests.get(GIPHY_TRANSLATE_API, params=query)
|
||||
except requests.exceptions.ConnectionError as e: # Usually triggered by bad connection.
|
||||
logging.warning(e)
|
||||
logging.exception('Bad connection')
|
||||
raise
|
||||
data.raise_for_status()
|
||||
try:
|
||||
|
@ -63,7 +64,7 @@ def get_url_gif_giphy(keyword, api_key):
|
|||
return gif_url
|
||||
|
||||
|
||||
def get_bot_giphy_response(message, bot_handler, config_info):
|
||||
def get_bot_giphy_response(message: Dict[str, str], bot_handler: Any, config_info: Dict[str, str]) -> str:
|
||||
# Each exception has a specific reply should "gif_url" return a number.
|
||||
# The bot will post the appropriate message for the error.
|
||||
keyword = message['content']
|
||||
|
|
|
@ -8,12 +8,13 @@ import json
|
|||
from unittest.mock import patch
|
||||
from requests.exceptions import HTTPError, ConnectionError
|
||||
|
||||
from typing import Any, Union
|
||||
from zulip_bots.test_lib import BotTestCase
|
||||
|
||||
class TestGiphyBot(BotTestCase):
|
||||
bot_name = "giphy"
|
||||
|
||||
def test_normal(self):
|
||||
def test_normal(self: Any) -> None:
|
||||
bot_response = '[Click to enlarge]' \
|
||||
'(https://media4.giphy.com/media/3o6ZtpxSZbQRRnwCKQ/giphy.gif)' \
|
||||
'[](/static/images/interactive-bot/giphy/powered-by-giphy.png)'
|
||||
|
@ -27,7 +28,7 @@ class TestGiphyBot(BotTestCase):
|
|||
expected_method='send_reply'
|
||||
)
|
||||
|
||||
def test_no_result(self):
|
||||
def test_no_result(self: Any) -> None:
|
||||
with self.mock_config_info({'key': '12345678'}), \
|
||||
self.mock_http_conversation('test_no_result'):
|
||||
self.initialize_bot()
|
||||
|
@ -37,7 +38,7 @@ class TestGiphyBot(BotTestCase):
|
|||
expected_method='send_reply'
|
||||
)
|
||||
|
||||
def test_403(self):
|
||||
def test_403(self: Any) -> None:
|
||||
with self.mock_config_info({'key': '12345678'}), \
|
||||
self.mock_http_conversation('test_403'), \
|
||||
self.assertRaises(HTTPError):
|
||||
|
@ -48,7 +49,7 @@ class TestGiphyBot(BotTestCase):
|
|||
self.message_handler.handle_message(message={'content': 'Hello'},
|
||||
bot_handler=self.mock_bot_handler)
|
||||
|
||||
def test_connection_error(self):
|
||||
def test_connection_error(self: Any) -> None:
|
||||
with self.mock_config_info({'key': '12345678'}), \
|
||||
patch('requests.get', side_effect=ConnectionError()), \
|
||||
patch('logging.warning'):
|
||||
|
|
Loading…
Reference in a new issue