Coverage for cosmolayer/cosmosac/__init__.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-26 00:09 +0000

1import importlib 

2from typing import TYPE_CHECKING 

3 

4if TYPE_CHECKING: 

5 from .component import Component 

6 from .datapoint import CosmoSacMixtureDatapoint 

7 from .mixture import Mixture 

8 from .model import CosmoSac2002Model, CosmoSac2010Model, Model 

9 

10__all__ = [ 

11 "Component", 

12 "CosmoSac2002Model", 

13 "CosmoSac2010Model", 

14 "CosmoSacMixtureDatapoint", 

15 "Mixture", 

16 "Model", 

17] 

18 

19# CosmoSacMixtureDatapoint pulls in cosmolayer.cosmodata, which imports torch; 

20# import it (and every other attribute here) lazily so that `import 

21# cosmolayer.cosmosac` alone -- or accessing only e.g. Component or Mixture -- 

22# stays cheap. 

23_LAZY_ATTRS = { 

24 "Component": ".component", 

25 "CosmoSacMixtureDatapoint": ".datapoint", 

26 "Mixture": ".mixture", 

27 "CosmoSac2002Model": ".model", 

28 "CosmoSac2010Model": ".model", 

29 "Model": ".model", 

30} 

31 

32 

33def __getattr__(name: str) -> object: 

34 if name in _LAZY_ATTRS: 

35 module = importlib.import_module(_LAZY_ATTRS[name], __name__) 

36 value = getattr(module, name) 

37 globals()[name] = value 

38 return value 

39 raise AttributeError(f"module {__name__!r} has no attribute {name!r}") 

40 

41 

42def __dir__() -> list[str]: 

43 return sorted(set(globals()) | set(_LAZY_ATTRS))