Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions docs/changes/newsfragments/8096.breaking
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Several module-level ``TypeVar`` definitions and type aliases that are no longer used
internally have been deprecated. Importing them will emit a ``QCoDeSDeprecationWarning``.
They will be removed in a future version.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ select = [
# PLxxxx are pylint lints that generate a fair amount of warnings
# it may be worth fixing some or these in the future
# PYI036 disable until https://github.com/astral-sh/ruff/issues/9794 is fixed
# UP040, UP046, UP047: PEP 695 type param conversions deferred — TypeVars use default/covariant features
ignore = ["E501", "G004", "PLR2004", "PLR0913", "PLR0911", "PLR0912", "PLR0915", "PLW0602", "PLW0603", "PLW2901", "PYI036", "UP040", "UP046", "UP047"]
ignore = ["E501", "G004", "PLR2004", "PLR0913", "PLR0911", "PLR0912", "PLR0915", "PLW0602", "PLW0603", "PLW2901", "PYI036"]

# we want to explicitly use the micro symbol
# not the greek letter
Expand Down
18 changes: 14 additions & 4 deletions src/qcodes/dataset/data_set_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
from pathlib import Path
from typing import TYPE_CHECKING, Generic, Literal, TypeVar
from typing import TYPE_CHECKING, Literal, TypeVar

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -34,12 +34,10 @@
from .data_set_in_memory import DataSetInMem
from .data_set_protocol import DataSetProtocol, ParameterData

DatasetType_co = TypeVar("DatasetType_co", bound="DataSetProtocol", covariant=True)

log = logging.getLogger(__name__)


class DataSetCache(Generic[DatasetType_co]):
class DataSetCache[DatasetType_co: "DataSetProtocol"]:
"""
The DataSetCache contains a in memory representation of the
data in this dataset as well a a method to progressively read data
Expand Down Expand Up @@ -572,3 +570,15 @@ def load_data_from_db(self) -> None:
)
if not data_not_read:
self._live = False


if not TYPE_CHECKING:
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr

_deprecated_typevars: dict[str, TypeVar] = {
"DatasetType_co": TypeVar(
"DatasetType_co", bound="DataSetProtocol", covariant=True
),
}

__getattr__ = _make_deprecated_typevars_getattr(__name__, _deprecated_typevars)
40 changes: 22 additions & 18 deletions src/qcodes/dataset/data_set_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
from .sqlite.queries import raw_time_to_str_time

if TYPE_CHECKING:
from typing import TypeAlias

import pandas as pd
import xarray as xr

Expand All @@ -51,32 +49,23 @@
# twice here convert to set to ensure no duplication.
_EXPORT_CALLBACKS = set(entry_points(group="qcodes.dataset.on_export"))

ScalarResTypes: TypeAlias = (
str | complex | np.integer | np.floating | np.complexfloating
)
ValuesType: TypeAlias = (
type ScalarResTypes = str | complex | np.integer | np.floating | np.complexfloating
type ValuesType = (
ScalarResTypes
| npt.NDArray
| Sequence[ScalarResTypes]
| Sequence[Sequence[ScalarResTypes]]
)
ResType: TypeAlias = "tuple[ParameterBase | str, ValuesType]"
SetpointsType: TypeAlias = "Sequence[str | ParameterBase]"
type ResType = "tuple[ParameterBase | str, ValuesType]"
type SetpointsType = "Sequence[str | ParameterBase]"

# deprecated alias left for backwards compatibility
array_like_types = (tuple, list, npt.NDArray)
scalar_res_types: TypeAlias = ScalarResTypes # noqa PYI042
values_type: TypeAlias = ValuesType # noqa PYI042
res_type: TypeAlias = ResType # noqa PYI042
setpoints_type: TypeAlias = SetpointsType # noqa PYI042


SPECS: TypeAlias = list[ParamSpec]
type SPECS = list[ParamSpec]
# Transition period type: SpecsOrInterDeps. We will allow both as input to
# the DataSet constructor for a while, then deprecate SPECS and finally remove
# the ParamSpec class
SpecsOrInterDeps: TypeAlias = SPECS | InterDependencies_
ParameterData: TypeAlias = dict[str, dict[str, npt.NDArray]]
type SpecsOrInterDeps = SPECS | InterDependencies_
type ParameterData = dict[str, dict[str, npt.NDArray]]

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -550,3 +539,18 @@ def dependent_parameters(self) -> tuple[ParamSpecBase, ...]:
class DataSetType(StrEnum):
DataSet = "DataSet"
DataSetInMem = "DataSetInMem"


if not TYPE_CHECKING:
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr

__getattr__ = _make_deprecated_typevars_getattr(
__name__,
{
"array_like_types": (tuple, list, npt.NDArray),
"scalar_res_types": ScalarResTypes,
"values_type": ValuesType,
"res_type": ResType,
"setpoints_type": SetpointsType,
},
)
18 changes: 13 additions & 5 deletions src/qcodes/dataset/dond/sweeps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Generic, TypeVar
from typing import TYPE_CHECKING, Any, TypeVar

import numpy as np
import numpy.typing as npt
Expand All @@ -12,10 +12,8 @@
from qcodes.dataset.dond.do_nd_utils import ActionsT
from qcodes.parameters import ParameterBase

T = TypeVar("T", bound=np.generic)


class AbstractSweep(ABC, Generic[T]):
class AbstractSweep[T: np.generic](ABC):
"""
Abstract sweep class that defines an interface for concrete sweep classes.
"""
Expand Down Expand Up @@ -195,7 +193,7 @@ def get_after_set(self) -> bool:
return self._get_after_set


class ArraySweep(AbstractSweep, Generic[T]):
class ArraySweep[T: np.generic](AbstractSweep):
"""
Sweep the values of a given array.

Expand Down Expand Up @@ -281,3 +279,13 @@ def get_setpoints(self) -> Iterable:
@property
def num_points(self) -> int:
return self.sweeps[0].num_points


if not TYPE_CHECKING:
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr

_deprecated_typevars: dict[str, TypeVar] = {
"T": TypeVar("T", bound=np.generic),
}

__getattr__ = _make_deprecated_typevars_getattr(__name__, _deprecated_typevars)
22 changes: 16 additions & 6 deletions src/qcodes/dataset/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from itertools import chain
from numbers import Number
from time import perf_counter, perf_counter_ns
from typing import TYPE_CHECKING, Any, TypeAlias, TypeVar, cast
from typing import TYPE_CHECKING, Any, cast

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -74,8 +74,8 @@
Callable[..., Any], MutableSequence[Any] | MutableMapping[Any, Any]
]

ParameterResultType: TypeAlias = tuple[ParameterBase, ValuesType]
DatasetResultDict: TypeAlias = dict[ParamSpecBase, npt.NDArray]
type ParameterResultType = tuple[ParameterBase, ValuesType]
type DatasetResultDict = dict[ParamSpecBase, npt.NDArray]


class ParameterTypeError(Exception):
Expand Down Expand Up @@ -804,9 +804,6 @@ def __exit__(
self._exit_stack.close()


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


class Measurement:
"""
Measurement procedure container. Note that multiple measurement
Expand Down Expand Up @@ -1616,3 +1613,16 @@ def _numeric_values_are_equal(
):
return False
return True


if not TYPE_CHECKING:
from typing import TypeVar

from qcodes.utils.deprecate import _make_deprecated_typevars_getattr

__getattr__ = _make_deprecated_typevars_getattr(
__name__,
{
"T": TypeVar("T", bound="Measurement"),
},
)
18 changes: 13 additions & 5 deletions src/qcodes/dataset/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import logging
from collections import defaultdict
from functools import partial
from typing import TYPE_CHECKING, Protocol, TypeAlias, TypeVar
from typing import TYPE_CHECKING, Protocol, TypeVar

from qcodes.utils import RespondingThread

Expand All @@ -21,10 +21,8 @@
from qcodes.dataset.data_set_protocol import ValuesType
from qcodes.parameters import ParamDataType, ParameterBase

ParamMeasT: TypeAlias = "ParameterBase | Callable[[], None]"
OutType: TypeAlias = "list[tuple[ParameterBase, ValuesType]]"

T = TypeVar("T")
type ParamMeasT = "ParameterBase | Callable[[], None]"
type OutType = "list[tuple[ParameterBase, ValuesType]]"

_LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -221,3 +219,13 @@ def __exit__(
exc_tb: TracebackType | None,
) -> None:
self._thread_pool.__exit__(exc_type, exc_val, exc_tb)


if not TYPE_CHECKING:
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr

_deprecated_typevars: dict[str, TypeVar] = {
"T": TypeVar("T"),
}

__getattr__ = _make_deprecated_typevars_getattr(__name__, _deprecated_typevars)
7 changes: 2 additions & 5 deletions src/qcodes/extensions/_driver_test_case.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import unittest
from typing import TYPE_CHECKING, Generic, TypeVar
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from qcodes.instrument import Instrument
Expand All @@ -25,10 +25,7 @@
"""


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


class DriverTestCase(unittest.TestCase, Generic[T]):
class DriverTestCase[T: "Instrument"](unittest.TestCase):
# override this in a subclass
driver: type[T] | None = None
instrument: T
Expand Down
22 changes: 15 additions & 7 deletions src/qcodes/extensions/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

DOES_NOT_EXIST = "Does not exist"

C = TypeVar("C", bound=ParameterBase)
TInstrument = TypeVar("TInstrument", bound=InstrumentBase)


class InferError(AttributeError): ...

Expand Down Expand Up @@ -225,7 +222,7 @@ def _merge_user_and_class_attrs(
return set.union(set(alt_source_attrs), set(InferAttrs.known_attrs()))


def get_chain_links_of_type(
def get_chain_links_of_type[C: ParameterBase](
link_param_type: type[C] | tuple[type[C], ...], parameter: Parameter
) -> tuple[C, ...]:
"""Gets all parameters in a chain of linked parameters that match a given type"""
Expand All @@ -237,7 +234,7 @@ def get_chain_links_of_type(
return tuple(chain_links)


def get_sole_chain_link_of_type(
def get_sole_chain_link_of_type[C: ParameterBase](
link_param_type: type[C] | tuple[type[C], ...], parameter: Parameter
) -> C:
"""Gets the one parameter in a chain of linked parameters that matches a given type"""
Expand All @@ -256,7 +253,7 @@ def get_sole_chain_link_of_type(
return chain_links[0]


def get_parent_instruments_from_chain_of_type(
def get_parent_instruments_from_chain_of_type[TInstrument: InstrumentBase](
instrument_type: type[TInstrument] | tuple[type[TInstrument], ...],
parameter: Parameter,
) -> tuple[TInstrument, ...]:
Expand All @@ -272,7 +269,7 @@ def get_parent_instruments_from_chain_of_type(
)


def get_sole_parent_instrument_from_chain_of_type(
def get_sole_parent_instrument_from_chain_of_type[TInstrument: InstrumentBase](
instrument_type: type[TInstrument] | tuple[type[TInstrument], ...],
parameter: Parameter,
) -> TInstrument:
Expand All @@ -289,3 +286,14 @@ def get_sole_parent_instrument_from_chain_of_type(

raise ValueError(f"{error_msg_1} {[instr.name for instr in instruments]}")
return instruments[0]


if not TYPE_CHECKING:
from qcodes.utils.deprecate import _make_deprecated_typevars_getattr

_deprecated_typevars: dict[str, TypeVar] = {
"C": TypeVar("C", bound=ParameterBase),
"TInstrument": TypeVar("TInstrument", bound=InstrumentBase),
}

__getattr__ = _make_deprecated_typevars_getattr(__name__, _deprecated_typevars)
21 changes: 16 additions & 5 deletions src/qcodes/extensions/parameters/parameter_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@

import logging
import warnings
from typing import Any, ClassVar, TypeVar
from typing import TYPE_CHECKING, Any, ClassVar

from qcodes.parameters import ParameterBase

T = TypeVar("T")

log = logging.getLogger(__name__)


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

@classmethod
def _get_leaf_classes(
def _get_leaf_classes[T](
cls, base_type: type[T], exclude_base_type: type | None = None
) -> list[type[T]]:
"""
Expand Down Expand Up @@ -289,7 +287,7 @@ def _get_leaf_classes(
return leaf_classes

@classmethod
def _get_mixin_classes(
def _get_mixin_classes[T](
cls, base_type: type[T], exclude_base_type: type | None = None
) -> list[type[T]]:
"""
Expand Down Expand Up @@ -321,3 +319,16 @@ def _get_mixin_classes(
]

return mixin_classes


if not TYPE_CHECKING:
from typing import TypeVar

from qcodes.utils.deprecate import _make_deprecated_typevars_getattr

__getattr__ = _make_deprecated_typevars_getattr(
__name__,
{
"T": TypeVar("T"),
},
)
Loading
Loading