Skip to content

Commit

Permalink
support for str in mbbi/mbbo (#499)
Browse files Browse the repository at this point in the history
closes #451
  • Loading branch information
ZohebShaikh authored Aug 2, 2024
1 parent 8273a2e commit 8e532d6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ophyd_async/epics/signal/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_supported_values(
f"which do not match {datatype}, which has {choices}."
)
return {x: datatype(x) if x else "_" for x in pv_choices}
elif datatype is None:
elif datatype is None or datatype is str:
return {x: x or "_" for x in pv_choices}

raise TypeError(
Expand Down
10 changes: 8 additions & 2 deletions tests/epics/signal/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ class MyEnum(str, Enum):
get_supported_values("", MyEnum, ("test",))


def test_given_no_supplied_enum_then_returns_generated_choices_enum_with_pv_choices():
supported_vals = get_supported_values("", None, ("test",))
@pytest.mark.parametrize(
"datatype",
[None, str],
)
def test_given_no_enum_or_string_then_returns_generated_choices_enum_with_pv_choices(
datatype,
):
supported_vals = get_supported_values("", datatype, ("test",))
assert len(supported_vals) == 1
assert "test" in supported_vals

Expand Down
13 changes: 13 additions & 0 deletions tests/epics/signal/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,19 @@ async def test_str_enum_returns_enum(ioc: IOC):
assert val == "Bbb"


async def test_str_datatype_in_mbbo(ioc: IOC):
backend = await ioc.make_backend(MyEnum, "enum")
pv_name = f"{ioc.protocol}://{PV_PREFIX}:{ioc.protocol}:enum"
sig = epics_signal_rw(str, pv_name)
datakey = await backend.get_datakey(sig.source)
assert datakey["choices"] == ["Aaa", "Bbb", "Ccc"]
await sig.connect()
description = await sig.describe()
assert description[""]["choices"] == ["Aaa", "Bbb", "Ccc"]
val = await sig.get_value()
assert val == "Bbb"


async def test_runtime_enum_returns_str(ioc: IOC):
await ioc.make_backend(MySubsetEnum, "enum")
pv_name = f"{ioc.protocol}://{PV_PREFIX}:{ioc.protocol}:enum"
Expand Down

0 comments on commit 8e532d6

Please sign in to comment.