-
Notifications
You must be signed in to change notification settings - Fork 34
Migrate to cattrs for centralized model structuring #1994
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
b1499be
Adopt attrs converters, rename ServerConfig.type to api_type, fix Loc…
iMicknl 229f941
Fix States/CommandDefinitions container semantics
iMicknl 4b22346
Type Event.metadata, deleted_raw_devices_count, and protocol_type
iMicknl 67877b8
Add dict index to CommandDefinitions and States for O(1) lookups
iMicknl 5acf843
Simplify Device to use _flexible_init and resolve ui_class/widget at …
iMicknl 784bb08
Simplify DeviceIdentifier to use __attrs_post_init__ for base_device_url
iMicknl ea74572
Replace _to_sub_places with _to_list("Place") forward reference
iMicknl 3b7f9d5
Replace _to_server_enum and _to_api_type with _to_optional_enum
iMicknl c42663b
Remove unused _LOGGER and stale pylint comment
iMicknl 04dfb45
Change Gateway.id and Place.id from mutable copies to read-only prope…
iMicknl fd4e390
Simplify ActionGroup label default (#1992)
iMicknl ca6ab24
Fix Event.setupoid field name to match decamelize output
iMicknl 89dd381
Fix Execution.action_group type and add missing fields
iMicknl bc72514
Fix Gateway.connectivity type annotation to be optional
iMicknl d6a33e3
Remove unused _to_command_definitions function
iMicknl 9b97555
Reorder models to eliminate string forward references
iMicknl 2ae4046
Migrate to cattrs for centralized model structuring
iMicknl 32c74ec
Simplify cattrs converter by removing redundant hooks
iMicknl 9d71d5d
Improve docstrings for _is_primitive_union and _make_converter functi…
iMicknl 6701da1
Fix Optional[Enum] fields bypassing enum structuring in cattrs converter
iMicknl d9f301b
Add new fields to Event, Gateway, Location, and Setup models for enha…
iMicknl 6f2ae74
Obfuscate country_code field in Location model for enhanced privacy
iMicknl 184f639
Add UpdateCriticityLevel enum and update Gateway model to use it
iMicknl 688a235
Refactor dataclass fields in AuthContext and Credentials for improved…
iMicknl 017b673
Add default values for ui_class and widget in Device model and valida…
iMicknl 569325c
Refactor ActionGroup to use oid as the primary identifier and remove …
iMicknl cb656fb
Merge v2/main and resolve conflicts
Copilot e6f2e96
Guard _structure_command_definitions against None input
iMicknl 872abe1
Add comment explaining Converter over GenConverter choice
iMicknl 49ee925
Lower cattrs minimum version to >=23.2
iMicknl 0e5fd1d
Add comment explaining skipped decamelize for ProtocolType
iMicknl 7991c41
Guard __contains__ against non-str inputs in States and CommandDefini…
iMicknl 26412e1
Validate state.name matches key in States.__setitem__
iMicknl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """Centralized cattrs converter for structuring Overkiz API responses.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import types | ||
| from enum import Enum | ||
| from typing import Any, Union, get_args, get_origin | ||
|
|
||
| import attr | ||
| import cattrs | ||
|
|
||
| from pyoverkiz.models import ( | ||
| CommandDefinition, | ||
| CommandDefinitions, | ||
| State, | ||
| States, | ||
| ) | ||
|
|
||
|
|
||
| def _is_primitive_union(t: Any) -> bool: | ||
| """True for unions of JSON-native types (e.g. StateType). | ||
|
|
||
| Excludes unions containing attrs classes (e.g. Definition | None) since those | ||
| need actual structuring by cattrs. | ||
| """ | ||
| origin = get_origin(t) | ||
| if origin is not Union and not isinstance(t, types.UnionType): | ||
| return False | ||
| non_none = [arg for arg in get_args(t) if arg is not type(None)] | ||
| if any(isinstance(arg, type) and attr.has(arg) for arg in non_none): | ||
| return False | ||
| # Exclude pure Optional[Enum] unions — those need the Enum structure hook. | ||
| return not all(isinstance(arg, type) and issubclass(arg, Enum) for arg in non_none) | ||
|
|
||
|
|
||
| def _make_converter() -> cattrs.Converter: | ||
| # Converter (not GenConverter) so unknown API keys are silently dropped for forward-compat. | ||
| c = cattrs.Converter() | ||
|
iMicknl marked this conversation as resolved.
|
||
|
|
||
| # JSON-native unions like StateType (str | int | float | … | None) are already the | ||
| # correct Python type after JSON parsing — tell cattrs to pass them through as-is. | ||
| c.register_structure_hook_func(_is_primitive_union, lambda v, _: v) | ||
|
|
||
| # Enums: call the constructor so UnknownEnumMixin._missing_ can handle unknown values | ||
| c.register_structure_hook_func( | ||
| lambda t: isinstance(t, type) and issubclass(t, Enum), | ||
| lambda v, t: v if isinstance(v, t) else t(v), | ||
| ) | ||
|
iMicknl marked this conversation as resolved.
iMicknl marked this conversation as resolved.
|
||
|
|
||
| # Custom container types that take a list in __init__ | ||
| def _structure_states(val: Any, _: type) -> States: | ||
| if val is None: | ||
| return States() | ||
| return States([c.structure(s, State) for s in val]) | ||
|
|
||
| def _structure_command_definitions(val: Any, _: type) -> CommandDefinitions: | ||
| if val is None: | ||
| return CommandDefinitions() | ||
| return CommandDefinitions([c.structure(cd, CommandDefinition) for cd in val]) | ||
|
iMicknl marked this conversation as resolved.
|
||
|
|
||
| c.register_structure_hook(States, _structure_states) | ||
| c.register_structure_hook(CommandDefinitions, _structure_command_definitions) | ||
|
|
||
| return c | ||
|
|
||
|
|
||
| converter = _make_converter() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.