Skip to content

Commit c33349a

Browse files
authored
Merge pull request #7486 from jenshnielsen/channel_type_safe
More type safe ways of gettting channel attributes
2 parents def8a41 + 357fbde commit c33349a

4 files changed

Lines changed: 312 additions & 141 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``ChannelTuple/ChannelList.get_channel_by_name`` support for getting more than one channel has been deprecated. To get more than one channel use ``get_channels_by_name``.
2+
``get_channels_by_name`` is guaranteed to always return an instance of the class it was called on independently of the number of channels supplied. In the future
3+
``get_channel_by_name`` will be updated to ensure that it always returns a single channel. The Exception raised when
4+
no argument is given to ``get_channel_by_name`` has changed from ``Exception`` to ``TypeError`` in line with how functions behave when an argument is missing.

src/qcodes/instrument/channel.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import sys
6+
import warnings
67
from collections.abc import Callable, Iterable, Iterator, MutableSequence, Sequence
78
from typing import TYPE_CHECKING, Any, TypeVar, cast, overload
89

@@ -14,7 +15,7 @@
1415
Parameter,
1516
)
1617
from qcodes.parameters.multi_channel_instrument_parameter import InstrumentModuleType
17-
from qcodes.utils import full_class
18+
from qcodes.utils import QCoDeSDeprecationWarning, full_class
1819
from qcodes.validators import Validator
1920

2021
from .instrument_base import InstrumentBase
@@ -338,6 +339,26 @@ def count( # pyright: ignore[reportIncompatibleMethodOverride]
338339
"""
339340
return self._channels.count(obj)
340341

342+
def get_channels_by_name(self: Self, *names: str) -> Self:
343+
"""
344+
Get a a ChannelTuple that only contains the selected names.
345+
346+
Args:
347+
*names: channel names
348+
349+
"""
350+
if len(names) == 0:
351+
raise TypeError("one or more names must be given")
352+
selected_channels = tuple(self._channel_mapping[name] for name in names)
353+
return type(self)(
354+
self._parent,
355+
self._name,
356+
self._chan_type,
357+
selected_channels,
358+
self._snapshotable,
359+
self._paramclass,
360+
)
361+
341362
def get_channel_by_name(self: Self, *names: str) -> InstrumentModuleType | Self:
342363
"""
343364
Get a channel by name, or a ChannelTuple if multiple names are given.
@@ -347,9 +368,15 @@ def get_channel_by_name(self: Self, *names: str) -> InstrumentModuleType | Self:
347368
348369
"""
349370
if len(names) == 0:
350-
raise Exception("one or more names must be given")
371+
raise TypeError("one or more names must be given")
351372
if len(names) == 1:
352373
return self._channel_mapping[names[0]]
374+
375+
warnings.warn(
376+
"Supplying more than one name to get_channel_by_name is deprecated, use get_channels_by_name instead",
377+
category=QCoDeSDeprecationWarning,
378+
)
379+
353380
selected_channels = tuple(self._channel_mapping[name] for name in names)
354381
return type(self)(
355382
self._parent,

0 commit comments

Comments
 (0)