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/incrementor/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/test_virtual_fs.py",
"zulip_bots/zulip_bots/bots/weather/test_weather.py",

View file

@ -1,21 +1,23 @@
import re
import requests
from typing import Any, Dict
class LinkShortenerHandler(object):
'''A Zulip bot that will shorten URLs ("links") in a conversation using the
goo.gl URL shortener.
'''
def usage(self):
def usage(self) -> str:
return (
'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'
'`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')
def handle_message(self, message, bot_handler):
def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
REGEX_STR = (
'('
'(?:http|https):\/\/' # This allows for the HTTP or HTTPS
@ -58,7 +60,7 @@ class LinkShortenerHandler(object):
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
returns an empty string if something goes wrong.

View file

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