mypy: Add annotations for converter.

This commit is contained in:
Kai Chen 2017-12-07 15:53:29 -08:00 committed by showell
parent 31853cfa80
commit d33e9b9d92
3 changed files with 25 additions and 16 deletions

View file

@ -52,7 +52,12 @@ force_include = [
"zulip_bots/zulip_bots/bots/weather/weather.py", "zulip_bots/zulip_bots/bots/weather/weather.py",
"zulip_bots/zulip_bots/bots/youtube/youtube.py", "zulip_bots/zulip_bots/bots/youtube/youtube.py",
"zulip_bots/zulip_bots/bots/youtube/test_youtube.py", "zulip_bots/zulip_bots/bots/youtube/test_youtube.py",
"zulip_bots/zulip_bots/bots/converter/converter.py",
"zulip_bots/zulip_bots/bots/converter/test_converter.py",
"zulip_bots/zulip_bots/bots/define/define.py",
"zulip_bots/zulip_bots/bots/define/test_define.py",
"zulip_bots/zulip_bots/bots/encrypt/encrypt.py",
"zulip_bots/zulip_bots/bots/encrypt/test_encrypt.py",
] ]
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.") parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")

View file

@ -10,7 +10,9 @@ from math import log10, floor
import re import re
from zulip_bots.bots.converter import utils from zulip_bots.bots.converter import utils
def is_float(value): from typing import Any, Dict, List
def is_float(value: Any) -> bool:
try: try:
float(value) float(value)
return True return True
@ -22,7 +24,8 @@ def is_float(value):
# fractional decimals, e.g. 0.00045 would become 0.0. # fractional decimals, e.g. 0.00045 would become 0.0.
# 'round_to()' rounds only the digits that are not 0. # 'round_to()' rounds only the digits that are not 0.
# 0.00045 would then become 0.0005. # 0.00045 would then become 0.0005.
def round_to(x, digits):
def round_to(x: float, digits: int) -> float:
return round(x, digits-int(floor(log10(abs(x))))) return round(x, digits-int(floor(log10(abs(x)))))
class ConverterHandler(object): class ConverterHandler(object):
@ -35,7 +38,7 @@ class ConverterHandler(object):
the plugin, along with a list of all supported units. the plugin, along with a list of all supported units.
''' '''
def usage(self): def usage(self) -> str:
return ''' return '''
This plugin allows users to make conversions between This plugin allows users to make conversions between
various units, e.g. Celsius to Fahrenheit, various units, e.g. Celsius to Fahrenheit,
@ -46,11 +49,11 @@ class ConverterHandler(object):
all supported units. all supported units.
''' '''
def handle_message(self, message, bot_handler): def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
bot_response = get_bot_converter_response(message, bot_handler) bot_response = get_bot_converter_response(message, bot_handler)
bot_handler.send_reply(message, bot_response) bot_handler.send_reply(message, bot_response)
def get_bot_converter_response(message, bot_handler): def get_bot_converter_response(message: Dict[str, str], bot_handler: Any) -> str:
content = message['content'] content = message['content']
words = content.lower().split() words = content.lower().split()
@ -72,8 +75,9 @@ def get_bot_converter_response(message, bot_handler):
results.append('`' + number + '` is not a valid number. ' + utils.QUICK_HELP) results.append('`' + number + '` is not a valid number. ' + utils.QUICK_HELP)
continue continue
number = float(number) # cannot reassign "number" as a float after using as string, so changed name
number_res = copy.copy(number) convert_num = float(number)
number_res = copy.copy(convert_num)
for key, exp in utils.PREFIXES.items(): for key, exp in utils.PREFIXES.items():
if unit_from.startswith(key): if unit_from.startswith(key):
@ -83,14 +87,14 @@ def get_bot_converter_response(message, bot_handler):
exponent -= exp exponent -= exp
unit_to = unit_to[len(key):] unit_to = unit_to[len(key):]
uf_to_std = utils.UNITS.get(unit_from, False) uf_to_std = utils.UNITS.get(unit_from, []) # type: List[Any]
ut_to_std = utils.UNITS.get(unit_to, False) ut_to_std = utils.UNITS.get(unit_to, []) # type: List[Any]
if uf_to_std is False: if not uf_to_std:
results.append('`' + unit_from + '` is not a valid unit. ' + utils.QUICK_HELP) results.append('`' + unit_from + '` is not a valid unit. ' + utils.QUICK_HELP)
if ut_to_std is False: if not ut_to_std:
results.append('`' + unit_to + '` is not a valid unit.' + utils.QUICK_HELP) results.append('`' + unit_to + '` is not a valid unit.' + utils.QUICK_HELP)
if uf_to_std is False or ut_to_std is False: if not uf_to_std or not ut_to_std:
continue continue
base_unit = uf_to_std[2] base_unit = uf_to_std[2]

View file

@ -5,15 +5,15 @@ from zulip_bots.test_lib import StubBotTestCase
class TestConverterBot(StubBotTestCase): class TestConverterBot(StubBotTestCase):
bot_name = "converter" bot_name = "converter"
def test_bot(self): def test_bot(self) -> None:
dialog = [ dialog = [
("", 'Too few arguments given. Enter `@convert help` ' ("", 'Too few arguments given. Enter `@convert help` '
'for help on using the converter.\n'), 'for help on using the converter.\n'),
("foo bar", 'Too few arguments given. Enter `@convert help` ' ("foo bar", 'Too few arguments given. Enter `@convert help` '
'for help on using the converter.\n'), 'for help on using the converter.\n'),
("2 m cm", "2.0 m = 200.0 cm\n"), ("2 m cm", "2 m = 200.0 cm\n"),
("12.0 celsius fahrenheit", "12.0 celsius = 53.600054 fahrenheit\n"), ("12.0 celsius fahrenheit", "12.0 celsius = 53.600054 fahrenheit\n"),
("0.002 kilometer millimile", "0.002 kilometer = 1.2427424 millimile\n"), ("0.002 kilometer millimile", "0.002 kilometer = 1.2427424 millimile\n"),
("3 megabyte kilobit", "3.0 megabyte = 24576.0 kilobit\n"), ("3 megabyte kilobit", "3 megabyte = 24576.0 kilobit\n"),
] ]
self.verify_dialog(dialog) self.verify_dialog(dialog)