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