mypy: Amend witai bot, to pass with strict-optional.

Including additional error-handling, associated with None-checks.
This commit is contained in:
neiljp (Neil Pilgrim) 2017-12-22 10:35:53 -08:00 committed by showell
parent 8b4e877546
commit b4bbd83335

View file

@ -24,7 +24,11 @@ class WitaiHandler(object):
handler_location = config.get('handler_location') handler_location = config.get('handler_location')
if not handler_location: if not handler_location:
raise KeyError('No `handler_location` was specified') 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') help_message = config.get('help_message')
if not help_message: if not help_message:
@ -53,7 +57,7 @@ class WitaiHandler(object):
handler_class = WitaiHandler 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 '''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 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`. 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: try:
spec = importlib.util.spec_from_file_location('module.name', location) spec = importlib.util.spec_from_file_location('module.name', location)
handler = importlib.util.module_from_spec(spec) 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 return handler.handle # type: ignore
except Exception as e: except Exception as e:
print(e) print(e)