Skip to content

Allow enum datatypes to be set to their enum values #174

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 3 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=64", "setuptools_scm[toml]>=8"]
requires = ["setuptools>=70.1", "setuptools_scm[toml]>=8"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version was required for pytest to work in a devcontainer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I have seen this issue recently. For reference, this is the error

$ pytest
ImportError while loading conftest '/workspaces/FastCS/tests/conftest.py'.
tests/conftest.py:17: in <module>
    from aioca import purge_channel_caches
/venv/lib/python3.11/site-packages/aioca/__init__.py:1: in <module>
    from epicscorelibs.ca.cadef import DBE_ALARM, DBE_LOG, DBE_PROPERTY, DBE_VALUE
/venv/lib/python3.11/site-packages/epicscorelibs/ca/cadef.py:22: in <module>
    from epicscorelibs import path
/venv/lib/python3.11/site-packages/epicscorelibs/path/__init__.py:4: in <module>
    from setuptools_dso.runtime import dylink_prepare_dso, find_dso
/venv/lib/python3.11/site-packages/setuptools_dso/__init__.py:10: in <module>
    from .dsocmd import DSO, Extension, install, build, build_dso, build_ext, bdist_egg
/venv/lib/python3.11/site-packages/setuptools_dso/dsocmd.py:26: in <module>
    _bdist_wheel = _import_bdist_wheel()
                   ^^^^^^^^^^^^^^^^^^^^^
/venv/lib/python3.11/site-packages/setuptools_dso/dsocmd.py:21: in _import_bdist_wheel
    from wheel.bdist_wheel import bdist_wheel
/venv/lib/python3.11/site-packages/wheel/bdist_wheel.py:4: in <module>
    warn(
E   DeprecationWarning: The 'wheel' package is no longer the canonical location of the 'bdist_wheel' command, and will be removed in a future release. Please update to setuptools v70.1 or later which contains an integrated version of this command.

Could you split the pyproject change into a separate commit?

build-backend = "setuptools.build_meta"

[project]
Expand All @@ -20,7 +20,7 @@ dependencies = [
"pytango",
"softioc>=4.5.0",
"strawberry-graphql",
"p4p"
"p4p",
]
dynamic = ["version"]
license.file = "LICENSE"
Expand Down Expand Up @@ -53,9 +53,7 @@ dev = [
"httpx",
"tickit~=0.4.3",
]
demo = [
"tickit~=0.4.3",
]
demo = ["tickit~=0.4.3"]

[project.scripts]
fastcs-demo = "fastcs.demo.__main__:main"
Expand Down
11 changes: 11 additions & 0 deletions src/fastcs/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ def __post_init__(self):
def index_of(self, value: T_Enum) -> int:
return self.members.index(value)

def validate(self, value: T) -> T:
enum_vals = [key.value for key in self.dtype]

if value not in enum_vals and not issubclass(type(value), self.dtype):
raise ValueError(
f"Value '{value}' is not a member of {self.dtype} or of "
f"type {self.dtype}"
)

return value

@cached_property
def members(self) -> list[T_Enum]:
return list(self.enum_cls)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_attribute.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import enum
from functools import partial

import numpy as np
Expand Down Expand Up @@ -106,3 +107,21 @@ async def test_handler_initialise(mocker: MockerFixture):
def test_validate(datatype, init_args, value):
with pytest.raises(ValueError):
datatype(**init_args).validate(value)


class MyEnum(enum.Enum):
TEST = "Test"


class MyOtherEnum(enum.Enum):
TEST = "Test"


def test_enum_validate():
enum_datatype = Enum(MyEnum)
enum_datatype.validate(MyEnum.TEST)
enum_datatype.validate("Test")
with pytest.raises(ValueError):
enum_datatype.validate("BadTest")
with pytest.raises(ValueError):
enum_datatype.validate(MyOtherEnum.TEST)