Skip to content

Commit

Permalink
Check for numeric type using numbers.Real
Browse files Browse the repository at this point in the history
isinstance(np.int32(0), int) evaluates to false for example.
  • Loading branch information
thangleiter committed Jan 17, 2025
1 parent 76ae6ae commit 8864baa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/qcodes/instrument_drivers/american_magnetics/AMI430.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ def _raise_if_not_same_field_and_ramp_rate_units(self) -> tuple[str, str]:
def _verify_safe_setpoint(
self, setpoint_values: tuple[float, float, float]
) -> bool:
if isinstance(self._field_limit, (int, float)):
if isinstance(self._field_limit, numbers.Real):
return bool(np.linalg.norm(setpoint_values) < self._field_limit)

answer = any(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ def _raise_if_not_same_field_and_ramp_rate_units(self) -> tuple[str, str]:
def _verify_safe_setpoint(
self, setpoint_values: tuple[float, float, float]
) -> bool:
if isinstance(self._field_limit, (int, float)):
if isinstance(self._field_limit, numbers.Real):
return bool(np.linalg.norm(setpoint_values) < self._field_limit)

answer = any(
Expand Down
7 changes: 4 additions & 3 deletions src/qcodes/math_utils/field_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import annotations

import numbers
from typing import TYPE_CHECKING, Any, ClassVar, Literal, TypeVar

import numpy as np
Expand Down Expand Up @@ -292,21 +293,21 @@ def __setitem__(self, component: str, value: float) -> None:
self.set_component(**{component: value})

def __mul__(self, other: Any) -> FieldVector:
if not isinstance(other, (float, int)):
if not isinstance(other, numbers.Real):
return NotImplemented

return FieldVector(
**{component: self[component] * other for component in "xyz"}
)

def __rmul__(self, other: Any) -> FieldVector:
if not isinstance(other, (int, float)):
if not isinstance(other, numbers.Real):
return NotImplemented

return self * other

def __truediv__(self, other: Any) -> FieldVector:
if not isinstance(other, (int, float)):
if not isinstance(other, numbers.Real):
return NotImplemented

return self * (1.0 / other)
Expand Down
11 changes: 6 additions & 5 deletions src/qcodes/parameters/parameter_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import collections.abc
import logging
import numbers
import time
import warnings
from contextlib import contextmanager
Expand Down Expand Up @@ -809,8 +810,8 @@ def get_ramp_values(
self.get()
start_value = self.get_latest()
if not (
isinstance(start_value, (int, float))
and isinstance(value, (int, float))
isinstance(start_value, numbers.Real)
and isinstance(value, numbers.Real)
):
# parameter is numeric but either one of the endpoints
# is not or the starting point is unknown. The later
Expand Down Expand Up @@ -888,7 +889,7 @@ def step(self, step: float | None) -> None:
self._step: float | None = step
elif not all(getattr(vals, "is_numeric", True) for vals in self._vals):
raise TypeError("you can only step numeric parameters")
elif not isinstance(step, (int, float)):
elif not isinstance(step, numbers.Real):
raise TypeError("step must be a number")
elif step == 0:
self._step = None
Expand Down Expand Up @@ -928,7 +929,7 @@ def post_delay(self) -> float:

@post_delay.setter
def post_delay(self, post_delay: float) -> None:
if not isinstance(post_delay, (int, float)):
if not isinstance(post_delay, numbers.Real):
raise TypeError(f"post_delay ({post_delay}) must be a number")
if post_delay < 0:
raise ValueError(f"post_delay ({post_delay}) must not be negative")
Expand Down Expand Up @@ -957,7 +958,7 @@ def inter_delay(self) -> float:

@inter_delay.setter
def inter_delay(self, inter_delay: float) -> None:
if not isinstance(inter_delay, (int, float)):
if not isinstance(inter_delay, numbers.Real):
raise TypeError(f"inter_delay ({inter_delay}) must be a number")
if inter_delay < 0:
raise ValueError(f"inter_delay ({inter_delay}) must not be negative")
Expand Down

0 comments on commit 8864baa

Please sign in to comment.