From 35f08641a8b9005c7125aa06cb45d0d44e9947a1 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Tue, 22 Mar 2022 13:53:34 +0100 Subject: [PATCH] Wip reuse existing instrument rather than error --- qcodes/instrument/base.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/qcodes/instrument/base.py b/qcodes/instrument/base.py index 1549f5ba079..7a60035dc1c 100644 --- a/qcodes/instrument/base.py +++ b/qcodes/instrument/base.py @@ -514,6 +514,25 @@ def __call__(cls, *args: Any, **kwargs: Any) -> Any: Overloads `type.__call__` to add code that runs only if __init__ completes successfully. """ + if len(args) >= 1: + name = args[0] + else: + name = kwargs.get("name", None) + existing_instr = None + if name is not None: + + try: + existing_instr = cls.find_instrument(name, cls) # type: ignore[attr-defined] + except (KeyError, TypeError): + pass + + if existing_instr is not None: + log.info(f"Reusing existing instrument {name}") + # todo this is only really safe if this is the same instrument + # e.g. address should be the same but that is a trait only implemented + # in subclasses + return existing_instr + new_inst = super().__call__(*args, **kwargs) is_abstract = new_inst._is_abstract() if is_abstract: