Skip to content

Commit

Permalink
Ruff unsafe and manual fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshnielsen committed May 8, 2024
1 parent cef147a commit c9e9fa5
Show file tree
Hide file tree
Showing 63 changed files with 203 additions and 201 deletions.
4 changes: 2 additions & 2 deletions src/qcodes/dataset/data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def _snapshot_raw(self) -> str | None:
snapshot_raw = select_one_where(
self.conn, "runs", "snapshot", "run_id", self.run_id
)
assert isinstance(snapshot_raw, (str, type(None)))
assert isinstance(snapshot_raw, str | type(None))
return snapshot_raw

@property
Expand Down Expand Up @@ -424,7 +424,7 @@ def _parameters(self) -> str | None:
parameters = select_one_where(
self.conn, "runs", "parameters", "run_id", self.run_id
)
assert isinstance(parameters, (str, type(None)))
assert isinstance(parameters, str | type(None))
return parameters

@property
Expand Down
3 changes: 1 addition & 2 deletions src/qcodes/dataset/data_set_in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import time
import warnings
from collections.abc import Callable
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal

Expand Down Expand Up @@ -50,7 +49,7 @@
from .linked_datasets.links import str_to_links

if TYPE_CHECKING:
from collections.abc import Mapping, Sequence
from collections.abc import Callable, Mapping, Sequence

import pandas as pd
import xarray as xr
Expand Down
9 changes: 1 addition & 8 deletions src/qcodes/dataset/data_set_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import logging
import os
import sys
import warnings
from collections.abc import Callable, Mapping, Sequence
from enum import Enum
from importlib.metadata import entry_points
from pathlib import Path
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -33,13 +33,6 @@
from .exporters.export_to_xarray import xarray_to_h5netcdf_with_complex_numbers
from .sqlite.queries import raw_time_to_str_time

if sys.version_info >= (3, 10):
# new entrypoints api was added in 3.10
from importlib.metadata import entry_points
else:
# 3.9 and earlier
from importlib_metadata import entry_points

if TYPE_CHECKING:
from typing import TypeAlias

Expand Down
2 changes: 1 addition & 1 deletion src/qcodes/dataset/descriptions/detect_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _get_shape_of_step(step: int | np.integer[Any] | Sized | np.ndarray) -> int:


def _param_is_array_like(meas_param: ParameterBase) -> bool:
if isinstance(meas_param, (ArrayParameter, ParameterWithSetpoints)):
if isinstance(meas_param, ArrayParameter | ParameterWithSetpoints):
return True
elif isinstance(meas_param.vals, Arrays):
return True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""
from __future__ import annotations

from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING

from typing_extensions import TypedDict

Expand Down Expand Up @@ -56,7 +56,6 @@ class RunDescriberV3Dict(RunDescriberV2Dict):
# dict from dependent to dict from depenency to num points in grid


RunDescriberDicts = Union[RunDescriberV0Dict,
RunDescriberV1Dict,
RunDescriberV2Dict,
RunDescriberV3Dict]
RunDescriberDicts = (
RunDescriberV0Dict | RunDescriberV1Dict | RunDescriberV2Dict | RunDescriberV3Dict
)
6 changes: 3 additions & 3 deletions src/qcodes/dataset/dond/do_nd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from collections.abc import Callable, Iterator, Sequence
from contextlib import contextmanager
from typing import TYPE_CHECKING, Optional, Union
from typing import TYPE_CHECKING, Optional

if TYPE_CHECKING:
import matplotlib.axes
Expand All @@ -23,7 +23,7 @@
ActionsT = Sequence[Callable[[], None]]
BreakConditionT = Callable[[], bool]

ParamMeasT = Union[ParameterBase, Callable[[], None]]
ParamMeasT = ParameterBase | Callable[[], None]

AxesTuple = tuple["matplotlib.axes.Axes", "matplotlib.colorbar.Colorbar"]
AxesTupleList = tuple[
Expand All @@ -45,7 +45,7 @@ class BreakConditionInterrupt(Exception):
pass


MeasInterruptT = Union[KeyboardInterrupt, BreakConditionInterrupt, None]
MeasInterruptT = KeyboardInterrupt | BreakConditionInterrupt | None


def _register_parameters(
Expand Down
2 changes: 1 addition & 1 deletion src/qcodes/dataset/experiment_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def finished_at(self) -> float | None:
finish_time = select_one_where(
self.conn, "experiments", "end_time", "exp_id", self.exp_id
)
assert isinstance(finish_time, (float, type(None)))
assert isinstance(finish_time, float | type(None))
return finish_time

@property
Expand Down
2 changes: 1 addition & 1 deletion src/qcodes/dataset/guid_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def guids_from_list_str(s: str) -> tuple[str, ...] | None:
else:
return tuple()

if not isinstance(parsed, (ast.List, ast.Tuple, ast.Set)):
if not isinstance(parsed, ast.List | ast.Tuple | ast.Set):
return None

if not all(isinstance(e, ast.Constant) for e in parsed.elts):
Expand Down
12 changes: 6 additions & 6 deletions src/qcodes/dataset/sqlite/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ def get_completed_timestamp_from_run_id(
# sometimes it happens that the timestamp is written to DB as an int
if isinstance(ts, int):
ts = float(ts)
assert isinstance(ts, (float, type(None)))
assert isinstance(ts, float | type(None))
return ts


Expand Down Expand Up @@ -1750,7 +1750,7 @@ def get_parent_dataset_links(conn: ConnectionPlus, run_id: int) -> str:
maybe_mayby_link_str = select_one_where(
conn, "runs", "parent_datasets", "run_id", run_id
)
assert isinstance(maybe_mayby_link_str, (str, type(None)))
assert isinstance(maybe_mayby_link_str, str | type(None))
maybe_link_str = maybe_mayby_link_str

if maybe_link_str is None:
Expand Down Expand Up @@ -1909,7 +1909,7 @@ def get_experiment_name_from_experiment_id(conn: ConnectionPlus, exp_id: int) ->

def get_sample_name_from_experiment_id(conn: ConnectionPlus, exp_id: int) -> str:
sample_name = select_one_where(conn, "experiments", "sample_name", "exp_id", exp_id)
assert isinstance(sample_name, (str, type(None)))
assert isinstance(sample_name, str | type(None))
# there may be a few cases for very old db where None is returned as a sample name
# however, these probably do not exist in relaity outside that test so here we
# cast to str. See test_experiments_with_NULL_sample_name
Expand All @@ -1921,7 +1921,7 @@ def get_run_timestamp_from_run_id(conn: ConnectionPlus, run_id: int) -> float |
# sometimes it happens that the timestamp is saved as an integer in the database
if isinstance(time_stamp, int):
time_stamp = float(time_stamp)
assert isinstance(time_stamp, (float, type(None)))
assert isinstance(time_stamp, float | type(None))
return time_stamp


Expand Down Expand Up @@ -2096,7 +2096,7 @@ def get_experiment_attributes_by_exp_id(
start_time = temp_exp_attrs["start_time"]
assert isinstance(start_time, float)
end_time = temp_exp_attrs["end_time"]
assert isinstance(end_time, (float, type(None)))
assert isinstance(end_time, float | type(None))

exp_attrs: ExperimentAttributeDict = {
"name": str(temp_exp_attrs["name"]),
Expand Down Expand Up @@ -2206,7 +2206,7 @@ def get_raw_run_attributes(
assert isinstance(name, str)

rawsnapshot = select_one_where(conn, "runs", "snapshot", "guid", guid)
assert isinstance(rawsnapshot, (str, type(None)))
assert isinstance(rawsnapshot, str | type(None))
output: RawRunAttributesDict = {
"run_id": run_id,
"experiment": experiment,
Expand Down
4 changes: 2 additions & 2 deletions src/qcodes/dataset/sqlite/query_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import itertools
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, Any, Union
from typing import TYPE_CHECKING, Any

import numpy as np
from numpy import ndarray
Expand All @@ -24,7 +24,7 @@
import sqlite3

# represent the type of data we can/want map to sqlite column
VALUE = Union[str, complex, list, ndarray, bool, None]
VALUE = str | complex | list | ndarray | bool | None
VALUES = Sequence[VALUE]


Expand Down
3 changes: 1 addition & 2 deletions src/qcodes/dataset/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import functools
import logging
import time
from collections.abc import Callable
from queue import Empty, Queue
from threading import Thread
from typing import TYPE_CHECKING, Any

from qcodes.dataset.sqlite.connection import atomic_transaction

if TYPE_CHECKING:
from collections.abc import Mapping
from collections.abc import Callable, Mapping

from qcodes.dataset.data_set import DataSet

Expand Down
8 changes: 5 additions & 3 deletions src/qcodes/instrument/mockers/simulated_ats_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@


import ctypes
from collections.abc import Callable
from typing import Any, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar

import numpy as np

Expand All @@ -21,6 +20,9 @@
)
from qcodes.instrument_drivers.AlazarTech.dll_wrapper import _mark_params_as_updated

if TYPE_CHECKING:
from collections.abc import Callable


class SimulatedATS9360API(AlazarATSAPI):

Expand All @@ -32,7 +34,7 @@ class SimulatedATS9360API(AlazarATSAPI):
def __init__(
self,
dll_path: str | None = None, # Need this for meta super class
buffer_generator: Callable[[np.ndarray], None] | None = None,
buffer_generator: "Callable[[np.ndarray], None] | None" = None,
):
def _default_buffer_generator(buffer: np.ndarray) -> None:
upper = buffer.size // 2
Expand Down
16 changes: 8 additions & 8 deletions src/qcodes/instrument_drivers/AlazarTech/ATS.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
import warnings
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any, Generic, TypeVar, Union, cast
from typing import TYPE_CHECKING, Any, Generic, TypeVar, cast

import numpy as np

Expand All @@ -24,13 +24,13 @@

OutputType = TypeVar('OutputType')

CtypesTypes = Union[
type[ctypes.c_uint8],
type[ctypes.c_uint16],
type[ctypes.c_uint32],
type[ctypes.c_int32],
type[ctypes.c_float],
]
CtypesTypes = (
type[ctypes.c_uint8]
| type[ctypes.c_uint16]
| type[ctypes.c_uint32]
| type[ctypes.c_int32]
| type[ctypes.c_float]
)


class AlazarTech_ATS(Instrument):
Expand Down
10 changes: 4 additions & 6 deletions src/qcodes/instrument_drivers/Harvard/Decadac.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from functools import partial
from time import time
from typing import Union, cast
from typing import cast

import qcodes.validators as vals
from qcodes.instrument import ChannelList, InstrumentChannel, VisaInstrument

number = Union[float, int]


class HarvardDecadacException(Exception):
pass
Expand Down Expand Up @@ -441,9 +439,9 @@ class HarvardDecadac(VisaInstrument, DacReader):
DAC_CHANNEL_CLASS = HarvardDecadacChannel
DAC_SLOT_CLASS = HarvardDecadacSlot

def __init__(self, name: str, address: str,
min_val: number=-5, max_val: number=5,
**kwargs) -> None:
def __init__(
self, name: str, address: str, min_val: float = -5, max_val: float = 5, **kwargs
) -> None:
"""
Creates an instance of the Decadac instruments
Expand Down
8 changes: 5 additions & 3 deletions src/qcodes/instrument_drivers/Keithley/Keithley_2000.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from collections.abc import Callable
from functools import partial
from typing import Any
from typing import TYPE_CHECKING, Any

from qcodes.instrument import VisaInstrument
from qcodes.validators import Bool, Enum, Ints, MultiType, Numbers

if TYPE_CHECKING:
from collections.abc import Callable


def _parse_output_string(s: str) -> str:
"""Parses and cleans string outputs of the Keithley"""
Expand Down Expand Up @@ -204,7 +206,7 @@ def _read_next_value(self) -> float:
return float(self.ask("SENSE:DATA:FRESH?"))

def _get_mode_param(
self, parameter: str, parser: Callable[[str], Any]
self, parameter: str, parser: "Callable[[str], Any]"
) -> float | str | bool:
"""Read the current Keithley mode and ask for a parameter"""
mode = _parse_output_string(self._mode_map[self.mode()])
Expand Down
8 changes: 5 additions & 3 deletions src/qcodes/instrument_drivers/Keithley/Keithley_6500.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from collections.abc import Callable
from functools import partial
from typing import Any, TypeVar
from typing import TYPE_CHECKING, Any, TypeVar

from qcodes.instrument import VisaInstrument
from qcodes.validators import Bool, Enum, Ints, MultiType, Numbers

if TYPE_CHECKING:
from collections.abc import Callable

T = TypeVar("T")


Expand Down Expand Up @@ -231,7 +233,7 @@ def reset(self) -> None:
def _read_next_value(self) -> float:
return float(self.ask("READ?"))

def _get_mode_param(self, parameter: str, parser: Callable[[str], T]) -> T:
def _get_mode_param(self, parameter: str, parser: "Callable[[str], T]") -> T:
"""Reads the current mode of the multimeter and ask for the given parameter.
Args:
Expand Down
27 changes: 13 additions & 14 deletions src/qcodes/instrument_drivers/Keithley/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Union

from ._Keithley_2600 import Keithley2600MeasurementStatus
from .Keithley_2000 import Keithley2000
from .Keithley_2400 import Keithley2400
Expand Down Expand Up @@ -37,18 +35,19 @@
KeithleyS46RelayLock,
)

Keithley26xx = Union[
Keithley2601B,
Keithley2602A,
Keithley2602B,
Keithley2604B,
Keithley2611B,
Keithley2612B,
Keithley2614B,
Keithley2634B,
Keithley2635B,
Keithley2636B,
]
Keithley26xx = (
Keithley2601B
| Keithley2602A
| Keithley2602B
| Keithley2604B
| Keithley2611B
| Keithley2612B
| Keithley2614B
| Keithley2634B
| Keithley2635B
| Keithley2636B
)

"""
All Keithley 26xx SMUs supported by QCoDeS.
"""
Expand Down

0 comments on commit c9e9fa5

Please sign in to comment.