Skip to content

Commit

Permalink
Correct attribute error on module spec loader
Browse files Browse the repository at this point in the history
The loader attribute on a module spec is technically optional, so mypy
raises a `[union-attr]` error:

Item "_Loader" of "Optional[_Loader]" has no attribute "exec_module"

To avoid referencing the potentially undefined `_Loader.exec_module`
attribute, this commit will update the syntax to get the attribute with
`getattr(spec.loader, "exec_module")` prior to running `exec_module()`.
An `AttributeError` will be raised if the attribute does not exist.
  • Loading branch information
br3ndonland committed Apr 18, 2021
1 parent 8cd8db2 commit 0ba94fc
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion inboard/logging_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def configure_logging(
if not spec:
raise ImportError(f"Unable to import {logging_conf}")
logging_conf_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(logging_conf_module) # type: ignore[union-attr]
exec_module = getattr(spec.loader, "exec_module")
exec_module(logging_conf_module)
if not hasattr(logging_conf_module, "LOGGING_CONFIG"):
raise AttributeError(f"No LOGGING_CONFIG in {logging_conf_module.__name__}")
logging_conf_dict = getattr(logging_conf_module, "LOGGING_CONFIG")
Expand Down

0 comments on commit 0ba94fc

Please sign in to comment.