From 71ad4bd08597a316f7b6264d7ce6d7e3583a7582 Mon Sep 17 00:00:00 2001 From: "Jens H. Nielsen" Date: Fri, 7 Feb 2025 13:31:13 +0100 Subject: [PATCH] Fix type checking with mypy --- src/qcodes/instrument/channel.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/qcodes/instrument/channel.py b/src/qcodes/instrument/channel.py index 0b2e21503c6..9e47adf7b90 100644 --- a/src/qcodes/instrument/channel.py +++ b/src/qcodes/instrument/channel.py @@ -153,10 +153,10 @@ def __init__( self._parent = parent self._name = name if not isinstance(chan_type, type) or not issubclass( - chan_type, InstrumentChannel + chan_type, InstrumentModule ): raise ValueError( - "ChannelTuple can only hold instances of type InstrumentChannel" + "ChannelTuple can only hold instances of type InstrumentModule" ) if not isinstance(multichan_paramclass, type) or not issubclass( multichan_paramclass, MultiChannelInstrumentParameter @@ -166,7 +166,7 @@ def __init__( "MultiChannelInstrumentParameter" ) - self._chan_type = chan_type + self._chan_type: type[InstrumentModuleType] = chan_type self._snapshotable = snapshotable self._paramclass = multichan_paramclass @@ -270,7 +270,9 @@ def __add__(self: Self, other: ChannelTuple) -> Self: if self._parent != other._parent: raise ValueError("Can only add channels from the same parent together.") - return type(self)( + # mypy does not understand that type[self] is type[self] + # it infers that type(self) == ChannelTuple[Any] e.g. not generic + return type(self)( # type: ignore[return-value] self._parent, self._name, self._chan_type, @@ -1066,7 +1068,7 @@ def exists_on_instrument(self) -> bool: return self._exists_on_instrument -class AutoLoadableChannelList(ChannelList): +class AutoLoadableChannelList(ChannelList[AutoLoadableInstrumentChannel]): """ Extends the QCoDeS :class:`ChannelList` class to add the following features: - Automatically create channel objects on initialization @@ -1112,7 +1114,7 @@ def __init__( self, parent: Instrument, name: str, - chan_type: type, + chan_type: type[AutoLoadableInstrumentChannel], chan_list: Sequence[AutoLoadableInstrumentChannel] | None = None, snapshotable: bool = True, multichan_paramclass: type = MultiChannelInstrumentParameter, @@ -1121,7 +1123,7 @@ def __init__( super().__init__( parent, name, chan_type, chan_list, snapshotable, multichan_paramclass ) - new_channels = self._chan_type.load_from_instrument( # type: ignore[attr-defined] + new_channels = self._chan_type.load_from_instrument( self._parent, channel_list=self, **kwargs ) @@ -1139,7 +1141,7 @@ def add(self, **kwargs: Any) -> AutoLoadableInstrumentChannel: Newly created instance of the channel class """ - new_channel = self._chan_type.new_instance( # type: ignore[attr-defined] + new_channel = self._chan_type.new_instance( self._parent, create_on_instrument=True, channel_list=self, **kwargs )