Skip to content

Commit

Permalink
Merge pull request #6055 from jenshnielsen/instrument_meta_typed
Browse files Browse the repository at this point in the history
Fix typing of Instrument
  • Loading branch information
jenshnielsen committed May 13, 2024
2 parents 7bdb583 + 17ae1b9 commit 79dda76
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/qcodes/instrument/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ def write(self, cmd: str) -> None:

T = TypeVar("T", bound="Instrument")

# a metaclass that overrides __call__ means that we lose
# both the args and return type hints.
# Since our metaclass does not modify the signature
# is is safe simply not to use that metaclass in typechecking context.
# See https://github.com/microsoft/pyright/discussions/5561 and
# https://github.com/microsoft/pyright/issues/5488
if TYPE_CHECKING:
instrument_meta_class = type
else:
instrument_meta_class = InstrumentMeta

class Instrument(InstrumentBase, metaclass=InstrumentMeta):

class Instrument(InstrumentBase, metaclass=instrument_meta_class):
"""
Base class for all QCodes instruments.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import pytest
from pytest import FixtureRequest
from typing_extensions import assert_type

from qcodes.instrument import (
Instrument,
Expand Down Expand Up @@ -80,6 +81,15 @@ def _close_before_and_after():
Instrument.close_all()


def test_instrument_type(request: pytest.FixtureRequest) -> None:
# make sure that the type of the instrument is correct.
# Due to our use of a metaclass for instrument this could be
# incorrect. See comment in instrument.py
testldummy = DummyInstrument("dummy")
request.addfinalizer(testldummy.close)
assert_type(testldummy, DummyInstrument)


def test_validate_function(testdummy: DummyInstrument) -> None:
testdummy.validate_status() # test the instrument has valid values

Expand Down

0 comments on commit 79dda76

Please sign in to comment.