Skip to content

Commit

Permalink
add S2 Message Union type (#37)
Browse files Browse the repository at this point in the history
* add S2 Message Union type

Signed-off-by: Victor Garcia Reolid <[email protected]>

* chore: move to Literals as Pydantic 2 does

* chore: run `datamodel-codegen --input specification/openapi.yml --input-file-type openapi --output src/s2python/generated/gen_s2.py`

Signed-off-by: F.N. Claessen <[email protected]>

* refactor: rename old S2Message to S2MessageComponent

Signed-off-by: F.N. Claessen <[email protected]>

* style: pylint

Signed-off-by: F.N. Claessen <[email protected]>

* refactor: prefer S2Message over S2MessageComponent

Signed-off-by: F.N. Claessen <[email protected]>

* Revert "refactor: prefer S2Message over S2MessageComponent"

This reverts commit fd59de6.

* refactor: prefer S2Message over S2MessageComponent in s2_parser.py

Signed-off-by: F.N. Claessen <[email protected]>

* fix: `src/s2python/s2_parser.py:49: error: Dict entry 8 has incompatible type "str": "type[PPBCScheduleInstruction]"; expected "str": "Union[type[FRBCActuatorStatus], type[FRBCFillLevelTargetProfile], type[FRBCInstruction], type[FRBCLeakageBehaviour], type[FRBCStorageStatus], <13 more items>]"  [dict-item]`

Signed-off-by: F.N. Claessen <[email protected]>

* refactor: prefer S2Message over S2MessageComponent in s2_control_type.py

Signed-off-by: F.N. Claessen <[email protected]>

* refactor: prefer S2Message over S2MessageComponent in s2_connection.py

Signed-off-by: F.N. Claessen <[email protected]>

* fix: s2_parser.py has 1 place that still requires the S2MessageComponent

Signed-off-by: F.N. Claessen <[email protected]>

* dev: change subject_message_id to message_id (did mypy find a bug? Related to flexiblepower/s2-ws-json#21)

Signed-off-by: F.N. Claessen <[email protected]>

* Revert "dev: change subject_message_id to message_id (did mypy find a bug? Related to flexiblepower/s2-ws-json#21)"

This reverts commit dc3d706.

* fix: type ignore union-attr instead; underlying issue should be fixed under flexiblepower/s2-ws-json#21 instead

Signed-off-by: F.N. Claessen <[email protected]>

* fix: linting

Signed-off-by: F.N. Claessen <[email protected]>

---------

Signed-off-by: Victor Garcia Reolid <[email protected]>
Signed-off-by: F.N. Claessen <[email protected]>
Co-authored-by: F.N. Claessen <[email protected]>
  • Loading branch information
victorgarcia98 and Flix6x authored Jan 23, 2025
1 parent c35b7e4 commit cb08e31
Show file tree
Hide file tree
Showing 49 changed files with 161 additions and 98 deletions.
12 changes: 12 additions & 0 deletions development_utilities/get_all_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import inspect
import s2python.frbc as frbc
import s2python.common as common

from pydantic import BaseModel

all_members = inspect.getmembers(frbc) + inspect.getmembers(common)
all_members.sort(key=lambda t: t[0])

for name, member in all_members:
if inspect.isclass(member) and issubclass(member, BaseModel) and "message_type" in member.__fields__:
print(f"{name},")
4 changes: 2 additions & 2 deletions src/s2python/common/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from s2python.generated.gen_s2 import Duration as GenDuration
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class Duration(GenDuration, S2Message["Duration"]):
class Duration(GenDuration, S2MessageComponent["Duration"]):
def to_timedelta(self) -> timedelta:
return timedelta(milliseconds=self.root)

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from s2python.generated.gen_s2 import Handshake as GenHandshake
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class Handshake(GenHandshake, S2Message["Handshake"]):
class Handshake(GenHandshake, S2MessageComponent["Handshake"]):
model_config = GenHandshake.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/handshake_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from s2python.generated.gen_s2 import HandshakeResponse as GenHandshakeResponse
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class HandshakeResponse(GenHandshakeResponse, S2Message["HandshakeResponse"]):
class HandshakeResponse(GenHandshakeResponse, S2MessageComponent["HandshakeResponse"]):
model_config = GenHandshakeResponse.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/instruction_status_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
)
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class InstructionStatusUpdate(GenInstructionStatusUpdate, S2Message["InstructionStatusUpdate"]):
class InstructionStatusUpdate(GenInstructionStatusUpdate, S2MessageComponent["InstructionStatusUpdate"]):
model_config = GenInstructionStatusUpdate.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/number_range.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Any

from s2python.validate_values_mixin import S2Message, catch_and_convert_exceptions
from s2python.validate_values_mixin import S2MessageComponent, catch_and_convert_exceptions
from s2python.generated.gen_s2 import NumberRange as GenNumberRange


@catch_and_convert_exceptions
class NumberRange(GenNumberRange, S2Message["NumberRange"]):
class NumberRange(GenNumberRange, S2MessageComponent["NumberRange"]):
model_config = GenNumberRange.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/power_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from s2python.generated.gen_s2 import PowerForecast as GenPowerForecast
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class PowerForecast(GenPowerForecast, S2Message["PowerForecast"]):
class PowerForecast(GenPowerForecast, S2MessageComponent["PowerForecast"]):
model_config = GenPowerForecast.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/power_forecast_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from s2python.generated.gen_s2 import PowerForecastElement as GenPowerForecastElement
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)
from s2python.common.duration import Duration
from s2python.common.power_forecast_value import PowerForecastValue


@catch_and_convert_exceptions
class PowerForecastElement(GenPowerForecastElement, S2Message["PowerForecastElement"]):
class PowerForecastElement(GenPowerForecastElement, S2MessageComponent["PowerForecastElement"]):
model_config = GenPowerForecastElement.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/power_forecast_value.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from s2python.generated.gen_s2 import PowerForecastValue as GenPowerForecastValue
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class PowerForecastValue(GenPowerForecastValue, S2Message["PowerForecastValue"]):
class PowerForecastValue(GenPowerForecastValue, S2MessageComponent["PowerForecastValue"]):
model_config = GenPowerForecastValue.model_config
model_config["validate_assignment"] = True
4 changes: 2 additions & 2 deletions src/s2python/common/power_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from s2python.generated.gen_s2 import PowerMeasurement as GenPowerMeasurement
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class PowerMeasurement(GenPowerMeasurement, S2Message["PowerMeasurement"]):
class PowerMeasurement(GenPowerMeasurement, S2MessageComponent["PowerMeasurement"]):
model_config = GenPowerMeasurement.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/power_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

from s2python.generated.gen_s2 import PowerRange as GenPowerRange
from s2python.validate_values_mixin import (
S2Message,
S2MessageComponent,
catch_and_convert_exceptions,
)


@catch_and_convert_exceptions
class PowerRange(GenPowerRange, S2Message["PowerRange"]):
class PowerRange(GenPowerRange, S2MessageComponent["PowerRange"]):
model_config = GenPowerRange.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/power_value.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from s2python.generated.gen_s2 import PowerValue as GenPowerValue
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class PowerValue(GenPowerValue, S2Message["PowerValue"]):
class PowerValue(GenPowerValue, S2MessageComponent["PowerValue"]):
model_config = GenPowerValue.model_config
model_config["validate_assignment"] = True
4 changes: 2 additions & 2 deletions src/s2python/common/reception_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from s2python.generated.gen_s2 import ReceptionStatus as GenReceptionStatus
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class ReceptionStatus(GenReceptionStatus, S2Message["ReceptionStatus"]):
class ReceptionStatus(GenReceptionStatus, S2MessageComponent["ReceptionStatus"]):
model_config = GenReceptionStatus.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/resource_manager_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
)
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class ResourceManagerDetails(GenResourceManagerDetails, S2Message["ResourceManagerDetails"]):
class ResourceManagerDetails(GenResourceManagerDetails, S2MessageComponent["ResourceManagerDetails"]):
model_config = GenResourceManagerDetails.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/revoke_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from s2python.generated.gen_s2 import RevokeObject as GenRevokeObject
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class RevokeObject(GenRevokeObject, S2Message["RevokeObject"]):
class RevokeObject(GenRevokeObject, S2MessageComponent["RevokeObject"]):
model_config = GenRevokeObject.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/role.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from s2python.generated.gen_s2 import Role as GenRole
from s2python.validate_values_mixin import (
S2Message,
S2MessageComponent,
catch_and_convert_exceptions,
)


@catch_and_convert_exceptions
class Role(GenRole, S2Message["Role"]):
class Role(GenRole, S2MessageComponent["Role"]):
model_config = GenRole.model_config
model_config["validate_assignment"] = True
4 changes: 2 additions & 2 deletions src/s2python/common/select_control_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from s2python.generated.gen_s2 import SelectControlType as GenSelectControlType
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class SelectControlType(GenSelectControlType, S2Message["SelectControlType"]):
class SelectControlType(GenSelectControlType, S2MessageComponent["SelectControlType"]):
model_config = GenSelectControlType.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/session_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from s2python.generated.gen_s2 import SessionRequest as GenSessionRequest
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class SessionRequest(GenSessionRequest, S2Message["SessionRequest"]):
class SessionRequest(GenSessionRequest, S2MessageComponent["SessionRequest"]):
model_config = GenSessionRequest.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from s2python.common.duration import Duration
from s2python.generated.gen_s2 import Timer as GenTimer
from s2python.validate_values_mixin import (
S2Message,
S2MessageComponent,
catch_and_convert_exceptions,
)


@catch_and_convert_exceptions
class Timer(GenTimer, S2Message["Timer"]):
class Timer(GenTimer, S2MessageComponent["Timer"]):
model_config = GenTimer.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/common/transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from s2python.common.duration import Duration
from s2python.generated.gen_s2 import Transition as GenTransition
from s2python.validate_values_mixin import (
S2Message,
S2MessageComponent,
catch_and_convert_exceptions,
)


@catch_and_convert_exceptions
class Transition(GenTransition, S2Message["Transition"]):
class Transition(GenTransition, S2MessageComponent["Transition"]):
model_config = GenTransition.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/frbc/frbc_actuator_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
FRBCActuatorDescription as GenFRBCActuatorDescription,
)
from s2python.validate_values_mixin import (
S2Message,
S2MessageComponent,
catch_and_convert_exceptions,
)


@catch_and_convert_exceptions
class FRBCActuatorDescription(GenFRBCActuatorDescription, S2Message["FRBCActuatorDescription"]):
class FRBCActuatorDescription(GenFRBCActuatorDescription, S2MessageComponent["FRBCActuatorDescription"]):
model_config = GenFRBCActuatorDescription.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/frbc/frbc_actuator_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from s2python.generated.gen_s2 import FRBCActuatorStatus as GenFRBCActuatorStatus
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class FRBCActuatorStatus(GenFRBCActuatorStatus, S2Message["FRBCActuatorStatus"]):
class FRBCActuatorStatus(GenFRBCActuatorStatus, S2MessageComponent["FRBCActuatorStatus"]):
model_config = GenFRBCActuatorStatus.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/frbc/frbc_fill_level_target_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
)
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class FRBCFillLevelTargetProfile(GenFRBCFillLevelTargetProfile, S2Message["FRBCFillLevelTargetProfile"]):
class FRBCFillLevelTargetProfile(GenFRBCFillLevelTargetProfile, S2MessageComponent["FRBCFillLevelTargetProfile"]):
model_config = GenFRBCFillLevelTargetProfile.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/frbc/frbc_fill_level_target_profile_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from s2python.generated.gen_s2 import (
FRBCFillLevelTargetProfileElement as GenFRBCFillLevelTargetProfileElement,
)
from s2python.validate_values_mixin import catch_and_convert_exceptions, S2Message
from s2python.validate_values_mixin import catch_and_convert_exceptions, S2MessageComponent


@catch_and_convert_exceptions
class FRBCFillLevelTargetProfileElement(
GenFRBCFillLevelTargetProfileElement, S2Message["FRBCFillLevelTargetProfileElement"]
GenFRBCFillLevelTargetProfileElement, S2MessageComponent["FRBCFillLevelTargetProfileElement"]
):
model_config = GenFRBCFillLevelTargetProfileElement.model_config
model_config["validate_assignment"] = True
Expand Down
4 changes: 2 additions & 2 deletions src/s2python/frbc/frbc_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from s2python.generated.gen_s2 import FRBCInstruction as GenFRBCInstruction
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class FRBCInstruction(GenFRBCInstruction, S2Message["FRBCInstruction"]):
class FRBCInstruction(GenFRBCInstruction, S2MessageComponent["FRBCInstruction"]):
model_config = GenFRBCInstruction.model_config
model_config["validate_assignment"] = True

Expand Down
4 changes: 2 additions & 2 deletions src/s2python/frbc/frbc_leakage_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from s2python.generated.gen_s2 import FRBCLeakageBehaviour as GenFRBCLeakageBehaviour
from s2python.validate_values_mixin import (
catch_and_convert_exceptions,
S2Message,
S2MessageComponent,
)


@catch_and_convert_exceptions
class FRBCLeakageBehaviour(GenFRBCLeakageBehaviour, S2Message["FRBCLeakageBehaviour"]):
class FRBCLeakageBehaviour(GenFRBCLeakageBehaviour, S2MessageComponent["FRBCLeakageBehaviour"]):
model_config = GenFRBCLeakageBehaviour.model_config
model_config["validate_assignment"] = True

Expand Down
Loading

0 comments on commit cb08e31

Please sign in to comment.