From 88ae9d04eeeeff27dfd338e0a28f832673460dc2 Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Fri, 22 Dec 2017 11:18:45 -0800 Subject: [PATCH] mypy: Amend import_module_from_source, to pass with strict-optional. Including additional error-reporting. --- zulip_bots/zulip_bots/run.py | 9 ++++++++- zulip_bots/zulip_bots/terminal.py | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/zulip_bots/zulip_bots/run.py b/zulip_bots/zulip_bots/run.py index befb1cd..126765e 100755 --- a/zulip_bots/zulip_bots/run.py +++ b/zulip_bots/zulip_bots/run.py @@ -36,7 +36,10 @@ def import_module_from_source(path, name=None): import importlib.util spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) + loader = spec.loader + if loader is None: + return None + loader.exec_module(module) return module @@ -123,7 +126,11 @@ def main(): bot_name = args.bot if args.provision: provision_bot(os.path.dirname(bot_path), args.force) + lib_module = import_module_from_source(bot_path, bot_name) + if lib_module is None: + print("ERROR: Could not load bot module. Exiting now.") + sys.exit(1) if not args.quiet: logging.basicConfig(stream=sys.stdout, level=logging.INFO) diff --git a/zulip_bots/zulip_bots/terminal.py b/zulip_bots/zulip_bots/terminal.py index 2759bf1..e7f4f42 100644 --- a/zulip_bots/zulip_bots/terminal.py +++ b/zulip_bots/zulip_bots/terminal.py @@ -40,6 +40,8 @@ def main(): try: lib_module = import_module_from_source(bot_path, bot_name) + if lib_module is None: + raise IOError except IOError: print("Could not find and import bot '{}'".format(bot_name)) sys.exit(1)