From 5a45f03b80d2b9324b4b7fa604dfaf388b7d062a Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Fri, 5 Apr 2024 11:29:26 +0200 Subject: [PATCH 1/2] make qcodes version lazy --- src/qcodes/__init__.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/qcodes/__init__.py b/src/qcodes/__init__.py index 0b94f308719..9979fde3bc6 100644 --- a/src/qcodes/__init__.py +++ b/src/qcodes/__init__.py @@ -12,15 +12,11 @@ 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 from qcodes.utils.spyder_utils import add_to_spyder_UMR_excludelist -__version__ = qcodes._version.__version__ - - config: qcconfig.Config = qcconfig.Config() conditionally_start_all_logging() @@ -107,3 +103,23 @@ def test(**kwargs: Any) -> int: del deprecated test.__test__ = False # type: ignore[attr-defined] # Don't try to run this method as a test + + +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 + + __version__ = qcodes._version.__version__ + return __version__ + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") + + +__all__ = ["__version___", "config"] From 27fce61198c1fa825fbb057bfc1ebf4affade988 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Sun, 9 Jun 2024 19:46:51 +0200 Subject: [PATCH 2/2] Add type for __version__ --- src/qcodes/__init__.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/qcodes/__init__.py b/src/qcodes/__init__.py index 9979fde3bc6..d0c180aff67 100644 --- a/src/qcodes/__init__.py +++ b/src/qcodes/__init__.py @@ -105,14 +105,15 @@ def test(**kwargs: Any) -> int: test.__test__ = False # type: ignore[attr-defined] # Don't try to run this method as a test +__version__: str + 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/ + TODO this means that unknown attributes are typed as Any rather than an error + Using Literal["__version__"] as the input type does not seem to result in other attributes being rejected """ if name == "__version__": import qcodes._version @@ -120,6 +121,3 @@ def __getattr__(name: str) -> Any: __version__ = qcodes._version.__version__ return __version__ raise AttributeError(f"module {__name__!r} has no attribute {name!r}") - - -__all__ = ["__version___", "config"]