Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Reuse existing instrument when recreating a new instrument with same name and type #4038

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions qcodes/instrument/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down