Repository navigation
Refactor WaterModes to use display_name instead of value aliases #990
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,3 +161,37 @@ def test_known_missing_unknown_baseline_inventory() -> None: | |
| f"Enums in KNOWN_ENUMS_MISSING_UNKNOWN are no longer missing 'unknown': {stale_in_baseline}. " | ||
| "Please remove them from KNOWN_ENUMS_MISSING_UNKNOWN." | ||
| ) | ||
|
|
||
|
|
||
| # Baseline inventory of RoborockModeEnum subclasses that intentionally define | ||
| # multiple names sharing one value string (e.g. fault descriptions shared | ||
| # across several firmware codes). These enums accept the silent aliasing | ||
| # behavior; every other mode enum must give each member a distinct value | ||
| # (using display_name for the shared user-facing name) so no code is lost. | ||
| KNOWN_MODE_ENUMS_WITH_VALUE_ALIASES = { | ||
| "roborock.data.b01_q7.b01_q7_code_mappings.B01Fault", | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("mode_enum_cls", to_pytest_params(_ALL_MODE_ENUMS)) | ||
| def test_roborock_mode_enum_no_silent_value_aliases(mode_enum_cls: type[RoborockModeEnum]) -> None: | ||
| """RoborockModeEnum subclasses must not define silent value aliases. | ||
|
|
||
| StrEnum collapses members sharing a value string into one, silently | ||
| discarding the other member's code. Give each member a distinct value and | ||
| use display_name for the shared user-facing name instead. | ||
| """ | ||
| fqn = f"{mode_enum_cls.__module__}.{mode_enum_cls.__name__}" | ||
| if fqn in KNOWN_MODE_ENUMS_WITH_VALUE_ALIASES: | ||
| return | ||
|
Comment on lines
+171
to
+186
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| seen: set[int] = set() | ||
| aliases = [] | ||
| for name, member in mode_enum_cls.__members__.items(): | ||
| if id(member) in seen: | ||
| aliases.append(name) | ||
| seen.add(id(member)) | ||
| assert not aliases, ( | ||
| f"{fqn} defines silent value aliases: {aliases}. " | ||
| "Members sharing a value string collapse into one and lose their codes; " | ||
| "use display_name for the shared user-facing name instead." | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that
225,235,245, and250are registered inWaterModes._code_map_,WaterModes.from_code_optional(value)resolves the 7 representative water-slide codes (221, 225, 230, 235, 240, 245, 250) even whenfeatures.is_water_slide_mode_supportedisFalse, while the other 23 intermediate slider codes (222, 223, 248, etc.) returnNone.This should guard the slider range (
221 <= value <= 250) behindfeatures.is_water_slide_mode_supportedso all 30 slider codes behave consistently on devices without water-slide support: