diff --git a/zulip_bots/zulip_bots/bots/witai/witai.py b/zulip_bots/zulip_bots/bots/witai/witai.py index 24ea608..05a874a 100644 --- a/zulip_bots/zulip_bots/bots/witai/witai.py +++ b/zulip_bots/zulip_bots/bots/witai/witai.py @@ -3,6 +3,7 @@ from typing import Dict, Any, Optional, Callable from zulip_bots.lib import BotHandler import wit +import importlib.abc import importlib.util class WitaiHandler: @@ -76,10 +77,10 @@ def get_handle(location: str) -> Optional[Callable[[Dict[str, Any]], Optional[st spec = importlib.util.spec_from_file_location('module.name', location) handler = importlib.util.module_from_spec(spec) loader = spec.loader - if loader is None: + if not isinstance(loader, importlib.abc.Loader): return None - loader.exec_module(handler) # type: ignore # FIXME: typeshed issue? - return handler.handle # type: ignore + loader.exec_module(handler) + return getattr(handler, "handle") except Exception as e: print(e) return None diff --git a/zulip_bots/zulip_bots/finder.py b/zulip_bots/zulip_bots/finder.py index ec6d758..3b9f5be 100644 --- a/zulip_bots/zulip_bots/finder.py +++ b/zulip_bots/zulip_bots/finder.py @@ -1,4 +1,5 @@ import importlib +import importlib.abc import importlib.util import os from typing import Any, Optional, Text, Tuple @@ -10,9 +11,9 @@ def import_module_from_source(path: Text, name: Text) -> Any: spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) loader = spec.loader - if loader is None: + if not isinstance(loader, importlib.abc.Loader): return None - loader.exec_module(module) # type: ignore # FIXME: typeshed issue? + loader.exec_module(module) return module def import_module_by_name(name: Text) -> Any: diff --git a/zulip_botserver/zulip_botserver/server.py b/zulip_botserver/zulip_botserver/server.py index ed5a048..9665979 100644 --- a/zulip_botserver/zulip_botserver/server.py +++ b/zulip_botserver/zulip_botserver/server.py @@ -5,6 +5,7 @@ import logging import json import os import sys +import importlib.abc import importlib.util from collections import OrderedDict @@ -102,9 +103,8 @@ def load_module_from_file(file_path: str) -> ModuleType: # Wrapper around importutil; see https://stackoverflow.com/a/67692/3909240. spec = importlib.util.spec_from_file_location("custom_bot_module", file_path) lib_module = importlib.util.module_from_spec(spec) - assert spec is not None - assert spec.loader is not None - spec.loader.exec_module(lib_module) # type: ignore # FIXME: typeshed issue? + assert isinstance(spec.loader, importlib.abc.Loader) + spec.loader.exec_module(lib_module) return lib_module def load_lib_modules(available_bots: List[str]) -> Dict[str, Any]: