-
Notifications
You must be signed in to change notification settings - Fork 5
Merge tag 'v0.3.3' into v0.x.x #84
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
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fd68238
Fix warning filtering in pytest configuration
llucax ec129d1
Add microgrid-related ID types
llucax 514aa3f
Add release notes
llucax cc5e5ba
Add microgrid-related ID types (#77)
llucax 1490d0b
Clear release notes
llucax 07b4d82
Clear release notes (#78)
llucax cdb8cce
Fix dependencies and prepare release notes for v0.3.2 release
llucax 895ad7c
Fix dependencies and prepare release notes for v0.3.2 release (#79)
llucax 96e57e6
Clear release notes
llucax 78548f5
Clear release notes (#80)
llucax 16a87c3
Make enums unique
llucax 9fb29df
Add missing component categories
llucax 10d81cb
Add the `HVAC` component category
llucax eb121d9
Add a generic `enum_from_proto()` function
llucax 4320e05
Deprecate uses of enum's `.from_proto()`
llucax 021010b
Bump the minimum version of `frequenz-core` to 1.0.2
llucax 31a2ea8
Update release notes
llucax 2e59182
Improve enums and add a generic `enum_from_proto()` (#82)
llucax 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Conversion of protobuf int enums to Python enums.""" | ||
|
|
||
| import enum | ||
| from typing import Literal, TypeVar, overload | ||
|
|
||
| EnumT = TypeVar("EnumT", bound=enum.Enum) | ||
| """A type variable that is bound to an enum.""" | ||
|
|
||
|
|
||
| @overload | ||
| def enum_from_proto( | ||
| value: int, enum_type: type[EnumT], *, allow_invalid: Literal[False] | ||
| ) -> EnumT: ... | ||
|
|
||
|
|
||
| @overload | ||
| def enum_from_proto( | ||
| value: int, enum_type: type[EnumT], *, allow_invalid: Literal[True] = True | ||
| ) -> EnumT | int: ... | ||
|
|
||
|
|
||
| def enum_from_proto( | ||
| value: int, enum_type: type[EnumT], *, allow_invalid: bool = True | ||
| ) -> EnumT | int: | ||
| """Convert a protobuf int enum value to a python enum. | ||
|
|
||
| Example: | ||
| ```python | ||
| import enum | ||
|
|
||
| from proto import proto_pb2 # Just an example. pylint: disable=import-error | ||
|
|
||
| @enum.unique | ||
| class SomeEnum(enum.Enum): | ||
| # These values should match the protobuf enum values. | ||
| UNSPECIFIED = 0 | ||
| SOME_VALUE = 1 | ||
|
|
||
| enum_value = enum_from_proto(proto_pb2.SomeEnum.SOME_ENUM_SOME_VALUE, SomeEnum) | ||
| # -> SomeEnum.SOME_VALUE | ||
|
|
||
| enum_value = enum_from_proto(42, SomeEnum) | ||
| # -> 42 | ||
|
|
||
| enum_value = enum_from_proto( | ||
| proto_pb2.SomeEnum.SOME_ENUM_UNKNOWN_VALUE, SomeEnum, allow_invalid=False | ||
| ) | ||
| # -> ValueError | ||
| ``` | ||
|
|
||
| Args: | ||
| value: The protobuf int enum value. | ||
| enum_type: The python enum type to convert to. | ||
| allow_invalid: If `True`, return the value as an `int` if the value is not | ||
| a valid member of the enum (this allows for forward-compatibility with new | ||
| enum values defined in the protocol but not added to the Python enum yet). | ||
| If `False`, raise a `ValueError` if the value is not a valid member of the | ||
| enum. | ||
|
|
||
| Returns: | ||
| The resulting python enum value if the protobuf value is known, otherwise | ||
| the input value converted to a plain `int`. | ||
|
|
||
| Raises: | ||
| ValueError: If `allow_invalid` is `False` and the value is not a valid member | ||
| of the enum. | ||
| """ | ||
| try: | ||
| return enum_type(value) | ||
| except ValueError: | ||
| if allow_invalid: | ||
| return value | ||
| raise |
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,13 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Microgrid sensors.""" | ||
|
|
||
| from typing import final | ||
|
|
||
| from frequenz.core.id import BaseId | ||
|
|
||
|
|
||
| @final | ||
| class SensorId(BaseId, str_prefix="SID"): | ||
| """A unique identifier for a microgrid sensor.""" |
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,28 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Tests for microgrid-related IDs.""" | ||
|
|
||
| import pytest | ||
| from frequenz.core.id import BaseId | ||
|
|
||
| from frequenz.client.common.microgrid import EnterpriseId, MicrogridId | ||
| from frequenz.client.common.microgrid.components import ComponentId | ||
| from frequenz.client.common.microgrid.sensors import SensorId | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "id_class, prefix", | ||
| [ | ||
| (EnterpriseId, "EID"), | ||
| (MicrogridId, "MID"), | ||
| (ComponentId, "CID"), | ||
| (SensorId, "SID"), | ||
| ], | ||
| ) | ||
| def test_string_representation(id_class: type[BaseId], prefix: str) -> None: | ||
| """Test string representation of IDs.""" | ||
| _id = id_class(123) | ||
|
|
||
| assert str(_id) == f"{prefix}123" | ||
| assert repr(_id) == f"{id_class.__name__}(123)" |
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.
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.
The simple "error" filter may not match any specific warning category; consider using explicit filters (e.g., "error::Warning") or re-enable
-Werrorto ensure all warnings are treated as errors as intended.