typing: Convert function type annotations to Python 3 style.

Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and two fixes for use-before-define
issues:

-    def set_zulip_client(self, zulipToJabberClient: ZulipToJabberBot) -> None:
+    def set_zulip_client(self, zulipToJabberClient: 'ZulipToJabberBot') -> None:

-def init_from_options(options: Any, client: Optional[str] = None) -> Client:
+def init_from_options(options: Any, client: Optional[str] = None) -> 'Client':

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2020-04-18 15:59:12 -07:00 committed by Tim Abbott
parent 7c5f73dce9
commit 5428c5f296
42 changed files with 311 additions and 577 deletions

View file

@ -78,8 +78,7 @@ except ImportError:
from importlib import import_module
# Manual dependency check
def check_dependency_manually(module_name, version=None):
# type: (str, Optional[str]) -> None
def check_dependency_manually(module_name: str, version: Optional[str] = None) -> None:
try:
module = import_module(module_name) # type: Any
if version is not None:

View file

@ -9,16 +9,14 @@ import glob
import pip
from typing import Iterator
def get_bot_paths():
# type: () -> Iterator[str]
def get_bot_paths() -> Iterator[str]:
current_dir = os.path.dirname(os.path.abspath(__file__))
bots_dir = os.path.join(current_dir, "bots")
bots_subdirs = map(lambda d: os.path.abspath(d), glob.glob(bots_dir + '/*'))
paths = filter(lambda d: os.path.isdir(d), bots_subdirs)
return paths
def provision_bot(path_to_bot, force):
# type: (str, bool) -> None
def provision_bot(path_to_bot: str, force: bool) -> None:
req_path = os.path.join(path_to_bot, 'requirements.txt')
if os.path.isfile(req_path):
bot_name = os.path.basename(path_to_bot)
@ -36,8 +34,7 @@ def provision_bot(path_to_bot, force):
logging.info('Installed dependencies successfully.')
def parse_args(available_bots):
# type: (Iterator[str]) -> argparse.Namespace
def parse_args(available_bots: Iterator[str]) -> argparse.Namespace:
usage = """
Installs dependencies of bots in the bots/<bot_name>
directories. Add a requirements.txt file in a bot's folder
@ -71,8 +68,7 @@ Example: ./provision.py helloworld xkcd wikipedia
return parser.parse_args()
def main():
# type: () -> None
def main() -> None:
options = parse_args(available_bots=get_bot_paths())
if not options.quiet:

View file

@ -7,8 +7,7 @@ from unittest.mock import patch
from typing import Any, Dict, List
@contextmanager
def mock_http_conversation(http_data):
# type: (Dict[str, Any]) -> Any
def mock_http_conversation(http_data: Dict[str, Any]) -> Any:
"""
Use this context manager to mock and verify a bot's HTTP
requests to the third-party API (and provide the correct
@ -18,8 +17,7 @@ def mock_http_conversation(http_data):
http_data should be fixtures data formatted like the data
in zulip_bots/zulip_bots/bots/giphy/fixtures/test_normal.json
"""
def get_response(http_response, http_headers, is_raw_response):
# type: (Dict[str, Any], Dict[str, Any], bool) -> Any
def get_response(http_response: Dict[str, Any], http_headers: Dict[str, Any], is_raw_response: bool) -> Any:
"""Creates a fake `requests` Response with a desired HTTP response and
response headers.
"""
@ -31,8 +29,7 @@ def mock_http_conversation(http_data):
mock_result.status_code = http_headers.get('status', 200)
return mock_result
def assert_called_with_fields(mock_result, http_request, fields, meta):
# type: (Any, Dict[str, Any], List[str], Dict[str, Any]) -> None
def assert_called_with_fields(mock_result: Any, http_request: Dict[str, Any], fields: List[str], meta: Dict[str, Any]) -> None:
"""Calls `assert_called_with` on a mock object using an HTTP request.
Uses `fields` to determine which keys to look for in HTTP request and
to test; if a key is in `fields`, e.g., 'headers', it will be used in
@ -101,10 +98,8 @@ def mock_http_conversation(http_data):
)
@contextmanager
def mock_request_exception():
# type: () -> Any
def assert_mock_called(mock_result):
# type: (Any) -> None
def mock_request_exception() -> Any:
def assert_mock_called(mock_result: Any) -> None:
assert mock_result.called
with patch('requests.get') as mock_get:

View file

@ -15,8 +15,7 @@ directory structure is currently:
fixtures/
'''
def get_bot_message_handler(bot_name):
# type: (str) -> Any
def get_bot_message_handler(bot_name: str) -> Any:
# message_handler is of type 'Any', since it can contain any bot's
# handler class. Eventually, we want bot's handler classes to
# inherit from a common prototype specifying the handle_message
@ -24,8 +23,7 @@ def get_bot_message_handler(bot_name):
lib_module = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot=bot_name)) # type: Any
return lib_module.handler_class()
def read_bot_fixture_data(bot_name, test_name):
# type: (str, str) -> Dict[str, Any]
def read_bot_fixture_data(bot_name: str, test_name: str) -> Dict[str, Any]:
base_path = os.path.realpath(os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'bots', bot_name, 'fixtures'))
http_data_path = os.path.join(base_path, '{}.json'.format(test_name))

View file

@ -53,13 +53,11 @@ class StubBotHandler:
def update_message(self, message: Dict[str, Any]) -> None:
self.message_server.update(message)
def upload_file_from_path(self, file_path):
# type: (str) -> Dict[str, Any]
def upload_file_from_path(self, file_path: str) -> Dict[str, Any]:
with open(file_path, 'rb') as file:
return self.message_server.upload_file(file)
def upload_file(self, file):
# type: (IO[Any]) -> Dict[str, Any]
def upload_file(self, file: IO[Any]) -> Dict[str, Any]:
return self.message_server.upload_file(file)
class BotQuitException(Exception):
@ -132,8 +130,7 @@ class BotTestCase(unittest.TestCase):
return bot, bot_handler
def get_response(self, message):
# type: (Dict[str, Any]) -> Dict[str, Any]
def get_response(self, message: Dict[str, Any]) -> Dict[str, Any]:
bot, bot_handler = self._get_handlers()
bot_handler.reset_transcript()
bot.handle_message(message, bot_handler)

View file

@ -56,8 +56,7 @@ class TestDefaultArguments(TestCase):
expected_bot_dir_path = '/path/to'
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_path, bot_dir_path=expected_bot_dir_path)
def _test_adding_bot_parent_dir_to_sys_path(self, bot_qualifier, bot_dir_path):
# type: (str, str) -> None
def _test_adding_bot_parent_dir_to_sys_path(self, bot_qualifier: str, bot_dir_path: str) -> None:
with patch('sys.argv', ['zulip-run-bot', bot_qualifier, '--config-file', '/path/to/config']):
with patch('zulip_bots.finder.import_module_from_source', return_value=mock.Mock()):
with patch('zulip_bots.run.run_message_handler_for_bot'):