Skip to content

Commit 7af321c

Browse files
authored
Widen Array1D to fix changes to numpy shape (#736)
* Widen Array1D to fix changes to numpy shape numpy/numpy#28077 (comment) * Fix error message output
1 parent aeae55c commit 7af321c

6 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎src/ophyd_async/core/_signal_backend.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
from ._utils import Callback, StrictEnum, T
1111

1212
DTypeScalar_co = TypeVar("DTypeScalar_co", covariant=True, bound=np.generic)
13-
Array1D = np.ndarray[tuple[int], np.dtype[DTypeScalar_co]]
13+
# To be a 1D array shape should really be tuple[int], but np.array()
14+
# currently produces tuple[int, ...] even when it has 1D input args
15+
# https://github.com/numpy/numpy/issues/28077#issuecomment-2566485178
16+
Array1D = np.ndarray[tuple[int, ...], np.dtype[DTypeScalar_co]]
1417
Primitive = bool | int | float | str
1518
# NOTE: if you change this union then update the docs to match
1619
SignalDatatype = (

‎src/ophyd_async/core/_table.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def numpy_table(self, selection: slice | None = None) -> np.ndarray:
101101
v = v[selection]
102102
if array is None:
103103
array = np.empty(v.shape, dtype=self.numpy_dtype())
104-
array[k] = v
104+
array[k] = v # type: ignore
105105
if array is None:
106106
msg = "No arrays found in table"
107107
raise ValueError(msg)

‎src/ophyd_async/epics/core/_util.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def get_supported_values(
4747

4848

4949
def format_datatype(datatype: Any) -> str:
50-
if get_origin(datatype) is np.ndarray and get_args(datatype)[0] == tuple[int]:
50+
if get_origin(datatype) is np.ndarray and get_args(datatype):
5151
dtype = get_dtype(datatype)
5252
return f"Array1D[np.{dtype.name}]"
5353
elif get_origin(datatype) is Sequence:

‎tests/core/test_soft_signal_backend.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ async def test_soft_signal_backend_set_callback():
148148
async def test_soft_signal_backend_with_numpy_typing():
149149
soft_backend = SoftSignalBackend(Array1D[np.float64])
150150
await soft_backend.connect(timeout=1)
151+
await soft_backend.put(np.array([1, 2]), wait=True)
151152
array = await soft_backend.get_value()
152-
assert array.shape == (0,)
153+
assert array.shape == (2,)
154+
assert array[0] == 1
153155

154156

155157
async def test_soft_signal_descriptor_fails_for_invalid_class():

‎tests/core/test_table.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class MyTable(Table):
3333
{"bool": [0, 1], "uint": [3, 4], "str": [44, ""]},
3434
"Input should be a valid string [type=string_type, input_value=44,",
3535
),
36+
(
37+
{"bool": [0, 1], "uint": [[3], [4]], "str": ["", ""]},
38+
"Array 2-dimensional; the target dimensions is 1",
39+
),
3640
],
3741
)
3842
def test_table_wrong_types(kwargs, error_msg):

‎tests/epics/signal/test_signals.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import bluesky.plan_stubs as bps
1010
import numpy as np
11+
import numpy.typing as npt
1112
import pytest
1213
import yaml
1314
from aioca import purge_channel_caches
@@ -38,6 +39,7 @@
3839
epics_signal_w,
3940
epics_signal_x,
4041
)
42+
from ophyd_async.epics.core._util import format_datatype # noqa: PLC2701
4143
from ophyd_async.epics.testing import (
4244
EpicsTestEnum,
4345
EpicsTestIocAndDevices,
@@ -567,6 +569,18 @@ async def test_non_existent_errors(
567569
await signal.connect(timeout=0.1)
568570

569571

572+
@pytest.mark.parametrize(
573+
"dt,expected",
574+
[
575+
(Array1D[np.int32], "Array1D[np.int32]"),
576+
(np.ndarray, "ndarray"),
577+
(npt.NDArray[np.float64], "Array1D[np.float64]"),
578+
],
579+
)
580+
def test_format_error_message(dt, expected):
581+
assert format_datatype(dt) == expected
582+
583+
570584
def test_make_backend_fails_for_different_transports():
571585
read_pv = "test"
572586
write_pv = "pva://test"

0 commit comments

Comments
 (0)