Skip to content

Augment base enumeration class to behave well in string contexts#192

Merged
kroenlein merged 2 commits intomainfrom
feature/robust-enum
Jun 13, 2023
Merged

Augment base enumeration class to behave well in string contexts#192
kroenlein merged 2 commits intomainfrom
feature/robust-enum

Conversation

@kroenlein
Copy link
Copy Markdown
Collaborator

This PR modifies the Base Enumeration class to behave better in string contexts, as well as support synonyms and better string matching.

This does not change any behavior that was previously under testing nor change any previously defined interfaces.

@StevenKauwe
Copy link
Copy Markdown

Can I get some context how this is being used? I'm curious why we need to be able to handle strings with mixed lower/uppercase. Where are these strings coming from?

@kroenlein
Copy link
Copy Markdown
Collaborator Author

Can I get some context how this is being used? I'm curious why we need to be able to handle strings with mixed lower/uppercase. Where are these strings coming from?

If you look at routes supported by citrine-python, you'll see several ostensibly enumerated field entries that are only being represented as strings. You'll also note the casing choice on those enumerations is actually end-point specific. This removes sensitivity to a user knowing the casing details of particular routes, through giving access to a Enum that still behaves cleanly in a string context, case-insensitive object deserialization, and easing library interactions.

anoto-moniz
anoto-moniz previously approved these changes Jun 6, 2023
Copy link
Copy Markdown
Contributor

@anoto-moniz anoto-moniz left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me. I presume you have some place in mind you'd like to use this?

@kroenlein
Copy link
Copy Markdown
Collaborator Author

Seems reasonable to me. I presume you have some place in mind you'd like to use this?

Every instance of citrine-python's use of BaseEnumeration. Plus they're just more pleasant. They've been in gemd-ingest for a long time.

@StevenKauwe
Copy link
Copy Markdown

Can you clarify the asserts? I'm confused by line 61 especially.

@kroenlein
Copy link
Copy Markdown
Collaborator Author

Can you clarify the asserts? I'm confused by line 61 especially.

ONE = "One", "1"
...
assert TestEnum.ONE != "1", "Equality worked for synonym"

The synonym mechanism allows you to have multiple inputs that map to the same enumerated value, but equality doesn't work because the string form is always the first entry: str(assert TestEnum.ONE) == "One". Many inputs, one output.

self._template = None
self._origin = None
self._file_links = None,
self._file_links = None
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Collateral discovery. Just a cosmetic issue b/c the attribute is initialized 4 lines later.


@property
def origin(self) -> str:
def origin(self) -> Origin:
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Origin is a str, so this is non-breaking and more intuitive.

Comment thread gemd/entity/setters.py


def validate_list(obj: Union[Iterable[T], T],
def validate_list(obj: Optional[Union[Iterable[T], T]],
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Collateral discovery. It accepts a None, initializing to empty list, so why is that not documented?

Comment thread tests/json/test_json.py
Comment on lines +115 to +116
assert copy_condition.notes == Origin.UNKNOWN.value
assert not isinstance(copy_condition.notes, Origin)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

get_value is deprecated, plus this is a better test anyway.

@kroenlein kroenlein requested a review from anoto-moniz June 12, 2023 15:11
Copy link
Copy Markdown

@StevenKauwe StevenKauwe left a comment

Choose a reason for hiding this comment

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

LGTM! The added doc strings are very helpful ❤️.

@kroenlein kroenlein merged commit ad7e5fe into main Jun 13, 2023
@kroenlein kroenlein deleted the feature/robust-enum branch June 13, 2023 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants