@@ -77,7 +77,7 @@ def validate_ids(id_list: list[str]) -> list[str]:
7777
7878class LazyImport :
7979 """Lazily import and load an object.
80-
80+
8181 This class is super lazy, in that it lazily imports and caches an object.
8282 If the object is a function, the function itself will be cached.
8383
@@ -90,38 +90,52 @@ class LazyImport:
9090 A dot-separated, import-like string.
9191 """
9292
93- __slots__ = ["_module_name" , "_class_name" , "_obj" ,"_imported" ]
93+ __slots__ = ["_module_name" , "_class_name" , "_obj" , "_imported" ]
94+
95+ def __init__ (
96+ self ,
97+ import_str : str ,
98+ ) -> None :
99+ """Initialize a lazily imported object.
94100
95- def __init__ (self , import_str : str ,) -> None :
96- if len (
97- splitted := import_str .rsplit ("." ,1 )
98- ) > 1 :
99- self ._module_name , self ._class_name = splitted
101+ Parameters
102+ -----------
103+ import_str : str
104+ A dot-separated, import-like string.
105+ """
106+ if len (split_import_str := import_str .rsplit ("." , 1 )) > 1 :
107+ self ._module_name , self ._class_name = split_import_str
100108 else :
101- self ._module_name = splitted [0 ]
109+ self ._module_name = split_import_str [0 ]
102110 self ._class_name = None
103111
104- self ._imported : Any | None = None
105- self ._obj : Any | None = None
112+ self ._imported : Any | None = None
113+ self ._obj : Any | None = None
106114
107115 def __str__ (self ) -> str :
108- return f"LazyImport of { self ._module_name } " + (f".{ self ._class_name } " if self ._class_name else "" )
116+ return f"LazyImport of { self ._module_name } " + (
117+ f".{ self ._class_name } " if self ._class_name else ""
118+ )
109119
110120 def __repr__ (self ) -> str :
111121 return self .__str__ ()
112122
113- def _load (self ,) -> None :
123+ def _load (
124+ self ,
125+ ) -> None :
114126 try :
115127 _imported = import_module (self ._module_name )
116128 if self ._class_name :
117129 _imported = getattr (_imported , self ._class_name )
118130 self ._imported = _imported
119131 except Exception as exc :
120- raise ImportError (f"Failed to import { self ._module_name } .{ self ._class_name } :\n { exc } " )
132+ raise ImportError (
133+ f"Failed to import { self ._module_name } .{ self ._class_name } :\n { exc } "
134+ )
121135
122136 def __call__ (self , * args , ** kwargs ) -> Any :
123137 """Call a function or (re-)initialize a class.
124-
138+
125139 If the object itself has not been imported, this will first import it.
126140
127141 If the object is a class, it will be initialized, cached, and returned.
@@ -132,20 +146,19 @@ def __call__(self, *args, **kwargs) -> Any:
132146 if self ._imported is None :
133147 self ._load ()
134148
135- if isinstance (self ._imported ,type ):
149+ if isinstance (self ._imported , type ):
136150 self ._obj = self ._imported (* args , ** kwargs )
137151 return self ._obj
138152 else :
139153 self ._obj = self ._imported
140- return self ._obj (* args ,** kwargs )
154+ return self ._obj (* args , ** kwargs )
141155
142- def __getattr__ (self , v : str ) -> Any :
156+ def __getattr__ (self , v : str ) -> Any :
143157 """Get an attribute on a super lazy object."""
144158 if self ._obj is not None and hasattr (self ._obj , v ):
145159 return getattr (self ._obj , v )
146160
147161 if self ._imported is None :
148162 self ._load ()
149- if hasattr (self ._imported ,v ):
150- return getattr (self ._imported ,v )
151-
163+ if hasattr (self ._imported , v ):
164+ return getattr (self ._imported , v )
0 commit comments