From 47add5c3dbb5b69d33b3e8642de26c5fdf706554 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Fri, 5 Apr 2024 11:29:26 +0200 Subject: [PATCH 1/5] make qcodes version lazy --- src/qcodes/__init__.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/qcodes/__init__.py b/src/qcodes/__init__.py index e6f0b8d4674d..9737948ebe45 100644 --- a/src/qcodes/__init__.py +++ b/src/qcodes/__init__.py @@ -8,16 +8,15 @@ # config import warnings -from typing import Any +from typing import TYPE_CHECKING, Any + + +from typing_extensions import deprecated -import qcodes._version import qcodes.configuration as qcconfig from qcodes.logger.logger import conditionally_start_all_logging from qcodes.utils import QCoDeSDeprecationWarning -__version__ = qcodes._version.__version__ - - config: qcconfig.Config = qcconfig.Config() conditionally_start_all_logging() @@ -81,3 +80,23 @@ "Please avoid setting this in your `qcodesrc.json` config file.", QCoDeSDeprecationWarning, ) + +__version__: str + +if not TYPE_CHECKING: + + def __getattr__(name: str) -> Any: + """ + Getting __version__ is slow in an editable install since we have shell out to run git describe. + Here we only do it lazily if required. + + TODO this means that the type of __version__ is Any, which is not ideal. + __version__ is also not listed by dir(qcodes) that could be fixed by overwrting __dir__. + see https://peps.python.org/pep-0562/ + """ + if name == "__version__": + import qcodes._version # noqa: PLC0415 # lazy import since getting version is slow in editable install. + + __version__ = qcodes._version.__version__ + return __version__ + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") From f24d2204ba534ba5ed865d00d3a2bebd8359375a Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sun, 9 Jun 2024 19:46:51 +0200 Subject: [PATCH 2/5] Add type for __version__ --- src/qcodes/__init__.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/qcodes/__init__.py b/src/qcodes/__init__.py index 9737948ebe45..3f12e79b4895 100644 --- a/src/qcodes/__init__.py +++ b/src/qcodes/__init__.py @@ -10,9 +10,6 @@ import warnings from typing import TYPE_CHECKING, Any - -from typing_extensions import deprecated - import qcodes.configuration as qcconfig from qcodes.logger.logger import conditionally_start_all_logging from qcodes.utils import QCoDeSDeprecationWarning @@ -99,4 +96,3 @@ def __getattr__(name: str) -> Any: __version__ = qcodes._version.__version__ return __version__ - raise AttributeError(f"module {__name__!r} has no attribute {name!r}") From fc25543ecc53ee66c600bc57f40a7289a3fe8932 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Wed, 13 May 2026 16:52:00 +0200 Subject: [PATCH 3/5] Update docstring --- src/qcodes/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/qcodes/__init__.py b/src/qcodes/__init__.py index 3f12e79b4895..4fe7adafa164 100644 --- a/src/qcodes/__init__.py +++ b/src/qcodes/__init__.py @@ -87,9 +87,6 @@ def __getattr__(name: str) -> Any: Getting __version__ is slow in an editable install since we have shell out to run git describe. Here we only do it lazily if required. - TODO this means that the type of __version__ is Any, which is not ideal. - __version__ is also not listed by dir(qcodes) that could be fixed by overwrting __dir__. - see https://peps.python.org/pep-0562/ """ if name == "__version__": import qcodes._version # noqa: PLC0415 # lazy import since getting version is slow in editable install. From 871bde6ec957afec18b9526e17af563d0994a36a Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Wed, 13 May 2026 16:53:40 +0200 Subject: [PATCH 4/5] Add __version__ to all --- src/qcodes/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/qcodes/__init__.py b/src/qcodes/__init__.py index 4fe7adafa164..c55cd2212e0c 100644 --- a/src/qcodes/__init__.py +++ b/src/qcodes/__init__.py @@ -78,8 +78,6 @@ QCoDeSDeprecationWarning, ) -__version__: str - if not TYPE_CHECKING: def __getattr__(name: str) -> Any: @@ -93,3 +91,10 @@ def __getattr__(name: str) -> Any: __version__ = qcodes._version.__version__ return __version__ +else: + __version__ = "not_set" + +__all__ = [ + "__version__", + "config", +] From 952ca6ad5ab120927ec0a7ae9483b1e497e0e805 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Wed, 13 May 2026 17:13:33 +0200 Subject: [PATCH 5/5] Add missing attribute error --- src/qcodes/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/qcodes/__init__.py b/src/qcodes/__init__.py index c55cd2212e0c..5e3d0c15b939 100644 --- a/src/qcodes/__init__.py +++ b/src/qcodes/__init__.py @@ -91,6 +91,7 @@ def __getattr__(name: str) -> Any: __version__ = qcodes._version.__version__ return __version__ + raise AttributeError(f"module {__name__} has no attribute {name}") else: __version__ = "not_set"