11"""Utils."""
2+
23import importlib
34from typing import Any , TypeVar
45
@@ -28,12 +29,13 @@ def detect_device() -> str:
2829
2930
3031def _is_package_available (pkg_name : str ) -> bool :
31- package_exists = importlib .util .find_spec (pkg_name ) is not None
32- return package_exists
32+ return importlib .util .find_spec (pkg_name ) is not None
3333
3434
3535def _requires_package (
36- obj : Any , package_name : str , group_name : str ,
36+ obj : Any , # noqa: ANN401
37+ package_name : str ,
38+ group_name : str ,
3739) -> None :
3840 """Check if a package is available and raise an error with installation instructions if it's not.
3941
@@ -44,14 +46,11 @@ def _requires_package(
4446 """
4547 if _is_package_available (package_name ):
4648 return
47- install_instruction = (
48- f"pip install { package_name } "
49- )
50- group_install_instruction = (
51- f"pip install autointent[{ group_name } ]"
52- )
49+ install_instruction = f"pip install { package_name } "
50+ group_install_instruction = f"pip install autointent[{ group_name } ]"
5351 name = obj .__name__ if hasattr (obj , "__name__" ) else obj .__class__ .__name__
54- raise ImportError (
52+ msg = (
5553 f"{ name } requires the `{ package_name } ` library but it was not found in your environment. "
56- + f"If you want to load { model_name } models, please run `{ group_install_instruction } ` or `{ install_instruction } ` to install the package."
57- )
54+ f"Please run `{ group_install_instruction } ` or `{ install_instruction } ` to install the package."
55+ )
56+ raise ImportError (msg )
0 commit comments