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 typing import Dict, Any, Optional, Callable
from zulip_bots.lib import BotHandler from zulip_bots.lib import BotHandler
import wit import wit
import importlib.abc
import importlib.util import importlib.util
class WitaiHandler: 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) spec = importlib.util.spec_from_file_location('module.name', location)
handler = importlib.util.module_from_spec(spec) handler = importlib.util.module_from_spec(spec)
loader = spec.loader loader = spec.loader
if loader is None: if not isinstance(loader, importlib.abc.Loader):
return None return None
loader.exec_module(handler) # type: ignore # FIXME: typeshed issue? loader.exec_module(handler)
return handler.handle # type: ignore return getattr(handler, "handle")
except Exception as e: except Exception as e:
print(e) print(e)
return None return None

View file

@ -1,4 +1,5 @@
import importlib import importlib
import importlib.abc
import importlib.util import importlib.util
import os import os
from typing import Any, Optional, Text, Tuple 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) spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec) module = importlib.util.module_from_spec(spec)
loader = spec.loader loader = spec.loader
if loader is None: if not isinstance(loader, importlib.abc.Loader):
return None return None
loader.exec_module(module) # type: ignore # FIXME: typeshed issue? loader.exec_module(module)
return module return module
def import_module_by_name(name: Text) -> Any: def import_module_by_name(name: Text) -> Any:

View file

@ -5,6 +5,7 @@ import logging
import json import json
import os import os
import sys import sys
import importlib.abc
import importlib.util import importlib.util
from collections import OrderedDict 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. # Wrapper around importutil; see https://stackoverflow.com/a/67692/3909240.
spec = importlib.util.spec_from_file_location("custom_bot_module", file_path) spec = importlib.util.spec_from_file_location("custom_bot_module", file_path)
lib_module = importlib.util.module_from_spec(spec) lib_module = importlib.util.module_from_spec(spec)
assert spec is not None assert isinstance(spec.loader, importlib.abc.Loader)
assert spec.loader is not None spec.loader.exec_module(lib_module)
spec.loader.exec_module(lib_module) # type: ignore # FIXME: typeshed issue?
return lib_module return lib_module
def load_lib_modules(available_bots: List[str]) -> Dict[str, Any]: def load_lib_modules(available_bots: List[str]) -> Dict[str, Any]: