Skip to content
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

Revert "migrate from pydantic v1.0 to v2.0" #60

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ install_requires =
importlib-metadata; python_version<"3.8"
aiohttp
pandas
pydantic<=2.1.1
pydantic>=1.10.8,<2.0


[options.packages.find]
where = src
Expand Down
18 changes: 10 additions & 8 deletions src/flexmeasures_client/s2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def wrap(*args, **kwargs):
self = args[0]

# TODO: implement function __hash__ in ID that returns
# the value of root, this way we would be able to use
# the value of __root__, this way we would be able to use
# the ID as key directly
self.incoming_messages[
get_message_id(incoming_message)
Expand All @@ -63,7 +63,7 @@ def wrap(*args, **kwargs):
status=ReceptionStatusValues.INVALID_DATA,
) # TODO: Discuss status

message_type = schema.model_fields.get("message_type").default
message_type = schema.__fields__.get("message_type").default
setattr(wrap, "_tag", Tag(message_type))

return wrap
Expand Down Expand Up @@ -178,7 +178,7 @@ def handle_message(self, message: pydantic.BaseModel | str | Dict) -> dict:
"""

if isinstance(message, pydantic.BaseModel):
message = json.loads(message.model_dump_json())
message = json.loads(message.json())

if isinstance(message, str):
message = json.loads(message)
Expand All @@ -200,8 +200,10 @@ def handle_response_status(self, message: ReceptionStatus):
callback_store = None

# save acknowledgement status code
# TODO: implement function __hash__ in ID that returns the value of root
self.outgoing_messages_status[message.subject_message_id.root] = message.status
# TODO: implement function __hash__ in ID that returns the value of __root__
self.outgoing_messages_status[
message.subject_message_id.__root__
] = message.status

# choose which callback to call, depending on the ReceptionStatus value
if message.status == ReceptionStatusValues.OK:
Expand All @@ -210,20 +212,20 @@ def handle_response_status(self, message: ReceptionStatus):
callback_store = self.failure_callbacks

# pop callback from callback_store and run it, if there exists one
if callback := callback_store.pop(message.subject_message_id.root, None):
if callback := callback_store.pop(message.subject_message_id.__root__, None):
callback()

# delete success callback related to this message
if callback is None and (message.status != ReceptionStatusValues.OK):
self.success_callbacks.pop(message.subject_message_id.root, None)
self.success_callbacks.pop(message.subject_message_id.__root__, None)

@register(RevokeObject)
def handle_revoke_object(self, message: RevokeObject):
"""
Stores the revoked object ID into the objects_revoked list
"""

self.objects_revoked.append(message.object_id.root)
self.objects_revoked.append(message.object_id.__root__)

return ReceptionStatus(
subject_message_id=message.message_id, status=ReceptionStatusValues.OK
Expand Down
4 changes: 2 additions & 2 deletions src/flexmeasures_client/s2/cem.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def handle_message(self, message: Dict | pydantic.BaseModel | str):
response = None

if isinstance(message, pydantic.BaseModel):
message = json.loads(message.model_dump_json())
message = json.loads(message.json())

if isinstance(message, str):
message = json.loads(message)
Expand Down Expand Up @@ -158,7 +158,7 @@ async def get_message(self) -> str:

# Pending for pydantic V2 to implement model.model_dump(mode="json") in
# PR #1409 (https://github.com/pydantic/pydantic/issues/1409)
message = json.loads(message.model_dump_json())
message = json.loads(message.json())

return message

Expand Down
10 changes: 5 additions & 5 deletions src/flexmeasures_client/s2/control_types/FRBC/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self, max_size: int = 100) -> None:
def handle_system_description(
self, message: FRBCSystemDescription
) -> pydantic.BaseModel:
system_description_id = message.message_id.root
system_description_id = message.message_id.__root__

# store system_description message for later
self._system_description_history[system_description_id] = message
Expand All @@ -77,7 +77,7 @@ async def send_actuator_status(self, status: FRBCActuatorStatus):

@register(FRBCStorageStatus)
def handle_storage_status(self, message: FRBCStorageStatus) -> pydantic.BaseModel:
message_id = message.message_id.root
message_id = message.message_id.__root__

self._storage_status_history[message_id] = message

Expand All @@ -87,7 +87,7 @@ def handle_storage_status(self, message: FRBCStorageStatus) -> pydantic.BaseMode

@register(FRBCActuatorStatus)
def handle_actuator_status(self, message: FRBCActuatorStatus) -> pydantic.BaseModel:
message_id = message.message_id.root
message_id = message.message_id.__root__

self._actuator_status_history[message_id] = message

Expand Down Expand Up @@ -129,8 +129,8 @@ async def trigger_schedule(self, system_description_id: str):
instruction = FRBCInstruction(
message_id=get_unique_id(),
id=get_unique_id(),
actuator_id=actuator.id.root,
operation_mode=actuator.operation_modes[0].id.root,
actuator_id=actuator.id.__root__,
operation_mode=actuator.operation_modes[0].id.__root__,
operation_mode_factor=0.5,
execution_time=system_description.valid_from,
abnormal_condition=False,
Expand Down
4 changes: 2 additions & 2 deletions src/flexmeasures_client/s2/control_types/FRBC/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def fm_schedule_to_instructions(
instruction = FRBCInstruction(
message_id=get_unique_id(),
id=get_unique_id(),
actuator_id=actuator.id.root,
operation_mode=operation_mode.id.root,
actuator_id=actuator.id.__root__,
operation_mode=operation_mode.id.__root__,
operation_mode_factor=operation_mode_factor,
execution_time=start,
abnormal_condition=False,
Expand Down
2 changes: 1 addition & 1 deletion src/flexmeasures_client/s2/control_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, max_size: int = 100) -> None:

@register(InstructionStatusUpdate)
def handle_instruction_status_update(self, message: InstructionStatusUpdate):
instruction_id: str = cast(str, message.instruction_id.root)
instruction_id: str = cast(str, message.instruction_id.__root__)

self._instruction_status_history[instruction_id] = message.status_type

Expand Down
80 changes: 49 additions & 31 deletions src/flexmeasures_client/s2/python_s2_protocol/FRBC/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from __future__ import annotations

from datetime import datetime
from typing import List, Literal, Optional
from typing import List, Optional

from pydantic import BaseModel, ConfigDict, Extra, Field, constr
from pydantic import BaseModel, Extra, Field, constr

from flexmeasures_client.s2.python_s2_protocol.common.schemas import (
ID,
Expand All @@ -22,8 +22,11 @@


class FRBCActuatorStatus(BaseModel):
model_config = ConfigDict(validate_assigment=True, extra="forbid")
message_type: Literal["FRBC.ActuatorStatus"] = Field(default="FRBC.ActuatorStatus")
class Config:
extra = Extra.forbid
validate_assignment = True

message_type: str = Field("FRBC.ActuatorStatus", const=True)
message_id: ID = Field(..., description="ID of this message")
actuator_id: ID = Field(
..., description="ID of the actuator this messages refers to"
Expand All @@ -46,25 +49,29 @@ class FRBCActuatorStatus(BaseModel):


class FRBCFillLevelTargetProfile(BaseModel):
model_config = ConfigDict(validate_assigment=True, extra="forbid")
message_type: Literal["FRBC.FillLevelTargetProfile"] = Field(
default="FRBC.FillLevelTargetProfile"
)
class Config:
extra = Extra.forbid
validate_assignment = True

message_type: str = Field("FRBC.FillLevelTargetProfile", const=True)
message_id: ID = Field(..., description="ID of this message")
start_time: datetime = Field(
..., description="Time at which the FRBC.FillLevelTargetProfile starts."
)
elements: List[FRBCFillLevelTargetProfileElement] = Field(
...,
description="List of different fill levels that have to be targeted within a given duration. There shall be at least one element. Elements must be placed in chronological order.",
max_length=288,
min_length=1,
max_items=288,
min_items=1,
)


class FRBCInstruction(BaseModel):
model_config = ConfigDict(validate_assigment=True, extra="forbid")
message_type: Literal["FRBC.Instruction"] = Field(default="FRBC.Instruction")
class Config:
extra = Extra.forbid
validate_assignment = True

message_type: str = Field("FRBC.Instruction", const=True)
message_id: ID = Field(..., description="ID of this message")
id: ID = Field(
...,
Expand All @@ -90,10 +97,11 @@ class FRBCInstruction(BaseModel):


class FRBCLeakageBehaviour(BaseModel):
model_config = ConfigDict(validate_assigment=True, extra="forbid")
message_type: Literal["FRBC.LeakageBehaviour"] = Field(
default="FRBC.LeakageBehaviour"
)
class Config:
extra = Extra.forbid
validate_assignment = True

message_type: str = Field("FRBC.LeakageBehaviour", const=True)
message_id: ID = Field(..., description="ID of this message")
valid_from: datetime = Field(
...,
Expand All @@ -102,39 +110,46 @@ class FRBCLeakageBehaviour(BaseModel):
elements: List[FRBCLeakageBehaviourElement] = Field(
...,
description="List of elements that model the leakage behaviour of the buffer. The fill_level_ranges of the elements must be contiguous.",
max_length=288,
min_length=1,
max_items=288,
min_items=1,
)


class FRBCStorageStatus(BaseModel):
model_config = ConfigDict(validate_assigment=True, extra="forbid")
message_type: Literal["FRBC.StorageStatus"] = Field(default="FRBC.StorageStatus")
class Config:
extra = Extra.forbid
validate_assignment = True

message_type: str = Field("FRBC.StorageStatus", const=True)
message_id: ID = Field(..., description="ID of this message")
present_fill_level: float = Field(
..., description="Present fill level of the Storage"
)


class FRBCSystemDescription(BaseModel):
model_config = ConfigDict(validate_assigment=True, extra="forbid")
message_type: Literal["FRBC.SystemDescription"] = Field(
default="FRBC.SystemDescription"
)
class Config:
extra = Extra.forbid
validate_assignment = True

message_type: str = Field("FRBC.SystemDescription", const=True)
message_id: ID = Field(..., description="ID of this message")
valid_from: datetime = Field(
...,
description="Moment this FRBC.SystemDescription starts to be valid. If the system description is immediately valid, the DateTimeStamp should be now or in the past.",
)
actuators: List[FRBCActuatorDescription] = Field(
..., description="Details of all Actuators.", max_length=10, min_length=1
..., description="Details of all Actuators.", max_items=10, min_items=1
)
storage: FRBCStorageDescription = Field(..., description="Details of the storage.")


class FRBCTimerStatus(BaseModel):
model_config = ConfigDict(validate_assigment=True, extra="forbid")
message_type: Literal["FRBC.TimerStatus"] = Field(default="FRBC.TimerStatus")
class Config:
extra = Extra.forbid
validate_assignment = True

message_type: str = Field("FRBC.TimerStatus", const=True)
message_id: ID = Field(..., description="ID of this message")
timer_id: ID = Field(..., description="The ID of the timer this message refers to")
actuator_id: ID = Field(
Expand All @@ -147,15 +162,18 @@ class FRBCTimerStatus(BaseModel):


class FRBCUsageForecast(BaseModel):
model_config = ConfigDict(validate_assigment=True, extra="forbid")
message_type: Literal["FRBC.UsageForecast"] = Field(default="FRBC.UsageForecast")
class Config:
extra = Extra.forbid
validate_assignment = True

message_type: str = Field("FRBC.UsageForecast", const=True)
message_id: ID = Field(..., description="ID of this message")
start_time: datetime = Field(
..., description="Time at which the FRBC.UsageForecast starts."
)
elements: List[FRBCUsageForecastElement] = Field(
...,
description="Further elements that model the profile. There shall be at least one element. Elements must be placed in chronological order.",
max_length=288,
min_length=1,
max_items=288,
min_items=1,
)
Loading