mypy: Add annotations for Link Shortener Bot.

Based on work started by @skunkmb.
This commit is contained in:
Marco Burstein 2017-12-09 14:00:33 -08:00 committed by showell
parent c4b42ff599
commit e9aa94f81c
3 changed files with 12 additions and 8 deletions

View file

@ -49,6 +49,8 @@ force_include = [
"zulip_bots/zulip_bots/bots/help/test_help.py", "zulip_bots/zulip_bots/bots/help/test_help.py",
"zulip_bots/zulip_bots/bots/incrementor/incrementor.py", "zulip_bots/zulip_bots/bots/incrementor/incrementor.py",
"zulip_bots/zulip_bots/bots/incrementor/test_incrementor.py", "zulip_bots/zulip_bots/bots/incrementor/test_incrementor.py",
"zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py",
"zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py",
"zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py", "zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py",
"zulip_bots/zulip_bots/bots/virtual_fs/test_virtual_fs.py", "zulip_bots/zulip_bots/bots/virtual_fs/test_virtual_fs.py",
"zulip_bots/zulip_bots/bots/weather/test_weather.py", "zulip_bots/zulip_bots/bots/weather/test_weather.py",

View file

@ -1,21 +1,23 @@
import re import re
import requests import requests
from typing import Any, Dict
class LinkShortenerHandler(object): class LinkShortenerHandler(object):
'''A Zulip bot that will shorten URLs ("links") in a conversation using the '''A Zulip bot that will shorten URLs ("links") in a conversation using the
goo.gl URL shortener. goo.gl URL shortener.
''' '''
def usage(self): def usage(self) -> str:
return ( return (
'Mention the link shortener bot in a conversation and then enter ' 'Mention the link shortener bot in a conversation and then enter '
'any URLs you want to shorten in the body of the message. \n\n' 'any URLs you want to shorten in the body of the message. \n\n'
'`key` must be set in `link_shortener.conf`.') '`key` must be set in `link_shortener.conf`.')
def initialize(self, bot_handler): def initialize(self, bot_handler: Any) -> None:
self.config_info = bot_handler.get_config_info('link_shortener') self.config_info = bot_handler.get_config_info('link_shortener')
def handle_message(self, message, bot_handler): def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
REGEX_STR = ( REGEX_STR = (
'(' '('
'(?:http|https):\/\/' # This allows for the HTTP or HTTPS '(?:http|https):\/\/' # This allows for the HTTP or HTTPS
@ -58,7 +60,7 @@ class LinkShortenerHandler(object):
bot_handler.send_reply(message, final_response) bot_handler.send_reply(message, final_response)
def shorten_link(self, long_url): def shorten_link(self, long_url: str) -> str:
'''Shortens a link using goo.gl Link Shortener and returns it, or '''Shortens a link using goo.gl Link Shortener and returns it, or
returns an empty string if something goes wrong. returns an empty string if something goes wrong.

View file

@ -3,18 +3,18 @@ from zulip_bots.test_lib import StubBotTestCase
class TestLinkShortenerBot(StubBotTestCase): class TestLinkShortenerBot(StubBotTestCase):
bot_name = "link_shortener" bot_name = "link_shortener"
def _test(self, message, response): def _test(self, message: str, response: str) -> None:
with self.mock_config_info({'key': 'qwertyuiop'}): with self.mock_config_info({'key': 'qwertyuiop'}):
self.verify_reply(message, response) self.verify_reply(message, response)
def test_bot(self): def test_bot(self) -> None:
message = 'Shorten https://www.github.com/zulip/zulip please.' message = 'Shorten https://www.github.com/zulip/zulip please.'
response = 'https://www.github.com/zulip/zulip: https://goo.gl/6uoWKb' response = 'https://www.github.com/zulip/zulip: https://goo.gl/6uoWKb'
with self.mock_http_conversation('test_normal'): with self.mock_http_conversation('test_normal'):
self._test(message, response) self._test(message, response)
def test_bot_empty(self): def test_bot_empty(self) -> None:
message = 'Shorten nothing please.' message = 'Shorten nothing please.'
response = 'No links found. Send "help" to see usage instructions.' response = 'No links found. Send "help" to see usage instructions.'
@ -23,7 +23,7 @@ class TestLinkShortenerBot(StubBotTestCase):
# requests. # requests.
self._test(message, response) self._test(message, response)
def test_bot_help(self): def test_bot_help(self) -> None:
message = 'help' message = 'help'
response = ('Mention the link shortener bot in a conversation and then ' response = ('Mention the link shortener bot in a conversation and then '
'enter any URLs you want to shorten in the body of the message.') 'enter any URLs you want to shorten in the body of the message.')