mypy: Fix exec_module type: ignore comments.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-03-04 15:43:59 -08:00
parent f2e2f1c7ff
commit 19f5b4f6a6
3 changed files with 10 additions and 8 deletions

View file

@ -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

View file

@ -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:

View file

@ -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]: