55from typing import TYPE_CHECKING , Any , ClassVar
66from weakref import WeakSet
77
8- from . import _lbug_capi as _lbug
8+ from ._backend import get_capi_module , get_pybind_module
99from .types import Type
1010
11- try :
12- from . import _lbug as _lbug_pybind
13- except (
14- ImportError
15- ): # pragma: no cover - pybind module may be unavailable in some builds
16- _lbug_pybind = None
17-
1811if TYPE_CHECKING :
1912 import sys
2013 from types import TracebackType
@@ -157,12 +150,13 @@ def _resolve_backend_preference(cls, backend: str) -> str:
157150 def _should_use_pybind_backend (self ) -> bool :
158151 if self .backend == "capi" :
159152 return False
153+ pybind_module = get_pybind_module ()
160154 if self .backend == "pybind" :
161- if _lbug_pybind is None :
155+ if pybind_module is None :
162156 msg = "Requested pybind backend, but ladybug._lbug is not available."
163157 raise RuntimeError (msg )
164158 return True
165- return _lbug_pybind is not None
159+ return pybind_module is not None
166160
167161 def __enter__ (self ) -> Self :
168162 return self
@@ -185,7 +179,7 @@ def get_version() -> str:
185179 str
186180 The version of the database.
187181 """
188- return _lbug .Database .get_version () # type: ignore[union-attr]
182+ return get_capi_module () .Database .get_version ()
189183
190184 @staticmethod
191185 def get_storage_version () -> int :
@@ -197,7 +191,7 @@ def get_storage_version() -> int:
197191 int
198192 The storage version of the database.
199193 """
200- return _lbug .Database .get_storage_version () # type: ignore[union-attr]
194+ return get_capi_module () .Database .get_storage_version ()
201195
202196 def __getstate__ (self ) -> dict [str , Any ]:
203197 state = {
@@ -217,7 +211,7 @@ def init_database(self) -> None:
217211 if self ._use_pybind_backend :
218212 self ._database = self .init_pybind_database ()
219213 else :
220- self ._database = _lbug .Database ( # type: ignore[union-attr]
214+ self ._database = get_capi_module () .Database (
221215 self .database_path ,
222216 self .buffer_pool_size ,
223217 self .max_num_threads ,
@@ -234,10 +228,11 @@ def init_database(self) -> None:
234228 def init_pybind_database (self ) -> Any | None :
235229 """Initialize and return the optional pybind database backend."""
236230 self .check_for_database_close ()
237- if _lbug_pybind is None :
231+ pybind_module = get_pybind_module ()
232+ if pybind_module is None :
238233 return None
239234 if self ._pybind_database is None :
240- self ._pybind_database = _lbug_pybind .Database (
235+ self ._pybind_database = pybind_module .Database (
241236 self .database_path ,
242237 self .buffer_pool_size ,
243238 self .max_num_threads ,
0 commit comments