incrementor: Clean up type annotation/conversion.

This adds mypy annotations, and we also make the type
conversions in `handle_message` be a little more clear.

(@showell helped clean up this commit a bit)
This commit is contained in:
Marco Burstein 2017-12-09 13:55:55 -08:00 committed by Steve Howell
parent b4ff5230a9
commit 3c66894aff
3 changed files with 16 additions and 8 deletions

View file

@ -46,6 +46,8 @@ force_include = [
"zulip_bots/zulip_bots/bots/googlesearch/test_googlesearch.py", "zulip_bots/zulip_bots/bots/googlesearch/test_googlesearch.py",
"zulip_bots/zulip_bots/bots/help/help.py", "zulip_bots/zulip_bots/bots/help/help.py",
"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/test_incrementor.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,5 +1,6 @@
# See readme.md for instructions on running this code. # See readme.md for instructions on running this code.
from zulip_bots.lib import ExternalBotHandler
class IncrementorHandler(object): class IncrementorHandler(object):
META = { META = {
@ -7,7 +8,7 @@ class IncrementorHandler(object):
'description': 'Example bot to test the update_message() function.', 'description': 'Example bot to test the update_message() function.',
} }
def usage(self): def usage(self) -> str:
return ''' return '''
This is a boilerplate bot that makes use of the This is a boilerplate bot that makes use of the
update_message function. For the first @-mention, it initially update_message function. For the first @-mention, it initially
@ -15,23 +16,28 @@ class IncrementorHandler(object):
is @-mentioned, this number will be incremented in the same message. is @-mentioned, this number will be incremented in the same message.
''' '''
def initialize(self, bot_handler): def initialize(self, bot_handler: ExternalBotHandler) -> None:
storage = bot_handler.storage storage = bot_handler.storage
if not storage.contains('number') or not storage.contains('message_id'): if not storage.contains('number') or not storage.contains('message_id'):
storage.put('number', 0) storage.put('number', 0)
storage.put('message_id', None) storage.put('message_id', None)
def handle_message(self, message, bot_handler): def handle_message(self, message: dict, bot_handler: ExternalBotHandler) -> None:
storage = bot_handler.storage storage = bot_handler.storage
num = storage.get('number') num = storage.get('number')
storage.put('number', num + 1)
# num should already be an int, but we do `int()` to force an
# explicit type check
num = int(num) + 1
storage.put('number', num)
if storage.get('message_id') is None: if storage.get('message_id') is None:
result = bot_handler.send_reply(message, str(storage.get('number'))) result = bot_handler.send_reply(message, str(num))
storage.put('message_id', result['id']) storage.put('message_id', result['id'])
else: else:
bot_handler.update_message(dict( bot_handler.update_message(dict(
message_id = storage.get('message_id'), message_id=storage.get('message_id'),
content = str(storage.get('number')) content=str(num)
)) ))

View file

@ -10,7 +10,7 @@ from zulip_bots.test_lib import (
class TestIncrementorBot(StubBotTestCase): class TestIncrementorBot(StubBotTestCase):
bot_name = "incrementor" bot_name = "incrementor"
def test_bot(self): def test_bot(self) -> None:
bot = get_bot_message_handler(self.bot_name) bot = get_bot_message_handler(self.bot_name)
bot_handler = StubBotHandler() bot_handler = StubBotHandler()