Skip to content

Commit

Permalink
change .dict to .model_dump
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Aug 21, 2024
1 parent 2ae4624 commit e8e99e8
Show file tree
Hide file tree
Showing 52 changed files with 205 additions and 192 deletions.
8 changes: 4 additions & 4 deletions antarest/core/tasks/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _launch_task(
message=custom_event_messages.start
if custom_event_messages is not None
else f"Task {task.id} added",
).dict(),
).model_dump(),
permissions=PermissionInfo(owner=request_params.user.impersonator),
)
)
Expand Down Expand Up @@ -349,7 +349,7 @@ def _run_task(
message=custom_event_messages.running
if custom_event_messages is not None
else f"Task {task_id} is running",
).dict(),
).model_dump(),
permissions=PermissionInfo(public_mode=PublicMode.READ),
channel=EventChannelDirectory.TASK + task_id,
)
Expand Down Expand Up @@ -395,7 +395,7 @@ def _run_task(
if custom_event_messages is not None
else f"Task {task_id} {event_msg}"
),
).dict(),
).model_dump(),
permissions=PermissionInfo(public_mode=PublicMode.READ),
channel=EventChannelDirectory.TASK + task_id,
)
Expand All @@ -420,7 +420,7 @@ def _run_task(
self.event_bus.push(
Event(
type=EventType.TASK_FAILED,
payload=TaskEventPayload(id=task_id, message=message).dict(),
payload=TaskEventPayload(id=task_id, message=message).model_dump(),
permissions=PermissionInfo(public_mode=PublicMode.READ),
channel=EventChannelDirectory.TASK + task_id,
)
Expand Down
2 changes: 1 addition & 1 deletion antarest/eventbus/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def configure_websockets(application: FastAPI, config: Config, event_bus: IEvent
manager = ConnectionManager()

async def send_event_to_ws(event: Event) -> None:
event_data = event.dict()
event_data = event.model_dump()
del event_data["permissions"]
del event_data["channel"]
await manager.broadcast(json.dumps(event_data), event.permissions, event.channel)
Expand Down
2 changes: 1 addition & 1 deletion antarest/launcher/adapters/abstractlauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ def update_log(log_line: str) -> None:
channel=EventChannelDirectory.JOB_STATUS + job_id,
)
)
self.cache.put(f"Launch_Progress_{job_id}", launch_progress_dto.dict())
self.cache.put(f"Launch_Progress_{job_id}", launch_progress_dto.model_dump())

return update_log
6 changes: 3 additions & 3 deletions antarest/launcher/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def update(
self.event_bus.push(
Event(
type=EventType.STUDY_JOB_COMPLETED if final_status else EventType.STUDY_JOB_STATUS_UPDATE,
payload=job_result.to_dto().dict(),
payload=job_result.to_dto().model_dump(),
permissions=PermissionInfo(public_mode=PublicMode.READ),
channel=EventChannelDirectory.JOB_STATUS + job_result.id,
)
Expand Down Expand Up @@ -252,7 +252,7 @@ def run_study(
self.event_bus.push(
Event(
type=EventType.STUDY_JOB_STARTED,
payload=job_status.to_dto().dict(),
payload=job_status.to_dto().model_dump(),
permissions=PermissionInfo.from_study(study_info),
)
)
Expand Down Expand Up @@ -293,7 +293,7 @@ def kill_job(self, job_id: str, params: RequestParameters) -> JobResult:
self.event_bus.push(
Event(
type=EventType.STUDY_JOB_CANCELLED,
payload=job_status.to_dto().dict(),
payload=job_status.to_dto().model_dump(),
permissions=PermissionInfo.from_study(study),
channel=EventChannelDirectory.JOB_STATUS + job_result.id,
)
Expand Down
16 changes: 10 additions & 6 deletions antarest/study/business/area_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class UpdateAreaUi(BaseModel, extra="forbid", allow_population_by_field_name=Tru
... }
>>> model = UpdateAreaUi(**obj)
>>> pprint(model.dict(by_alias=True), width=80)
>>> pprint(model.model_dump(by_alias=True), width=80)
{'colorRgb': [230, 108, 44],
'layerColor': {0: '230, 108, 44',
4: '230, 108, 44',
Expand Down Expand Up @@ -215,10 +215,14 @@ def from_model(
obj = {
"average_unsupplied_energy_cost": average_unsupplied_energy_cost,
"average_spilled_energy_cost": average_spilled_energy_cost,
**area_folder.optimization.filtering.dict(by_alias=False),
**area_folder.optimization.nodal_optimization.dict(by_alias=False),
**area_folder.optimization.filtering.model_dump(by_alias=False),
**area_folder.optimization.nodal_optimization.model_dump(by_alias=False),
# adequacy_patch is only available if study version >= 830.
**(area_folder.adequacy_patch.adequacy_patch.dict(by_alias=False) if area_folder.adequacy_patch else {}),
**(
area_folder.adequacy_patch.adequacy_patch.model_dump(by_alias=False)
if area_folder.adequacy_patch
else {}
),
}
return cls(**obj)

Expand Down Expand Up @@ -347,7 +351,7 @@ def update_areas_props(
for area_id, update_area in update_areas_by_ids.items():
# Update the area properties.
old_area = old_areas_by_ids[area_id]
new_area = old_area.copy(update=update_area.dict(by_alias=False, exclude_none=True))
new_area = old_area.copy(update=update_area.model_dump(by_alias=False, exclude_none=True))
new_areas_by_ids[area_id] = new_area

# Convert the DTO to a configuration object and update the configuration file.
Expand Down Expand Up @@ -728,7 +732,7 @@ def update_thermal_cluster_metadata(
id=area_id,
name=file_study.config.areas[area_id].name,
type=AreaType.AREA,
metadata=patch.areas.get(area_id, PatchArea()).dict(),
metadata=patch.areas.get(area_id, PatchArea()).model_dump(),
thermals=self._get_clusters(file_study, area_id, patch),
set=None,
)
Expand Down
18 changes: 9 additions & 9 deletions antarest/study/business/areas/renewable_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def validate_name(cls, name: t.Optional[str]) -> str:
return name

def to_config(self, study_version: t.Union[str, int]) -> RenewableConfigType:
values = self.dict(by_alias=False, exclude_none=True)
values = self.model_dump(by_alias=False, exclude_none=True)
return create_renewable_config(study_version=study_version, **values)


Expand Down Expand Up @@ -99,7 +99,7 @@ def create_renewable_output(
config: t.Mapping[str, t.Any],
) -> "RenewableClusterOutput":
obj = create_renewable_config(study_version=study_version, **config, id=cluster_id)
kwargs = obj.dict(by_alias=False)
kwargs = obj.model_dump(by_alias=False)
return RenewableClusterOutput(**kwargs)


Expand Down Expand Up @@ -206,7 +206,7 @@ def _make_create_cluster_cmd(self, area_id: str, cluster: RenewableConfigType) -
command = CreateRenewablesCluster(
area_id=area_id,
cluster_name=cluster.id,
parameters=cluster.dict(by_alias=True, exclude={"id"}),
parameters=cluster.model_dump(by_alias=True, exclude={"id"}),
command_context=self.storage_service.variant_study_service.command_factory.command_context,
)
return command
Expand Down Expand Up @@ -269,7 +269,7 @@ def update_cluster(
old_config = create_renewable_config(study_version, **values)

# use Python values to synchronize Config and Form values
new_values = cluster_data.dict(by_alias=False, exclude_none=True)
new_values = cluster_data.model_dump(by_alias=False, exclude_none=True)
new_config = old_config.copy(exclude={"id"}, update=new_values)
new_data = json.loads(new_config.model_dump_json(by_alias=True, exclude={"id"}))

Expand All @@ -288,7 +288,7 @@ def update_cluster(
]
execute_or_add_commands(study, file_study, commands, self.storage_service)

values = new_config.dict(by_alias=False)
values = new_config.model_dump(by_alias=False)
return RenewableClusterOutput(**values, id=cluster_id)

def delete_clusters(self, study: Study, area_id: str, cluster_ids: t.Sequence[str]) -> None:
Expand Down Expand Up @@ -340,7 +340,7 @@ def duplicate_cluster(
# Cluster duplication
current_cluster = self.get_cluster(study, area_id, source_id)
current_cluster.name = new_cluster_name
creation_form = RenewableClusterCreation(**current_cluster.dict(by_alias=False, exclude={"id"}))
creation_form = RenewableClusterCreation(**current_cluster.model_dump(by_alias=False, exclude={"id"}))
new_config = creation_form.to_config(study.version)
create_cluster_cmd = self._make_create_cluster_cmd(area_id, new_config)

Expand All @@ -358,7 +358,7 @@ def duplicate_cluster(

execute_or_add_commands(study, self._get_file_study(study), commands, self.storage_service)

return RenewableClusterOutput(**new_config.dict(by_alias=False))
return RenewableClusterOutput(**new_config.model_dump(by_alias=False))

def update_renewables_props(
self,
Expand All @@ -375,12 +375,12 @@ def update_renewables_props(
for renewable_id, update_cluster in update_renewables_by_ids.items():
# Update the renewable cluster properties.
old_cluster = old_renewables_by_ids[renewable_id]
new_cluster = old_cluster.copy(update=update_cluster.dict(by_alias=False, exclude_none=True))
new_cluster = old_cluster.copy(update=update_cluster.model_dump(by_alias=False, exclude_none=True))
new_renewables_by_areas[area_id][renewable_id] = new_cluster

# Convert the DTO to a configuration object and update the configuration file.
properties = create_renewable_config(
study.version, **new_cluster.dict(by_alias=False, exclude_none=True)
study.version, **new_cluster.model_dump(by_alias=False, exclude_none=True)
)
path = _CLUSTER_PATH.format(area_id=area_id, cluster_id=renewable_id)
cmd = UpdateConfig(
Expand Down
16 changes: 8 additions & 8 deletions antarest/study/business/areas/st_storage_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def validate_name(cls, name: t.Optional[str]) -> str:

# noinspection PyUnusedLocal
def to_config(self, study_version: t.Union[str, int]) -> STStorageConfigType:
values = self.dict(by_alias=False, exclude_none=True)
values = self.model_dump(by_alias=False, exclude_none=True)
return create_st_storage_config(study_version=study_version, **values)


Expand Down Expand Up @@ -237,7 +237,7 @@ def create_storage_output(
config: t.Mapping[str, t.Any],
) -> "STStorageOutput":
obj = create_st_storage_config(study_version=study_version, **config, id=cluster_id)
kwargs = obj.dict(by_alias=False)
kwargs = obj.model_dump(by_alias=False)
return STStorageOutput(**kwargs)


Expand Down Expand Up @@ -381,12 +381,12 @@ def update_storages_props(
for storage_id, update_cluster in update_storages_by_ids.items():
# Update the storage cluster properties.
old_cluster = old_storages_by_ids[storage_id]
new_cluster = old_cluster.copy(update=update_cluster.dict(by_alias=False, exclude_none=True))
new_cluster = old_cluster.copy(update=update_cluster.model_dump(by_alias=False, exclude_none=True))
new_storages_by_areas[area_id][storage_id] = new_cluster

# Convert the DTO to a configuration object and update the configuration file.
properties = create_st_storage_config(
study.version, **new_cluster.dict(by_alias=False, exclude_none=True)
study.version, **new_cluster.model_dump(by_alias=False, exclude_none=True)
)
path = _STORAGE_LIST_PATH.format(area_id=area_id, storage_id=storage_id)
cmd = UpdateConfig(
Expand Down Expand Up @@ -460,7 +460,7 @@ def update_storage(
old_config = create_st_storage_config(study_version, **values)

# use Python values to synchronize Config and Form values
new_values = form.dict(by_alias=False, exclude_none=True)
new_values = form.model_dump(by_alias=False, exclude_none=True)
new_config = old_config.copy(exclude={"id"}, update=new_values)
new_data = json.loads(new_config.model_dump_json(by_alias=True, exclude={"id"}))

Expand All @@ -480,7 +480,7 @@ def update_storage(
]
execute_or_add_commands(study, file_study, commands, self.storage_service)

values = new_config.dict(by_alias=False)
values = new_config.model_dump(by_alias=False)
return STStorageOutput(**values, id=storage_id)

def delete_storages(
Expand Down Expand Up @@ -542,7 +542,7 @@ def duplicate_cluster(self, study: Study, area_id: str, source_id: str, new_clus
# We should remove the field 'enabled' for studies before v8.8 as it didn't exist
if int(study.version) < 880:
fields_to_exclude.add("enabled")
creation_form = STStorageCreation(**current_cluster.dict(by_alias=False, exclude=fields_to_exclude))
creation_form = STStorageCreation(**current_cluster.model_dump(by_alias=False, exclude=fields_to_exclude))

new_config = creation_form.to_config(study.version)
create_cluster_cmd = self._make_create_cluster_cmd(area_id, new_config)
Expand Down Expand Up @@ -571,7 +571,7 @@ def duplicate_cluster(self, study: Study, area_id: str, source_id: str, new_clus

execute_or_add_commands(study, self._get_file_study(study), commands, self.storage_service)

return STStorageOutput(**new_config.dict(by_alias=False))
return STStorageOutput(**new_config.model_dump(by_alias=False))

def get_matrix(
self,
Expand Down
20 changes: 11 additions & 9 deletions antarest/study/business/areas/thermal_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def validate_name(cls, name: t.Optional[str]) -> str:
return name

def to_config(self, study_version: t.Union[str, int]) -> ThermalConfigType:
values = self.dict(by_alias=False, exclude_none=True)
values = self.model_dump(by_alias=False, exclude_none=True)
return create_thermal_config(study_version=study_version, **values)


Expand Down Expand Up @@ -109,7 +109,7 @@ def create_thermal_output(
config: t.Mapping[str, t.Any],
) -> "ThermalClusterOutput":
obj = create_thermal_config(study_version=study_version, **config, id=cluster_id)
kwargs = obj.dict(by_alias=False)
kwargs = obj.model_dump(by_alias=False)
return ThermalClusterOutput(**kwargs)


Expand Down Expand Up @@ -240,11 +240,13 @@ def update_thermals_props(
for thermal_id, update_cluster in update_thermals_by_ids.items():
# Update the thermal cluster properties.
old_cluster = old_thermals_by_ids[thermal_id]
new_cluster = old_cluster.copy(update=update_cluster.dict(by_alias=False, exclude_none=True))
new_cluster = old_cluster.copy(update=update_cluster.model_dump(by_alias=False, exclude_none=True))
new_thermals_by_areas[area_id][thermal_id] = new_cluster

# Convert the DTO to a configuration object and update the configuration file.
properties = create_thermal_config(study.version, **new_cluster.dict(by_alias=False, exclude_none=True))
properties = create_thermal_config(
study.version, **new_cluster.model_dump(by_alias=False, exclude_none=True)
)
path = _CLUSTER_PATH.format(area_id=area_id, cluster_id=thermal_id)
cmd = UpdateConfig(
target=path,
Expand Down Expand Up @@ -293,7 +295,7 @@ def _make_create_cluster_cmd(self, area_id: str, cluster: ThermalConfigType) ->
command = CreateCluster(
area_id=area_id,
cluster_name=cluster.id,
parameters=cluster.dict(by_alias=True, exclude={"id"}),
parameters=cluster.model_dump(by_alias=True, exclude={"id"}),
command_context=self.storage_service.variant_study_service.command_factory.command_context,
)
return command
Expand Down Expand Up @@ -334,7 +336,7 @@ def update_cluster(
old_config = create_thermal_config(study_version, **values)

# Use Python values to synchronize Config and Form values
new_values = cluster_data.dict(by_alias=False, exclude_none=True)
new_values = cluster_data.model_dump(by_alias=False, exclude_none=True)
new_config = old_config.copy(exclude={"id"}, update=new_values)
new_data = json.loads(new_config.model_dump_json(by_alias=True, exclude={"id"}))

Expand All @@ -353,7 +355,7 @@ def update_cluster(
]
execute_or_add_commands(study, file_study, commands, self.storage_service)

values = new_config.dict(by_alias=False)
values = new_config.model_dump(by_alias=False)
return ThermalClusterOutput(**values, id=cluster_id)

def delete_clusters(self, study: Study, area_id: str, cluster_ids: t.Sequence[str]) -> None:
Expand Down Expand Up @@ -406,7 +408,7 @@ def duplicate_cluster(
# Cluster duplication
source_cluster = self.get_cluster(study, area_id, source_id)
source_cluster.name = new_cluster_name
creation_form = ThermalClusterCreation(**source_cluster.dict(by_alias=False, exclude={"id"}))
creation_form = ThermalClusterCreation(**source_cluster.model_dump(by_alias=False, exclude={"id"}))
new_config = creation_form.to_config(study.version)
create_cluster_cmd = self._make_create_cluster_cmd(area_id, new_config)

Expand Down Expand Up @@ -439,7 +441,7 @@ def duplicate_cluster(

execute_or_add_commands(study, self._get_file_study(study), commands, self.storage_service)

return ThermalClusterOutput(**new_config.dict(by_alias=False))
return ThermalClusterOutput(**new_config.model_dump(by_alias=False))

def validate_series(self, study: Study, area_id: str, cluster_id: str) -> bool:
lower_cluster_id = cluster_id.lower()
Expand Down
6 changes: 3 additions & 3 deletions antarest/study/business/link_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def get_all_links_props(self, study: RawStudy) -> t.Mapping[t.Tuple[str, str], L
for area2_id, properties_cfg in property_map.items():
area1_id, area2_id = sorted([area1_id, area2_id])
properties = LinkProperties(**properties_cfg)
links_by_ids[(area1_id, area2_id)] = LinkOutput(**properties.dict(by_alias=False))
links_by_ids[(area1_id, area2_id)] = LinkOutput(**properties.model_dump(by_alias=False))

return links_by_ids

Expand All @@ -125,11 +125,11 @@ def update_links_props(
for (area1, area2), update_link_dto in update_links_by_ids.items():
# Update the link properties.
old_link_dto = old_links_by_ids[(area1, area2)]
new_link_dto = old_link_dto.copy(update=update_link_dto.dict(by_alias=False, exclude_none=True))
new_link_dto = old_link_dto.copy(update=update_link_dto.model_dump(by_alias=False, exclude_none=True))
new_links_by_ids[(area1, area2)] = new_link_dto

# Convert the DTO to a configuration object and update the configuration file.
properties = LinkProperties(**new_link_dto.dict(by_alias=False))
properties = LinkProperties(**new_link_dto.model_dump(by_alias=False))
path = f"{_ALL_LINKS_PATH}/{area1}/properties/{area2}"
cmd = UpdateConfig(
target=path,
Expand Down
Loading

0 comments on commit e8e99e8

Please sign in to comment.