Skip to content
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

Add doc string to SubsetEnumMeta dunder #749

Merged
merged 1 commit into from
Feb 5, 2025
Merged
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
14 changes: 14 additions & 0 deletions src/ophyd_async/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ class StrictEnum(str, Enum, metaclass=StrictEnumMeta):

class SubsetEnumMeta(StrictEnumMeta):
def __call__(self, value, *args, **kwargs): # type: ignore
"""
Returns given value if it is a string and not a member of the enum.
If the value is not a string or is an enum member, default enum behavior
is applied. Type checking will complain if provided arbitrary string.

Returns:
Union[str, SubsetEnum]: If the value is a string and not a member of the
enum, the string is returned as is. Otherwise, the corresponding enum
member is returned.

Raises:
ValueError: If the value is not a string and cannot be converted to an enum
member.
"""
if isinstance(value, str) and not isinstance(value, self):
return value
return super().__call__(value, *args, **kwargs)
Expand Down
Loading