mypy: Add annotations for weather.

This commit is contained in:
fredfishgames 2017-12-08 12:31:42 +00:00 committed by showell
parent f7f54d159f
commit ab9128d939
3 changed files with 13 additions and 7 deletions

View file

@ -48,6 +48,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/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/weather.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

@ -5,10 +5,12 @@ from __future__ import print_function
from zulip_bots.test_lib import BotTestCase from zulip_bots.test_lib import BotTestCase
from typing import Any
class TestWeatherBot(BotTestCase): class TestWeatherBot(BotTestCase):
bot_name = "weather" bot_name = "weather"
def test_bot(self): def test_bot(self) -> None:
# City query # City query
bot_response = "Weather in New York, US:\n71.33 F / 21.85 C\nMist" bot_response = "Weather in New York, US:\n71.33 F / 21.85 C\nMist"

View file

@ -3,17 +3,19 @@ from __future__ import print_function
import requests import requests
import json import json
from typing import Any, Dict
class WeatherHandler(object): class WeatherHandler(object):
def initialize(self, bot_handler): def initialize(self, bot_handler: Any) -> None:
self.api_key = bot_handler.get_config_info('weather')['key'] self.api_key = bot_handler.get_config_info('weather')['key']
self.response_pattern = 'Weather in {}, {}:\n{:.2f} F / {:.2f} C\n{}' self.response_pattern = 'Weather in {}, {}:\n{:.2f} F / {:.2f} C\n{}'
def usage(self): def usage(self) -> str:
return ''' return '''
This plugin will give info about weather in a specified city This plugin will give info about weather in a specified city
''' '''
def handle_message(self, message, bot_handler): def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
help_content = ''' help_content = '''
This bot returns weather info for specified city. This bot returns weather info for specified city.
You specify city in the following format: You specify city in the following format:
@ -37,7 +39,7 @@ class WeatherHandler(object):
bot_handler.send_reply(message, response) bot_handler.send_reply(message, response)
def format_response(text, city, response_pattern): def format_response(text: Any, city: str, response_pattern: str) -> str:
j = text.json() j = text.json()
city = j['name'] city = j['name']
country = j['sys']['country'] country = j['sys']['country']
@ -48,11 +50,11 @@ def format_response(text, city, response_pattern):
return response_pattern.format(city, country, fahrenheit, celsius, description) return response_pattern.format(city, country, fahrenheit, celsius, description)
def to_celsius(temp_kelvin): def to_celsius(temp_kelvin: float) -> float:
return int(temp_kelvin) - 273.15 return int(temp_kelvin) - 273.15
def to_fahrenheit(temp_kelvin): def to_fahrenheit(temp_kelvin: float) -> float:
return int(temp_kelvin) * (9. / 5.) - 459.67 return int(temp_kelvin) * (9. / 5.) - 459.67
handler_class = WeatherHandler handler_class = WeatherHandler