Skip to content

Commit

Permalink
Merge pull request #6867 from jenshnielsen/ruff_9_5
Browse files Browse the repository at this point in the history
Upgrade precommit hooks
  • Loading branch information
jenshnielsen authored Feb 11, 2025
2 parents 7cf19f8 + 94404dd commit 58bb4fa
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: 'v0.9.3'
rev: 'v0.9.5'
hooks:
- id: ruff
types_or: [python, pyi, jupyter, toml]
Expand All @@ -22,7 +22,7 @@ repos:
- id: mixed-line-ending
args: ['--fix=no']
- repo: https://github.com/gitleaks/gitleaks
rev: v8.23.2
rev: v8.23.3
hooks:
- id: gitleaks
- repo: https://github.com/jumanjihouse/pre-commit-hooks
Expand Down
30 changes: 18 additions & 12 deletions src/qcodes/dataset/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
if TYPE_CHECKING:
from types import TracebackType

from typing_extensions import Self

from qcodes.dataset.descriptions.versioning.rundescribertypes import Shapes
from qcodes.dataset.experiment_container import Experiment
from qcodes.dataset.sqlite.connection import ConnectionPlus
Expand Down Expand Up @@ -881,8 +883,8 @@ def _paramspecbase_from_strings(
return tuple(depends_on), tuple(inf_from)

def register_parent(
self: T, parent: DataSetProtocol, link_type: str, description: str = ""
) -> T:
self: Self, parent: DataSetProtocol, link_type: str, description: str = ""
) -> Self:
"""
Register a parent for the outcome of this measurement
Expand All @@ -906,12 +908,12 @@ def register_parent(
return self

def register_parameter(
self: T,
self: Self,
parameter: ParameterBase,
setpoints: SetpointsType | None = None,
basis: SetpointsType | None = None,
paramtype: str | None = None,
) -> T:
) -> Self:
"""
Add QCoDeS Parameter to the dataset produced by running this
measurement.
Expand Down Expand Up @@ -1034,15 +1036,15 @@ def _infer_paramtype(parameter: ParameterBase, paramtype: str | None) -> str | N
return paramtype

def _register_parameter(
self: T,
self: Self,
name: str,
label: str | None,
unit: str | None,
setpoints: SetpointsType | None,
basis: SetpointsType | None,
paramtype: str,
metadata: dict[str, Any] | None = None,
) -> T:
) -> Self:
"""
Update the interdependencies object with a new group
"""
Expand Down Expand Up @@ -1250,14 +1252,14 @@ def _register_multiparameter(
)

def register_custom_parameter(
self: T,
self: Self,
name: str,
label: str | None = None,
unit: str | None = None,
basis: SetpointsType | None = None,
setpoints: SetpointsType | None = None,
paramtype: str = "numeric",
) -> T:
) -> Self:
"""
Register a custom parameter with this measurement
Expand Down Expand Up @@ -1316,7 +1318,9 @@ def unregister_parameter(self, parameter: SetpointsType) -> None:

log.info(f"Removed {param_name} from Measurement.")

def add_before_run(self: T, func: Callable[..., Any], args: Sequence[Any]) -> T:
def add_before_run(
self: Self, func: Callable[..., Any], args: Sequence[Any]
) -> Self:
"""
Add an action to be performed before the measurement.
Expand All @@ -1336,7 +1340,9 @@ def add_before_run(self: T, func: Callable[..., Any], args: Sequence[Any]) -> T:

return self

def add_after_run(self: T, func: Callable[..., Any], args: Sequence[Any]) -> T:
def add_after_run(
self: Self, func: Callable[..., Any], args: Sequence[Any]
) -> Self:
"""
Add an action to be performed after the measurement.
Expand All @@ -1357,10 +1363,10 @@ def add_after_run(self: T, func: Callable[..., Any], args: Sequence[Any]) -> T:
return self

def add_subscriber(
self: T,
self: Self,
func: Callable[..., Any],
state: MutableSequence[Any] | MutableMapping[Any, Any],
) -> T:
) -> Self:
"""
Add a subscriber to the dataset of the measurement.
Expand Down
30 changes: 16 additions & 14 deletions src/qcodes/instrument/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .instrument_base import InstrumentBase

if TYPE_CHECKING:
from typing_extensions import Unpack
from typing_extensions import Self, Unpack

from .instrument import Instrument
from .instrument_base import InstrumentBaseKWArgs
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -192,11 +192,11 @@ def __init__(
def __getitem__(self, i: int) -> InstrumentModuleType: ...

@overload
def __getitem__(self: T, i: slice | tuple[int, ...]) -> T: ...
def __getitem__(self: Self, i: slice | tuple[int, ...]) -> Self: ...

def __getitem__(
self: T, i: int | slice | tuple[int, ...]
) -> InstrumentModuleType | T:
self: Self, i: int | slice | tuple[int, ...]
) -> InstrumentModuleType | Self:
"""
Return either a single channel, or a new :class:`ChannelTuple`
containing only the specified channels
Expand Down Expand Up @@ -244,7 +244,7 @@ def __repr__(self) -> str:
f"{self._chan_type.__name__}, {self._channels!r})"
)

def __add__(self: T, other: ChannelTuple) -> T:
def __add__(self: Self, other: ChannelTuple) -> Self:
"""
Return a new ChannelTuple containing the channels from both
:class:`ChannelTuple` self and r.
Expand All @@ -270,7 +270,9 @@ def __add__(self: T, other: ChannelTuple) -> T:
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,
Expand Down Expand Up @@ -336,7 +338,7 @@ def count( # pyright: ignore[reportIncompatibleMethodOverride]
"""
return self._channels.count(obj)

def get_channel_by_name(self: T, *names: str) -> InstrumentModuleType | T:
def get_channel_by_name(self: Self, *names: str) -> InstrumentModuleType | Self:
"""
Get a channel by name, or a ChannelTuple if multiple names are given.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
)

Expand All @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions src/qcodes/instrument/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .instrument_meta import InstrumentMeta

if TYPE_CHECKING:
from typing_extensions import Unpack
from typing_extensions import Self, Unpack

from qcodes.logger.instrument_logger import InstrumentLoggerAdapter

Expand Down Expand Up @@ -233,7 +233,7 @@ def record_instance(cls, instance: Instrument) -> None:
cls._instances.add(instance)

@classmethod
def instances(cls: type[T]) -> list[T]:
def instances(cls: type[Self]) -> list[Self]:
"""
Get all currently defined instances of this instrument class.
Expand Down
6 changes: 4 additions & 2 deletions src/qcodes/math_utils/field_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
if TYPE_CHECKING:
from collections.abc import Sequence

from typing_extensions import Self

AllCoordsType = tuple[float, float, float, float, float, float, float]
NormOrder = Literal["fro", "nuc"] | None | float
T = TypeVar("T", bound="FieldVector")
Expand Down Expand Up @@ -179,7 +181,7 @@ def _compute_unknowns(self) -> None:
self._set_attribute_values(FieldVector.attributes, new_values)
break

def copy(self: T, other: T) -> None:
def copy(self: Self, other: Self) -> None:
"""Copy the properties of other vector to yourself."""
for att in FieldVector.attributes:
value = getattr(other, "_" + att)
Expand Down Expand Up @@ -409,7 +411,7 @@ def as_homogeneous(self) -> np.ndarray:
return np.array([self.x, self.y, self.z, 1])

@classmethod
def from_homogeneous(cls: type[T], hvec: np.ndarray) -> T:
def from_homogeneous(cls: type[Self], hvec: np.ndarray) -> Self:
# Homogeneous coordinates define an equivalence relation
# [x / s, y / s, z / s, 1] == [x, y, z, s].
# More generally,
Expand Down
1 change: 0 additions & 1 deletion src/qcodes/parameters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
"Group",
"GroupParameter",
"GroupedParameter",
"GroupedParameter",
"InstrumentRefParameter",
"ManualParameter",
"MultiChannelInstrumentParameter",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_channel_access_is_identical(dci, value, channel) -> None:
def test_invalid_channel_type_raises(empty_instrument: Instrument) -> None:
with pytest.raises(
ValueError,
match="ChannelTuple can only hold instances of type InstrumentChannel",
match="ChannelTuple can only hold instances of type InstrumentModule",
):
ChannelList(
parent=empty_instrument,
Expand Down

0 comments on commit 58bb4fa

Please sign in to comment.