Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Being more explicit with the import of importlib.util. #181

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/fibad/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import random
import re
from importlib import util as importlib_util
from pathlib import Path
from typing import Optional, Union

Expand Down Expand Up @@ -155,7 +156,7 @@ def _find_external_library_default_config_paths(runtime_config: dict) -> set:
else:
if key == "name" and "." in value:
external_library = value.split(".")[0]
if importlib.util.find_spec(external_library) is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK that is really weird.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if some package deep in fibad or KBMOD's dep tree is conditionally replacing functions in importlib.util?

Copy link
Collaborator Author

@drewoldag drewoldag Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we start python on the command line and run the following on baldur in an environment that shares a kbmod installation

>>> import importlib
>>> importlib.ut <attempt tab complete, nothing happens>
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'

It does seem unpleasant, but it seems to work.

Outside of a KBMOD environment, there's no problem with:

>>> import importlib
>>> importlib.util

if importlib_util.find_spec(external_library) is not None:
try:
lib = importlib.import_module(external_library)
if lib.__file__ is None:
Expand Down
7 changes: 4 additions & 3 deletions src/fibad/plugin_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
from importlib import util as importlib_util
from typing import Any, Optional, TypeVar, Union

T = TypeVar("T")
Expand Down Expand Up @@ -71,10 +72,10 @@ def import_module_from_string(module_path: str) -> Any:
try:
# Attempt to find the module spec, i.e. `module.submodule.`.
# Will raise exception if `submodule`, 'subsubmodule', etc. is not found.
importlib.util.find_spec(module_name)
importlib_util.find_spec(module_name)

# `importlib.util.find_spec()` will return None if `module` is not found.
if (importlib.util.find_spec(module_name)) is not None:
# `importlib_util.find_spec()` will return None if `module` is not found.
if (importlib_util.find_spec(module_name)) is not None:
# Load the requested module
module = importlib.import_module(module_name)

Expand Down
Loading