Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace remaining use of optional / union with 3.10 style #6484

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
5 changes: 2 additions & 3 deletions src/qcodes/dataset/data_set_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Any,
Literal,
Protocol,
Union,
runtime_checkable,
)

Expand Down Expand Up @@ -56,8 +55,8 @@
str | complex | np.integer | np.floating | np.complexfloating
)
values_type: TypeAlias = scalar_res_types | np.ndarray | Sequence[scalar_res_types]
res_type: TypeAlias = tuple[Union["ParameterBase", str], values_type]
setpoints_type: TypeAlias = Sequence[Union[str, "ParameterBase"]]
res_type: TypeAlias = "tuple[ParameterBase | str, values_type]"
setpoints_type: TypeAlias = "Sequence[str | ParameterBase]"
SPECS: TypeAlias = 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
Expand Down
9 changes: 4 additions & 5 deletions src/qcodes/dataset/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@
import itertools
import logging
from collections import defaultdict
from collections.abc import Callable
from functools import partial
from typing import TYPE_CHECKING, Protocol, TypeVar, Union
from typing import TYPE_CHECKING, Protocol, TypeAlias, TypeVar

from qcodes.utils import RespondingThread

if TYPE_CHECKING:
from collections.abc import Sequence
from collections.abc import Callable, Sequence
from types import TracebackType

from qcodes.dataset.data_set_protocol import values_type
from qcodes.parameters import ParamDataType, ParameterBase

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

T = TypeVar("T")

Expand Down
61 changes: 31 additions & 30 deletions src/qcodes/instrument_drivers/AlazarTech/ats_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import ctypes
from ctypes import POINTER
from typing import TYPE_CHECKING, Any, ClassVar, Union
from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias

# `ParameterBase` is needed because users may pass instrument parameters
# that originate from `Instrument.parameters` dictionary which is typed
Expand All @@ -30,6 +30,9 @@
POINTER_c_long = Any


int_or_param: TypeAlias = "int | Parameter"


class AlazarATSAPI(WrappedDll):
"""
A thread-safe wrapper for the ATS API library.
Expand All @@ -54,7 +57,7 @@ class AlazarATSAPI(WrappedDll):
signatures: ClassVar[dict[str, Signature]] = {}

def set_trigger_time_out(
self, handle: int, timeout_ticks: Union[int, "Parameter"]
self, handle: int, timeout_ticks: int_or_param
) -> ReturnCode:
return self._sync_dll_call("AlazarSetTriggerTimeOut", handle, timeout_ticks)

Expand Down Expand Up @@ -227,10 +230,10 @@ def set_parameter(
def set_capture_clock(
self,
handle: int,
source_id: Union[int, "Parameter"],
sample_rate_id_or_value: Union[int, "Parameter"],
edge_id: Union[int, "Parameter"],
decimation: Union[int, "Parameter"],
source_id: int_or_param,
sample_rate_id_or_value: int_or_param,
edge_id: int_or_param,
decimation: int_or_param,
) -> ReturnCode:
return self._sync_dll_call(
"AlazarSetCaptureClock",
Expand All @@ -252,10 +255,10 @@ def set_capture_clock(
def input_control(
self,
handle: int,
channel_id: Union[int, "Parameter"],
coupling_id: Union[int, "Parameter"],
range_id: Union[int, "Parameter"],
impedance_id: Union[int, "Parameter"],
channel_id: int_or_param,
coupling_id: int_or_param,
range_id: int_or_param,
impedance_id: int_or_param,
) -> ReturnCode:
return self._sync_dll_call(
"AlazarInputControl",
Expand All @@ -273,8 +276,8 @@ def input_control(
def set_bw_limit(
self,
handle: int,
channel_id: Union[int, "Parameter"],
flag: Union[int, "Parameter"],
channel_id: int_or_param,
flag: int_or_param,
) -> ReturnCode:
return self._sync_dll_call("AlazarSetBWLimit", handle, channel_id, flag)

Expand All @@ -285,15 +288,15 @@ def set_bw_limit(
def set_trigger_operation(
self,
handle: int,
trigger_operation: Union[int, "Parameter"],
trigger_engine_id_1: Union[int, "Parameter"],
source_id_1: Union[int, "Parameter"],
slope_id_1: Union[int, "Parameter"],
level_1: Union[int, "Parameter"],
trigger_engine_id_2: Union[int, "Parameter"],
source_id_2: Union[int, "Parameter"],
slope_id_2: Union[int, "Parameter"],
level_2: Union[int, "Parameter"],
trigger_operation: int_or_param,
trigger_engine_id_1: int_or_param,
source_id_1: int_or_param,
slope_id_1: int_or_param,
level_1: int_or_param,
trigger_engine_id_2: int_or_param,
source_id_2: int_or_param,
slope_id_2: int_or_param,
level_2: int_or_param,
) -> ReturnCode:
return self._sync_dll_call(
"AlazarSetTriggerOperation",
Expand All @@ -320,8 +323,8 @@ def set_trigger_operation(
def set_external_trigger(
self,
handle: int,
coupling_id: Union[int, "Parameter"],
range_id: Union[int, "Parameter"],
coupling_id: int_or_param,
range_id: int_or_param,
) -> ReturnCode:
return self._sync_dll_call(
"AlazarSetExternalTrigger", handle, coupling_id, range_id
Expand All @@ -331,9 +334,7 @@ def set_external_trigger(
{"AlazarSetExternalTrigger": Signature(argument_types=[HANDLE, U32, U32])}
)

def set_trigger_delay(
self, handle: int, value: Union[int, "Parameter"]
) -> ReturnCode:
def set_trigger_delay(self, handle: int, value: int_or_param) -> ReturnCode:
return self._sync_dll_call("AlazarSetTriggerDelay", handle, value)

signatures.update(
Expand All @@ -343,8 +344,8 @@ def set_trigger_delay(
def configure_aux_io(
self,
handle: int,
mode_id: Union[int, "Parameter"],
mode_parameter_value: Union[int, "Parameter"],
mode_id: int_or_param,
mode_parameter_value: int_or_param,
) -> ReturnCode:
return self._sync_dll_call(
"AlazarConfigureAuxIO", handle, mode_id, mode_parameter_value
Expand All @@ -357,8 +358,8 @@ def configure_aux_io(
def set_record_size(
self,
handle: int,
pre_trigger_samples: Union[int, "Parameter"],
post_trigger_samples: Union[int, "Parameter"],
pre_trigger_samples: int_or_param,
post_trigger_samples: int_or_param,
) -> ReturnCode:
return self._sync_dll_call(
"AlazarSetRecordSize", handle, pre_trigger_samples, post_trigger_samples
Expand Down
4 changes: 2 additions & 2 deletions src/qcodes/instrument_drivers/Keithley/Keithley_2450.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, ClassVar, Optional, cast
from typing import TYPE_CHECKING, Any, ClassVar, cast

import numpy as np
from typing_extensions import TypedDict, Unpack
Expand Down Expand Up @@ -138,7 +138,7 @@ def __exit__(
self,
exception_type: type[BaseException] | None,
value: BaseException | None,
traceback: Optional["TracebackType"],
traceback: "TracebackType | None",
) -> None:
self.delete()

Expand Down
6 changes: 3 additions & 3 deletions src/qcodes/instrument_drivers/Keithley/Keithley_7510.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, ClassVar, Optional, TypedDict, cast
from typing import TYPE_CHECKING, Any, ClassVar, TypedDict, cast

import numpy as np

Expand Down Expand Up @@ -36,7 +36,7 @@ def __init__(
self,
names: "Sequence[str]",
shapes: "Sequence[Sequence[int]]",
setpoints: Optional["Sequence[Sequence[Any]]"],
setpoints: "Sequence[Sequence[Any]] | None",
**kwargs: Any,
):
super().__init__(
Expand Down Expand Up @@ -282,7 +282,7 @@ def __exit__(
self,
exception_type: type[BaseException] | None,
value: BaseException | None,
traceback: Optional["TracebackType"],
traceback: "TracebackType | None",
) -> None:
self.delete()

Expand Down
8 changes: 4 additions & 4 deletions src/qcodes/instrument_drivers/Keysight/Infiniium.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar, Literal, Optional, Union
from typing import TYPE_CHECKING, Any, ClassVar, Literal

import numpy as np
from pyvisa import VisaIOError
Expand Down Expand Up @@ -102,7 +102,7 @@ class DSOTraceParam(ParameterWithSetpoints):
def __init__(
self,
name: str,
instrument: Union["KeysightInfiniiumChannel", "KeysightInfiniiumFunction"],
instrument: "KeysightInfiniiumChannel | KeysightInfiniiumFunction",
channel: str,
**kwargs: Any,
):
Expand Down Expand Up @@ -170,7 +170,7 @@ def unit(self, unit: Any) -> None:
"""
return

def update_setpoints(self, preamble: Optional["Sequence[str]"] = None) -> None:
def update_setpoints(self, preamble: "Sequence[str] | None" = None) -> None:
"""
Update waveform parameters. Must be called before data
acquisition if instr.cache_setpoints is False
Expand Down Expand Up @@ -466,7 +466,7 @@ def _create_query(self, cmd: str, pre_cmd: str = "", post_cmd: str = "") -> str:
class KeysightInfiniiumBoundMeasurement(AbstractMeasurementSubsystem):
def __init__(
self,
parent: Union["KeysightInfiniiumChannel", "KeysightInfiniiumFunction"],
parent: "KeysightInfiniiumChannel | KeysightInfiniiumFunction",
name: str,
**kwargs: "Unpack[InstrumentBaseKWArgs]",
):
Expand Down
4 changes: 2 additions & 2 deletions src/qcodes/instrument_drivers/Keysight/keysight_34934a.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import re
from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING

from qcodes import validators

Expand Down Expand Up @@ -32,7 +32,7 @@ class Keysight34934A(Keysight34980ASwitchMatrixSubModule):

def __init__(
self,
parent: Union["VisaInstrument", "InstrumentChannel"],
parent: "VisaInstrument | InstrumentChannel",
name: str,
slot: int,
**kwargs: "Unpack[InstrumentBaseKWArgs]",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import textwrap
from collections import defaultdict
from typing import TYPE_CHECKING, Any, Optional
from typing import TYPE_CHECKING, Any

from qcodes.instrument import VisaInstrument, VisaInstrumentKWArgs
from qcodes.parameters import MultiParameter, Parameter, create_on_off_val_mapping
Expand Down Expand Up @@ -516,9 +516,9 @@ def __init__(self, name: str, instrument: KeysightB1517A, **kwargs: Any):

def set_names_labels_and_units(
self,
names: Optional["Sequence[str]"] = None,
labels: Optional["Sequence[str]"] = None,
units: Optional["Sequence[str]"] = None,
names: "Sequence[str] | None" = None,
labels: "Sequence[str] | None" = None,
units: "Sequence[str] | None" = None,
) -> None:
"""
Set names, labels, and units of the measured parts of the MultiParameter.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
import textwrap
from typing import TYPE_CHECKING, Any, Literal, Optional, cast, overload
from typing import TYPE_CHECKING, Any, Literal, cast, overload

import numpy as np
from typing_extensions import NotRequired, TypedDict, Unpack
Expand Down Expand Up @@ -713,7 +713,7 @@ def measurement_status(self) -> MeasurementStatus | None:
def snapshot_base(
self,
update: bool | None = True,
params_to_skip_update: Optional["Sequence[str]"] = None,
params_to_skip_update: "Sequence[str] | None" = None,
) -> dict[Any, Any]:
snapshot = super().snapshot_base(
update=update, params_to_skip_update=params_to_skip_update
Expand Down
6 changes: 3 additions & 3 deletions src/qcodes/instrument_drivers/QDev/QDac_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import time
from functools import partial
from typing import TYPE_CHECKING, Any, Optional
from typing import TYPE_CHECKING, Any

import pyvisa
import pyvisa.constants
Expand Down Expand Up @@ -142,7 +142,7 @@ def __init__(
def snapshot_base(
self,
update: bool | None = False,
params_to_skip_update: Optional["Sequence[str]"] = None,
params_to_skip_update: "Sequence[str] | None" = None,
) -> dict[Any, Any]:
update_currents = self._parent._update_currents and update
if update and not self._parent._get_status_performed:
Expand Down Expand Up @@ -327,7 +327,7 @@ def __init__(
def snapshot_base(
self,
update: bool | None = False,
params_to_skip_update: Optional["Sequence[str]"] = None,
params_to_skip_update: "Sequence[str] | None" = None,
) -> dict[Any, Any]:
update_currents = self._update_currents and update is True
if update:
Expand Down
4 changes: 2 additions & 2 deletions src/qcodes/instrument_drivers/rigol/Rigol_DG4000.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import partial
from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING

from qcodes.instrument import VisaInstrument, VisaInstrumentKWArgs
from qcodes.validators import Anything, Enum, Ints, MultiType, Numbers
Expand Down Expand Up @@ -745,7 +745,7 @@ def __init__(

self.connect_message()

def _upload_data(self, data: Union["Sequence[float]", "np.ndarray"]) -> None:
def _upload_data(self, data: "Sequence[float] | np.ndarray") -> None:
"""
Upload data to the AWG memory.
Expand Down
8 changes: 4 additions & 4 deletions src/qcodes/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from contextlib import contextmanager
from copy import copy
from datetime import datetime
from typing import TYPE_CHECKING, Any, Optional, Union
from typing import TYPE_CHECKING, Any

from typing_extensions import deprecated

Expand Down Expand Up @@ -66,7 +66,7 @@
# console hander.
console_handler: logging.Handler | None = None
file_handler: logging.Handler | None = None
telemetry_handler: Optional["AzureLogHandler"] = None
telemetry_handler: AzureLogHandler | None = None


_opencensus_filter = logging.Filter(name="opencensus")
Expand Down Expand Up @@ -385,7 +385,7 @@ def running_in_test_or_tool() -> bool:

@contextmanager
def handler_level(
level: LevelType, handler: Union[logging.Handler, "Sequence[logging.Handler]"]
level: LevelType, handler: "logging.Handler | Sequence[logging.Handler]"
) -> "Iterator[None]":
"""
Context manager to temporarily change the level of handlers.
Expand Down Expand Up @@ -469,7 +469,7 @@ def __exit__(
self,
exception_type: type[BaseException] | None,
exception_value: BaseException | None,
traceback: Optional["TracebackType"],
traceback: "TracebackType | None",
) -> None:
self.logger.removeHandler(self.string_handler)
self.value = self.log_capture.getvalue()
Expand Down
Loading
Loading