From 2ba6f75fb3e8c899b7ac4711a76fc7af7ba35285 Mon Sep 17 00:00:00 2001 From: Guitar1st Date: Mon, 14 May 2018 19:45:54 +0000 Subject: [PATCH] zulip_botserver: Change style of type annotations to python3 --- zulip_botserver/tests/server_test_lib.py | 20 +++++++++---------- zulip_botserver/tests/test_server.py | 18 ++++++----------- zulip_botserver/zulip_botserver/server.py | 24 +++++++++-------------- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/zulip_botserver/tests/server_test_lib.py b/zulip_botserver/tests/server_test_lib.py index 70ecaff..b649f08 100644 --- a/zulip_botserver/tests/server_test_lib.py +++ b/zulip_botserver/tests/server_test_lib.py @@ -6,21 +6,19 @@ from zulip_botserver import server class BotServerTestCase(TestCase): - def setUp(self): - # type: () -> None + def setUp(self) -> None: server.app.testing = True self.app = server.app.test_client() def assert_bot_server_response(self, - available_bots=None, - bots_config=None, - bots_lib_module=None, - bot_handlers=None, - payload_url="/bots/helloworld", - message=dict(message={'key': "test message"}), - check_success=False, - ): - # type: (Optional[List[str]], Optional[Dict[str, Any]], Optional[Dict[str, Any]], Optional[Dict[str, Any]], str, Dict[str, Dict[str, Any]], bool) -> None + available_bots: Optional[List[str]]=None, + bots_config: Optional[Dict[str, Any]]=None, + bots_lib_module: Optional[Dict[str, Any]]=None, + bot_handlers: Optional[Dict[str, Any]]=None, + payload_url: str="/bots/helloworld", + message: Optional[Dict[str, Any]]=dict(message={'key': "test message"}), + check_success: bool=False, + ) -> None: if available_bots is not None: server.available_bots = available_bots server.bots_config = bots_config # type: ignore # monkey-patching diff --git a/zulip_botserver/tests/test_server.py b/zulip_botserver/tests/test_server.py index a295ff6..d4c6ec1 100644 --- a/zulip_botserver/tests/test_server.py +++ b/zulip_botserver/tests/test_server.py @@ -1,24 +1,20 @@ -from __future__ import absolute_import import mock +from typing import Any, Dict import unittest -from typing import Any from .server_test_lib import BotServerTestCase import six class BotServerTests(BotServerTestCase): class MockMessageHandler(object): - def handle_message(self, message, bot_handler): - # type: (Any, Any, Any) -> None + def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None: assert message == {'key': "test message"} class MockLibModule(object): - def handler_class(self): - # type: () -> Any + def handler_class(self) -> Any: return BotServerTests.MockMessageHandler() @mock.patch('zulip_botserver.server.ExternalBotHandler') - def test_successful_request(self, mock_ExternalBotHandler): - # type: (mock.Mock) -> None + def test_successful_request(self, mock_ExternalBotHandler: mock.Mock) -> None: available_bots = ['helloworld'] bots_config = { 'helloworld': { @@ -31,16 +27,14 @@ class BotServerTests(BotServerTestCase): bots_config=bots_config, check_success=True) - def test_bot_module_not_exists(self): - # type: () -> None + def test_bot_module_not_exists(self) -> None: self.assert_bot_server_response(available_bots=[], payload_url="/bots/not_supported_bot", check_success=False) @mock.patch('logging.error') @mock.patch('zulip_bots.lib.StateHandler') - def test_wrong_bot_credentials(self, mock_StateHandler, mock_LoggingError): - # type: (mock.Mock, mock.Mock) -> None + def test_wrong_bot_credentials(self, mock_StateHandler: mock.Mock, mock_LoggingError: mock.Mock) -> None: available_bots = ['nonexistent-bot'] bots_config = { 'nonexistent-bot': { diff --git a/zulip_botserver/zulip_botserver/server.py b/zulip_botserver/zulip_botserver/server.py index cab082c..03db830 100644 --- a/zulip_botserver/zulip_botserver/server.py +++ b/zulip_botserver/zulip_botserver/server.py @@ -5,7 +5,7 @@ import argparse from flask import Flask, request from importlib import import_module -from typing import Any, Dict, Mapping, Union, List +from typing import Any, Dict, Mapping, Union, List, Optional from werkzeug.exceptions import BadRequest from six.moves.configparser import SafeConfigParser @@ -18,8 +18,7 @@ available_bots = [] # type: List[str] bots_lib_module = {} # type: Dict[str, Any] bot_handlers = {} # type: Dict[str, ExternalBotHandler] -def read_config_file(config_file_path): - # type: (str) -> None +def read_config_file(config_file_path: str) -> None: config_file_path = os.path.abspath(os.path.expanduser(config_file_path)) if not os.path.isfile(config_file_path): raise IOError("Could not read config file {}: File not found.".format(config_file_path)) @@ -33,8 +32,7 @@ def read_config_file(config_file_path): "site": parser.get(section, 'site'), } -def load_lib_modules(): - # type: () -> None +def load_lib_modules() -> None: for bot in available_bots: try: module_name = 'zulip_bots.bots.{bot}.{bot}'.format(bot=bot) @@ -44,8 +42,7 @@ def load_lib_modules(): raise ImportError("\n Import Error: Bot \"{}\" doesn't exists. Please make sure you have set up the flaskbotrc " "file correctly.\n".format(bot)) -def load_bot_handlers(): - # type: () -> Any +def load_bot_handlers() -> Optional[BadRequest]: for bot in available_bots: client = Client(email=bots_config[bot]["email"], api_key=bots_config[bot]["key"], @@ -77,9 +74,9 @@ def load_bot_handlers(): except SystemExit: return BadRequest("Cannot fetch user profile for bot {}, make sure you have set up the flaskbotrc " "file correctly.".format(bot)) + return None -def get_bot_lib_module(bot): - # type: (str) -> Any +def get_bot_lib_module(bot: str) -> Any: if bot in bots_lib_module.keys(): return bots_lib_module[bot] return None @@ -87,8 +84,7 @@ def get_bot_lib_module(bot): app = Flask(__name__) @app.route('/bots/', methods=['POST']) -def handle_bot(bot): - # type: (str) -> Union[str, BadRequest] +def handle_bot(bot: str) -> Union[str, BadRequest]: lib_module = get_bot_lib_module(bot) if lib_module is None: return BadRequest("Can't find the configuration or Bot Handler code for bot {}. " @@ -101,8 +97,7 @@ def handle_bot(bot): bot_handler=bot_handlers[bot]) return json.dumps("") -def parse_args(): - # type: () -> argparse.Namespace +def parse_args() -> argparse.Namespace: usage = ''' zulip-bot-server --config-file --hostname
--port Example: zulip-bot-server --config-file ~/flaskbotrc @@ -133,8 +128,7 @@ def parse_args(): return parser.parse_args() -def main(): - # type: () -> None +def main() -> None: options = parse_args() read_config_file(options.config_file) global available_bots