Skip to content
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
2 changes: 1 addition & 1 deletion skops/io/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def whichmodule(obj: Any, name: str) -> str:
warnings.simplefilter("ignore", DeprecationWarning)
if _getattribute(module, name)[0] is obj:
return module_name
except AttributeError:
except (AttributeError, ImportError):
pass
return "__main__"

Expand Down
18 changes: 17 additions & 1 deletion skops/io/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sys
from types import ModuleType

import numpy as np
import pytest
import scipy
import sklearn.tree

from skops.io._utils import get_type_name, get_type_paths
from skops.io._utils import get_type_name, get_type_paths, whichmodule


class UserDefinedClass:
Expand Down Expand Up @@ -74,3 +77,16 @@ class TestConvertTypesToStrings:
)
def test_for_normal_input_lists_returns_as_expected(self, input_list, output_list):
assert get_type_paths(input_list) == output_list


def test_whichmodule_ignores_import_errors_from_lazy_modules(monkeypatch):
module = ModuleType("lazy_module_for_skops_tests")

def _getattr(name):
raise ModuleNotFoundError("No module named 'torchvision'")

module.__getattr__ = _getattr
monkeypatch.setitem(sys.modules, module.__name__, module)

obj = type("T", (), {"__module__": None, "__name__": "target"})()
assert whichmodule(obj, obj.__name__) == "__main__"