From b4bbd833351102fd68ccc9488e048717b0c6f3ec Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Fri, 22 Dec 2017 10:35:53 -0800 Subject: [PATCH] mypy: Amend witai bot, to pass with strict-optional. Including additional error-handling, associated with None-checks. --- zulip_bots/zulip_bots/bots/witai/witai.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/witai/witai.py b/zulip_bots/zulip_bots/bots/witai/witai.py index 3407cb7..c2486b7 100644 --- a/zulip_bots/zulip_bots/bots/witai/witai.py +++ b/zulip_bots/zulip_bots/bots/witai/witai.py @@ -24,7 +24,11 @@ class WitaiHandler(object): handler_location = config.get('handler_location') if not handler_location: raise KeyError('No `handler_location` was specified') - self.handle = get_handle(handler_location) + handle = get_handle(handler_location) + if handle is None: + raise Exception('Could not get handler from handler_location.') + else: + self.handle = handle help_message = config.get('help_message') if not help_message: @@ -53,7 +57,7 @@ class WitaiHandler(object): handler_class = WitaiHandler -def get_handle(location: str) -> Callable[[Dict[str, Any]], Optional[str]]: +def get_handle(location: str) -> Optional[Callable[[Dict[str, Any]], Optional[str]]]: '''Returns a function to be used when generating a response from Wit.ai bot. This function is the function named `handle` in the module at the given `location`. For an example of a `handle` function, see `doc.md`. @@ -71,7 +75,10 @@ def get_handle(location: str) -> Callable[[Dict[str, Any]], Optional[str]]: try: spec = importlib.util.spec_from_file_location('module.name', location) handler = importlib.util.module_from_spec(spec) - spec.loader.exec_module(handler) + loader = spec.loader + if loader is None: + return None + loader.exec_module(handler) return handler.handle # type: ignore except Exception as e: print(e)