Skip to content

Commit 453c81c

Browse files
jenshnielsenCopilot
andcommitted
Add newsfragment for PR #8096
Also deprecate 3 additional pre-existing unused TypeVars in field_vector.py, threading.py, and channel.py. Fix pyright errors in test_deprecate.py. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9eab1c6 commit 453c81c

25 files changed

Lines changed: 279 additions & 96 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Several module-level ``TypeVar`` definitions that are no longer used internally have been
2+
deprecated. Importing them will emit a ``QCoDeSDeprecationWarning``. They will be removed
3+
in a future version.

src/qcodes/dataset/measurements.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from itertools import chain
1919
from numbers import Number
2020
from time import perf_counter, perf_counter_ns
21-
from typing import TYPE_CHECKING, Any, TypeAlias, TypeVar, cast
21+
from typing import TYPE_CHECKING, Any, TypeAlias, cast
2222

2323
import numpy as np
2424
import numpy.typing as npt
@@ -804,9 +804,6 @@ def __exit__(
804804
self._exit_stack.close()
805805

806806

807-
T = TypeVar("T", bound="Measurement")
808-
809-
810807
class Measurement:
811808
"""
812809
Measurement procedure container. Note that multiple measurement
@@ -1616,3 +1613,16 @@ def _numeric_values_are_equal(
16161613
):
16171614
return False
16181615
return True
1616+
1617+
1618+
if not TYPE_CHECKING:
1619+
from typing import TypeVar
1620+
1621+
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr
1622+
1623+
__getattr__ = _make_deprecated_typevars_getattr(
1624+
__name__,
1625+
{
1626+
"T": TypeVar("T", bound="Measurement"),
1627+
},
1628+
)

src/qcodes/dataset/threading.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
ParamMeasT: TypeAlias = "ParameterBase | Callable[[], None]"
2525
OutType: TypeAlias = "list[tuple[ParameterBase, ValuesType]]"
2626

27-
T = TypeVar("T")
28-
2927
_LOG = logging.getLogger(__name__)
3028

3129

@@ -221,3 +219,13 @@ def __exit__(
221219
exc_tb: TracebackType | None,
222220
) -> None:
223221
self._thread_pool.__exit__(exc_type, exc_val, exc_tb)
222+
223+
224+
if not TYPE_CHECKING:
225+
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr
226+
227+
_deprecated_typevars: dict[str, TypeVar] = {
228+
"T": TypeVar("T"),
229+
}
230+
231+
__getattr__ = _make_deprecated_typevars_getattr(__name__, _deprecated_typevars)

src/qcodes/extensions/parameters/parameter_mixin.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424

2525
import logging
2626
import warnings
27-
from typing import Any, ClassVar, TypeVar
27+
from typing import TYPE_CHECKING, Any, ClassVar
2828

2929
from qcodes.parameters import ParameterBase
3030

31-
T = TypeVar("T")
32-
3331
log = logging.getLogger(__name__)
3432

3533

@@ -245,7 +243,7 @@ def _update_docstring(
245243
cls.__doc__ = (original_doc.strip() + "\n\n" + additional_doc).strip()
246244

247245
@classmethod
248-
def _get_leaf_classes(
246+
def _get_leaf_classes[T](
249247
cls, base_type: type[T], exclude_base_type: type | None = None
250248
) -> list[type[T]]:
251249
"""
@@ -289,7 +287,7 @@ def _get_leaf_classes(
289287
return leaf_classes
290288

291289
@classmethod
292-
def _get_mixin_classes(
290+
def _get_mixin_classes[T](
293291
cls, base_type: type[T], exclude_base_type: type | None = None
294292
) -> list[type[T]]:
295293
"""
@@ -321,3 +319,16 @@ def _get_mixin_classes(
321319
]
322320

323321
return mixin_classes
322+
323+
324+
if not TYPE_CHECKING:
325+
from typing import TypeVar
326+
327+
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr
328+
329+
__getattr__ = _make_deprecated_typevars_getattr(
330+
__name__,
331+
{
332+
"T": TypeVar("T"),
333+
},
334+
)

src/qcodes/instrument/channel.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
MultiParameter,
1616
Parameter,
1717
)
18-
from qcodes.parameters.multi_channel_instrument_parameter import InstrumentModuleType
1918
from qcodes.utils import full_class
2019
from qcodes.validators import Validator
2120

@@ -27,6 +26,7 @@
2726
from .instrument_base import InstrumentBaseKWArgs
2827

2928

29+
# Cannot convert to PEP 695: uses default= and covariant= which require PEP 696 (Python 3.13+).
3030
_TIB_co = TypeVar(
3131
"_TIB_co", bound="InstrumentBase", default=InstrumentBase, covariant=True
3232
)
@@ -98,10 +98,7 @@ class InstrumentChannel(InstrumentModule[_TIB_co], Generic[_TIB_co]):
9898
pass
9999

100100

101-
T = TypeVar("T", bound="ChannelTuple")
102-
103-
104-
class ChannelTuple(MetadatableWithName, Sequence[InstrumentModuleType]):
101+
class ChannelTuple[InstrumentModuleType: "InstrumentModule"](MetadatableWithName, Sequence[InstrumentModuleType]):
105102
"""
106103
Container for channelized parameters that allows for sweeps over
107104
all channels, as well as addressing of individual channels.
@@ -628,7 +625,7 @@ def invalidate_cache(self) -> None:
628625

629626
# in index method the parameter obj should be called value but that would
630627
# be an incompatible change
631-
class ChannelList( # pyright: ignore[reportIncompatibleMethodOverride]
628+
class ChannelList[InstrumentModuleType: "InstrumentModule"]( # pyright: ignore[reportIncompatibleMethodOverride]
632629
ChannelTuple[InstrumentModuleType], MutableSequence[InstrumentModuleType]
633630
):
634631
"""
@@ -1161,10 +1158,8 @@ def exists_on_instrument(self) -> bool:
11611158
return self._exists_on_instrument
11621159

11631160

1164-
TAUTORELOADCHANNEL = TypeVar("TAUTORELOADCHANNEL", bound=AutoLoadableInstrumentChannel)
11651161

1166-
1167-
class AutoLoadableChannelList(ChannelList[TAUTORELOADCHANNEL]):
1162+
class AutoLoadableChannelList[TAUTORELOADCHANNEL: AutoLoadableInstrumentChannel](ChannelList[TAUTORELOADCHANNEL]):
11681163
"""
11691164
Extends the QCoDeS :class:`ChannelList` class to add the following features:
11701165
- Automatically create channel objects on initialization
@@ -1243,3 +1238,16 @@ def add(self, **kwargs: Any) -> TAUTORELOADCHANNEL:
12431238

12441239
self.append(new_channel)
12451240
return new_channel
1241+
1242+
1243+
if not TYPE_CHECKING:
1244+
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr
1245+
1246+
_deprecated_typevars: dict[str, TypeVar] = {
1247+
"T": TypeVar("T", bound="ChannelTuple"),
1248+
"TAUTORELOADCHANNEL": TypeVar(
1249+
"TAUTORELOADCHANNEL", bound=AutoLoadableInstrumentChannel
1250+
),
1251+
}
1252+
1253+
__getattr__ = _make_deprecated_typevars_getattr(__name__, _deprecated_typevars)

src/qcodes/instrument/instrument.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import time
77
import weakref
8-
from typing import TYPE_CHECKING, Any, Protocol, TypeVar, overload
8+
from typing import TYPE_CHECKING, Any, Protocol, overload
99

1010
from qcodes.utils import strip_attrs
1111
from qcodes.validators import Anything
@@ -31,7 +31,6 @@ def ask(self, cmd: str) -> str: ...
3131
def write(self, cmd: str) -> None: ...
3232

3333

34-
T = TypeVar("T", bound="Instrument")
3534

3635
# a metaclass that overrides __call__ means that we lose
3736
# both the args and return type hints.
@@ -280,10 +279,10 @@ def find_instrument(
280279

281280
@overload
282281
@classmethod
283-
def find_instrument(cls, name: str, instrument_class: type[T]) -> T: ...
282+
def find_instrument[T: "Instrument"](cls, name: str, instrument_class: type[T]) -> T: ...
284283

285284
@classmethod
286-
def find_instrument(
285+
def find_instrument[T: "Instrument"](
287286
cls, name: str, instrument_class: type[T] | None = None
288287
) -> T | Instrument:
289288
"""
@@ -504,3 +503,16 @@ def find_or_create_instrument[T: "Instrument"](
504503
instrument.connect_message() # prints the message
505504

506505
return instrument
506+
507+
508+
if not TYPE_CHECKING:
509+
from typing import TypeVar
510+
511+
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr
512+
513+
__getattr__ = _make_deprecated_typevars_getattr(
514+
__name__,
515+
{
516+
"T": TypeVar("T", bound="Instrument"),
517+
},
518+
)

src/qcodes/instrument/instrument_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
log = logging.getLogger(__name__)
2929

30+
# Cannot convert to PEP 695: uses default= which requires PEP 696 (Python 3.13+).
3031
TParameter = TypeVar("TParameter", bound="ParameterBase", default="Parameter")
3132
TSubmodule = TypeVar(
3233
"TSubmodule", bound="InstrumentModule | ChannelTuple", default="InstrumentModule"

src/qcodes/instrument_drivers/AlazarTech/ATS.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time
77
import warnings
88
from contextlib import contextmanager
9-
from typing import TYPE_CHECKING, Any, TypeVar, cast
9+
from typing import TYPE_CHECKING, Any, cast
1010

1111
import numpy as np
1212
import numpy.typing as npt
@@ -24,8 +24,6 @@
2424

2525
logger = logging.getLogger(__name__)
2626

27-
OutputType = TypeVar("OutputType")
28-
2927
CtypesTypes = (
3028
type[ctypes.c_uint8]
3129
| type[ctypes.c_uint16]
@@ -308,7 +306,7 @@ def allocate_and_post_buffer(
308306
)
309307
return buffer
310308

311-
def acquire( # noqa: D417 (missing args documentation)
309+
def acquire[OutputType]( # noqa: D417 (missing args documentation)
312310
self,
313311
mode: str | None = None,
314312
samples_per_record: int | None = None,
@@ -932,3 +930,16 @@ def _get_alazar(self) -> AlazarTechATS:
932930
:return: reference to the Alazar instrument
933931
"""
934932
return self._alazar
933+
934+
935+
if not TYPE_CHECKING:
936+
from typing import TypeVar
937+
938+
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr
939+
940+
__getattr__ = _make_deprecated_typevars_getattr(
941+
__name__,
942+
{
943+
"OutputType": TypeVar("OutputType"),
944+
},
945+
)

src/qcodes/instrument_drivers/Keithley/Keithley_6500.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from functools import partial
2-
from typing import TYPE_CHECKING, TypeVar
2+
from typing import TYPE_CHECKING
33

44
from qcodes.instrument import VisaInstrument, VisaInstrumentKWArgs
55
from qcodes.validators import Bool, Enum, Ints, MultiType, Numbers
@@ -10,8 +10,6 @@
1010

1111
from qcodes.parameters import Parameter
1212

13-
T = TypeVar("T")
14-
1513

1614
def _parse_output_string(string_value: str) -> str:
1715
"""Parses and cleans string output of the multimeter. Removes the surrounding
@@ -258,7 +256,7 @@ def reset(self) -> None:
258256
def _read_next_value(self) -> float:
259257
return float(self.ask("READ?"))
260258

261-
def _get_mode_param(self, parameter: str, parser: "Callable[[str], T]") -> T:
259+
def _get_mode_param[T](self, parameter: str, parser: "Callable[[str], T]") -> T:
262260
"""Reads the current mode of the multimeter and ask for the given parameter.
263261
264262
Args:
@@ -287,3 +285,16 @@ def _set_mode_param(self, parameter: str, value: str | float | bool) -> None:
287285
mode = _parse_output_string(self._mode_map[self.mode()])
288286
cmd = f"{mode}:{parameter} {value}"
289287
self.write(cmd)
288+
289+
290+
if not TYPE_CHECKING:
291+
from typing import TypeVar
292+
293+
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr
294+
295+
__getattr__ = _make_deprecated_typevars_getattr(
296+
__name__,
297+
{
298+
"T": TypeVar("T"),
299+
},
300+
)

src/qcodes/instrument_drivers/Keysight/Keysight_N9030B.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from collections.abc import Callable
2626
from typing import Unpack
2727

28+
# Cannot convert to PEP 695: uses default= which requires PEP 696 (Python 3.13+).
2829
_T = TypeVar(
2930
"_T",
3031
bound="KeysightN9030BSpectrumAnalyzerMode | KeysightN9030BPhaseNoiseMode",

0 commit comments

Comments
 (0)