Skip to content

Commit 9bd9158

Browse files
committed
make qcodes version lazy
1 parent d522c68 commit 9bd9158

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/qcodes/__init__.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212

1313
from typing_extensions import deprecated
1414

15-
import qcodes._version
1615
import qcodes.configuration as qcconfig
1716
from qcodes.logger.logger import conditionally_start_all_logging
1817
from qcodes.utils import QCoDeSDeprecationWarning
1918
from qcodes.utils.spyder_utils import add_to_spyder_UMR_excludelist
2019

21-
__version__ = qcodes._version.__version__
22-
23-
2420
config: qcconfig.Config = qcconfig.Config()
2521

2622
conditionally_start_all_logging()
@@ -107,3 +103,23 @@ def test(**kwargs: Any) -> int:
107103
del deprecated
108104

109105
test.__test__ = False # type: ignore[attr-defined] # Don't try to run this method as a test
106+
107+
108+
def __getattr__(name: str) -> Any:
109+
"""
110+
Getting __version__ is slow in an editable install since we have shell out to run git describe.
111+
Here we only do it lazily if required.
112+
113+
TODO this means that the type of __version__ is Any, which is not ideal.
114+
__version__ is also not listed by dir(qcodes) that could be fixed by overwrting __dir__.
115+
see https://peps.python.org/pep-0562/
116+
"""
117+
if name == "__version__":
118+
import qcodes._version
119+
120+
__version__ = qcodes._version.__version__
121+
return __version__
122+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
123+
124+
125+
__all__ = ["__version___", "config"]

0 commit comments

Comments
 (0)