diff --git a/src/antares/craft/model/area.py b/src/antares/craft/model/area.py index 15e687aa..a2ba0401 100644 --- a/src/antares/craft/model/area.py +++ b/src/antares/craft/model/area.py @@ -17,7 +17,7 @@ """ from types import MappingProxyType -from typing import Any, Dict, List, Mapping, Optional, Set +from typing import Any, Mapping, Optional, cast import pandas as pd @@ -26,13 +26,6 @@ from antares.craft.model.renewable import RenewableCluster, RenewableClusterProperties from antares.craft.model.st_storage import STStorage, STStorageProperties from antares.craft.model.thermal import ThermalCluster, ThermalClusterProperties -from antares.craft.service.base_services import ( - BaseAreaService, - BaseHydroService, - BaseRenewableService, - BaseShortTermStorageService, - BaseThermalService, -) from antares.craft.tools.alias_generators import to_space from antares.craft.tools.all_optional_meta import all_optional_model from antares.craft.tools.contents_tool import EnumIgnoreCase, transform_name_to_id @@ -62,14 +55,14 @@ class DefaultAreaProperties(BaseModel, extra="forbid", populate_by_name=True): non_dispatch_power: bool = True dispatch_hydro_power: bool = True other_dispatch_power: bool = True - filter_synthesis: Set[FilterOption] = { + filter_synthesis: set[FilterOption] = { FilterOption.HOURLY, FilterOption.DAILY, FilterOption.WEEKLY, FilterOption.MONTHLY, FilterOption.ANNUAL, } - filter_by_year: Set[FilterOption] = { + filter_by_year: set[FilterOption] = { FilterOption.HOURLY, FilterOption.DAILY, FilterOption.WEEKLY, @@ -128,11 +121,11 @@ class AreaUi(BaseModel, extra="forbid", populate_by_name=True, alias_generator=t layer: Optional[int] = None x: Optional[int] = None y: Optional[int] = None - color_rgb: Optional[List[int]] = None + color_rgb: Optional[list[int]] = None - layer_x: Optional[Dict[int, int]] = None - layer_y: Optional[Dict[int, int]] = None - layer_color: Optional[Dict[int, str]] = None + layer_x: Optional[dict[int, int]] = None + layer_y: Optional[dict[int, int]] = None + layer_color: Optional[dict[int, str]] = None class AreaUiLocal(BaseModel): @@ -158,7 +151,7 @@ def __init__( @computed_field # type: ignore[misc] @property - def ui(self) -> Dict[str, Optional[int]]: + def ui(self) -> dict[str, Optional[int]]: return dict( x=self._x, y=self._y, @@ -170,17 +163,17 @@ def ui(self) -> Dict[str, Optional[int]]: @computed_field # type: ignore[misc] @property - def layerX(self) -> Dict[int, int]: + def layerX(self) -> dict[int, int]: return self._layer_x @computed_field # type: ignore[misc] @property - def layerY(self) -> Dict[int, int]: + def layerY(self) -> dict[int, int]: return self._layer_y @computed_field # type: ignore[misc] @property - def layerColor(self) -> Dict[int, str]: + def layerColor(self) -> dict[int, str]: return self._layer_color def yield_area_ui(self) -> AreaUi: @@ -196,18 +189,18 @@ def yield_area_ui(self) -> AreaUi: class Area: - def __init__( # TODO: Find a way to avoid circular imports + def __init__( # type: ignore # TODO: Find a way to avoid circular imports self, name: str, - area_service: BaseAreaService, - storage_service: BaseShortTermStorageService, - thermal_service: BaseThermalService, - renewable_service: BaseRenewableService, - hydro_service: BaseHydroService, + area_service, + storage_service, + thermal_service, + renewable_service, + hydro_service, *, - renewables: Optional[Dict[str, RenewableCluster]] = None, - thermals: Optional[Dict[str, ThermalCluster]] = None, - st_storages: Optional[Dict[str, STStorage]] = None, + renewables: Optional[dict[str, RenewableCluster]] = None, + thermals: Optional[dict[str, ThermalCluster]] = None, + st_storages: Optional[dict[str, STStorage]] = None, hydro: Optional[Hydro] = None, properties: Optional[AreaProperties] = None, ui: Optional[AreaUi] = None, @@ -260,7 +253,7 @@ def create_thermal_cluster( ) -> ThermalCluster: thermal = self._area_service.create_thermal_cluster(self.id, thermal_name, properties) self._thermals[thermal.id] = thermal - return thermal + return cast(ThermalCluster, thermal) def create_thermal_cluster_with_matrices( self, @@ -276,37 +269,37 @@ def create_thermal_cluster_with_matrices( self.id, cluster_name, parameters, prepro, modulation, series, CO2Cost, fuelCost ) self._thermals[thermal.id] = thermal - return thermal + return cast(ThermalCluster, thermal) def create_renewable_cluster( self, renewable_name: str, properties: Optional[RenewableClusterProperties], series: Optional[pd.DataFrame] ) -> RenewableCluster: renewable = self._area_service.create_renewable_cluster(self.id, renewable_name, properties, series=series) self._renewables[renewable.id] = renewable - return renewable + return cast(RenewableCluster, renewable) def create_st_storage(self, st_storage_name: str, properties: Optional[STStorageProperties] = None) -> STStorage: storage = self._area_service.create_st_storage(self.id, st_storage_name, properties) self._st_storages[storage.id] = storage - return storage + return cast(STStorage, storage) def get_load_matrix(self) -> pd.DataFrame: - return self._area_service.get_load_matrix(self.id) + return cast(pd.DataFrame, self._area_service.get_load_matrix(self.id)) def get_wind_matrix(self) -> pd.DataFrame: - return self._area_service.get_wind_matrix(self.id) + return cast(pd.DataFrame, self._area_service.get_wind_matrix(self.id)) def get_solar_matrix(self) -> pd.DataFrame: - return self._area_service.get_solar_matrix(self.id) + return cast(pd.DataFrame, self._area_service.get_solar_matrix(self.id)) def get_reserves_matrix(self) -> pd.DataFrame: - return self._area_service.get_reserves_matrix(self.id) + return cast(pd.DataFrame, self._area_service.get_reserves_matrix(self.id)) def get_misc_gen_matrix(self) -> pd.DataFrame: - return self._area_service.get_misc_gen_matrix(self.id) + return cast(pd.DataFrame, self._area_service.get_misc_gen_matrix(self.id)) - def delete_thermal_clusters(self, thermal_clusters: List[ThermalCluster]) -> None: + def delete_thermal_clusters(self, thermal_clusters: list[ThermalCluster]) -> None: self._area_service.delete_thermal_clusters(self.id, thermal_clusters) for cluster in thermal_clusters: self._thermals.pop(cluster.id) @@ -314,7 +307,7 @@ def delete_thermal_clusters(self, thermal_clusters: List[ThermalCluster]) -> Non def delete_thermal_cluster(self, thermal_cluster: ThermalCluster) -> None: self.delete_thermal_clusters([thermal_cluster]) - def delete_renewable_clusters(self, renewable_clusters: List[RenewableCluster]) -> None: + def delete_renewable_clusters(self, renewable_clusters: list[RenewableCluster]) -> None: self._area_service.delete_renewable_clusters(self.id, renewable_clusters) for cluster in renewable_clusters: self._renewables.pop(cluster.id) @@ -322,7 +315,7 @@ def delete_renewable_clusters(self, renewable_clusters: List[RenewableCluster]) def delete_renewable_cluster(self, renewable_cluster: RenewableCluster) -> None: self.delete_renewable_clusters([renewable_cluster]) - def delete_st_storages(self, storages: List[STStorage]) -> None: + def delete_st_storages(self, storages: list[STStorage]) -> None: self._area_service.delete_st_storages(self.id, storages) for storage in storages: self._st_storages.pop(storage.id) @@ -356,29 +349,29 @@ def create_misc_gen(self, series: pd.DataFrame) -> None: def create_hydro( self, properties: Optional[HydroProperties] = None, - matrices: Optional[Dict[HydroMatrixName, pd.DataFrame]] = None, + matrices: Optional[dict[HydroMatrixName, pd.DataFrame]] = None, ) -> Hydro: # todo: is it necessary to create allocation or correlation ? hydro = self._area_service.create_hydro(self.id, properties, matrices) self._hydro = hydro - return hydro + return cast(Hydro, hydro) def read_st_storages( self, - ) -> List[STStorage]: - return self._storage_service.read_st_storages(self.id) + ) -> list[STStorage]: + return cast(list[STStorage], self._storage_service.read_st_storages(self.id)) def read_renewables( self, - ) -> List[RenewableCluster]: - return self._renewable_service.read_renewables(self.id) + ) -> list[RenewableCluster]: + return cast(list[RenewableCluster], self._renewable_service.read_renewables(self.id)) def read_thermal_clusters( self, - ) -> List[ThermalCluster]: - return self._thermal_service.read_thermal_clusters(self.id) + ) -> list[ThermalCluster]: + return cast(list[ThermalCluster], self._thermal_service.read_thermal_clusters(self.id)) def read_hydro( self, ) -> Hydro: - return self._area_service.read_hydro(self.id) + return cast(Hydro, self._area_service.read_hydro(self.id)) diff --git a/src/antares/craft/model/binding_constraint.py b/src/antares/craft/model/binding_constraint.py index 1547deff..908b50c5 100644 --- a/src/antares/craft/model/binding_constraint.py +++ b/src/antares/craft/model/binding_constraint.py @@ -11,11 +11,10 @@ # This file is part of the Antares project. from enum import Enum -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union, cast import pandas as pd -from antares.craft.service.base_services import BaseBindingConstraintService from antares.craft.tools.all_optional_meta import all_optional_model from antares.craft.tools.contents_tool import EnumIgnoreCase, transform_name_to_id from pydantic import BaseModel, Field, model_validator @@ -77,12 +76,12 @@ class ConstraintTerm(TermOperators): id: str = Field(init=False) @model_validator(mode="before") - def fill_id(cls, v: Dict[str, Any]) -> Dict[str, Any]: + def fill_id(cls, v: dict[str, Any]) -> dict[str, Any]: v["id"] = cls.generate_id(v["data"]) return v @classmethod - def generate_id(cls, data: Union[Dict[str, str], LinkData, ClusterData]) -> str: + def generate_id(cls, data: Union[dict[str, str], LinkData, ClusterData]) -> str: if isinstance(data, dict): if "area1" in data: return "%".join(sorted((data["area1"].lower(), data["area2"].lower()))) @@ -121,12 +120,12 @@ class BindingConstraintProperties(DefaultBindingConstraintProperties): class BindingConstraint: - def __init__( # TODO: Find a way to avoid circular imports + def __init__( # type: ignore # TODO: Find a way to avoid circular imports self, name: str, - binding_constraint_service: BaseBindingConstraintService, + binding_constraint_service, properties: Optional[BindingConstraintProperties] = None, - terms: Optional[List[ConstraintTerm]] = None, + terms: Optional[list[ConstraintTerm]] = None, ): self._name = name self._binding_constraint_service = binding_constraint_service @@ -150,10 +149,10 @@ def properties(self) -> BindingConstraintProperties: def properties(self, new_properties: BindingConstraintProperties) -> None: self._properties = new_properties - def get_terms(self) -> Dict[str, ConstraintTerm]: + def get_terms(self) -> dict[str, ConstraintTerm]: return self._terms - def add_terms(self, terms: List[ConstraintTerm]) -> None: + def add_terms(self, terms: list[ConstraintTerm]) -> None: added_terms = self._binding_constraint_service.add_constraint_terms(self, terms) for term in added_terms: self._terms[term.id] = term @@ -167,13 +166,20 @@ def update_properties(self, properties: BindingConstraintProperties) -> None: self._properties = new_properties def get_less_term_matrix(self) -> pd.DataFrame: - return self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.LESS_TERM) + return cast( + pd.DataFrame, self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.LESS_TERM) + ) def get_equal_term_matrix(self) -> pd.DataFrame: - return self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.EQUAL_TERM) + return cast( + pd.DataFrame, self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.EQUAL_TERM) + ) def get_greater_term_matrix(self) -> pd.DataFrame: - return self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.GREATER_TERM) + return cast( + pd.DataFrame, + self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.GREATER_TERM), + ) def update_less_term_matrix(self, matrix: pd.DataFrame) -> None: self._binding_constraint_service.update_constraint_matrix(self, ConstraintMatrixName.LESS_TERM, matrix) diff --git a/src/antares/craft/model/hydro.py b/src/antares/craft/model/hydro.py index 154fd92a..c465d988 100644 --- a/src/antares/craft/model/hydro.py +++ b/src/antares/craft/model/hydro.py @@ -11,11 +11,10 @@ # This file is part of the Antares project. from enum import Enum -from typing import Dict, Optional +from typing import Dict, Optional, cast import pandas as pd -from antares.craft.service.base_services import BaseHydroService from antares.craft.tools.all_optional_meta import all_optional_model from pydantic import BaseModel from pydantic.alias_generators import to_camel @@ -91,9 +90,9 @@ def yield_hydro_properties(self) -> HydroProperties: class Hydro: - def __init__( # + def __init__( # type: ignore self, - service: BaseHydroService, + service, area_id: str, properties: Optional[HydroProperties] = None, matrices: Optional[Dict[HydroMatrixName, pd.DataFrame]] = None, @@ -116,16 +115,16 @@ def matrices(self) -> Optional[Dict[HydroMatrixName, pd.DataFrame]]: return self._matrices def get_maxpower(self) -> pd.DataFrame: - return self._service.get_maxpower(self.area_id) + return cast(pd.DataFrame, self._service.get_maxpower(self.area_id)) def get_reservoir(self) -> pd.DataFrame: - return self._service.get_reservoir(self.area_id) + return cast(pd.DataFrame, self._service.get_reservoir(self.area_id)) def get_inflow_pattern(self) -> pd.DataFrame: - return self._service.get_inflow_pattern(self.area_id) + return cast(pd.DataFrame, self._service.get_inflow_pattern(self.area_id)) def get_credit_modulations(self) -> pd.DataFrame: - return self._service.get_credit_modulations(self.area_id) + return cast(pd.DataFrame, self._service.get_credit_modulations(self.area_id)) def get_water_values(self) -> pd.DataFrame: - return self._service.get_water_values(self.area_id) + return cast(pd.DataFrame, self._service.get_water_values(self.area_id)) diff --git a/src/antares/craft/model/link.py b/src/antares/craft/model/link.py index 110e62ca..953073ef 100644 --- a/src/antares/craft/model/link.py +++ b/src/antares/craft/model/link.py @@ -11,12 +11,11 @@ # This file is part of the Antares project. from enum import Enum -from typing import Mapping, Optional, Set +from typing import Mapping, Optional, Set, cast import pandas as pd from antares.craft.model.commons import FilterOption, sort_filter_values -from antares.craft.service.base_services import BaseLinkService from antares.craft.tools.alias_generators import to_kebab from antares.craft.tools.all_optional_meta import all_optional_model from antares.craft.tools.contents_tool import transform_name_to_id @@ -134,11 +133,11 @@ def yield_link_ui(self) -> LinkUi: class Link: - def __init__( # TODO: Find a way to avoid circular imports + def __init__( # type: ignore # TODO: Find a way to avoid circular imports self, area_from: str, area_to: str, - link_service: BaseLinkService, + link_service, properties: Optional[LinkProperties] = None, ui: Optional[LinkUi] = None, ): @@ -170,12 +169,12 @@ def ui(self) -> LinkUi: def update_properties(self, properties: LinkProperties) -> LinkProperties: new_properties = self._link_service.update_link_properties(self, properties) self._properties = new_properties - return new_properties + return cast(LinkProperties, new_properties) def update_ui(self, ui: LinkUi) -> LinkUi: new_ui = self._link_service.update_link_ui(self, ui) self._ui = new_ui - return new_ui + return cast(LinkUi, new_ui) def create_parameters(self, series: pd.DataFrame) -> None: self._link_service.create_parameters(series, self.area_from_id, self.area_to_id) @@ -187,10 +186,10 @@ def create_capacity_indirect(self, series: pd.DataFrame) -> None: self._link_service.create_capacity_indirect(series, self.area_from_id, self.area_to_id) def get_capacity_direct(self) -> pd.DataFrame: - return self._link_service.get_capacity_direct(self.area_from_id, self.area_to_id) + return cast(pd.DataFrame, self._link_service.get_capacity_direct(self.area_from_id, self.area_to_id)) def get_capacity_indirect(self) -> pd.DataFrame: - return self._link_service.get_capacity_indirect(self.area_from_id, self.area_to_id) + return cast(pd.DataFrame, self._link_service.get_capacity_indirect(self.area_from_id, self.area_to_id)) def get_parameters(self) -> pd.DataFrame: - return self._link_service.get_parameters(self.area_from_id, self.area_to_id) + return cast(pd.DataFrame, self._link_service.get_parameters(self.area_from_id, self.area_to_id)) diff --git a/src/antares/craft/model/output.py b/src/antares/craft/model/output.py index fd0c5a86..476360da 100644 --- a/src/antares/craft/model/output.py +++ b/src/antares/craft/model/output.py @@ -10,11 +10,10 @@ # # This file is part of the Antares project. from enum import Enum -from typing import Optional, Union +from typing import Optional, Union, cast import pandas as pd -from antares.craft.service.base_services import BaseOutputService from pydantic import BaseModel @@ -76,7 +75,7 @@ def to_api_query(self, object_type: str) -> str: class Output: - def __init__(self, name: str, archived: bool, output_service: BaseOutputService): + def __init__(self, name: str, archived: bool, output_service): # type: ignore self._name = name self._archived = archived self._output_service = output_service @@ -98,7 +97,7 @@ def get_matrix(self, path: str) -> pd.DataFrame: Returns: Pandas DataFrame """ - return self._output_service.get_matrix(self.name, path) + return cast(pd.DataFrame, self._output_service.get_matrix(self.name, path)) def aggregate_areas_mc_ind( self, @@ -125,7 +124,7 @@ def aggregate_areas_mc_ind( columns_names=columns_names, ) - return self._output_service.aggregate_values(self.name, aggregation_entry, "areas", "ind") + return cast(pd.DataFrame, self._output_service.aggregate_values(self.name, aggregation_entry, "areas", "ind")) def aggregate_links_mc_ind( self, @@ -152,7 +151,7 @@ def aggregate_links_mc_ind( columns_names=columns_names, ) - return self._output_service.aggregate_values(self.name, aggregation_entry, "links", "ind") + return cast(pd.DataFrame, self._output_service.aggregate_values(self.name, aggregation_entry, "links", "ind")) def aggregate_areas_mc_all( self, @@ -179,7 +178,7 @@ def aggregate_areas_mc_all( columns_names=columns_names, ) - return self._output_service.aggregate_values(self.name, aggregation_entry, "areas", "all") + return cast(pd.DataFrame, self._output_service.aggregate_values(self.name, aggregation_entry, "areas", "all")) def aggregate_links_mc_all( self, @@ -206,4 +205,4 @@ def aggregate_links_mc_all( columns_names=columns_names, ) - return self._output_service.aggregate_values(self.name, aggregation_entry, "links", "all") + return cast(pd.DataFrame, self._output_service.aggregate_values(self.name, aggregation_entry, "links", "all")) diff --git a/src/antares/craft/model/renewable.py b/src/antares/craft/model/renewable.py index 8ae45509..0c27bbbf 100644 --- a/src/antares/craft/model/renewable.py +++ b/src/antares/craft/model/renewable.py @@ -11,12 +11,11 @@ # This file is part of the Antares project. from enum import Enum -from typing import Optional +from typing import Optional, cast import pandas as pd from antares.craft.model.cluster import ClusterProperties -from antares.craft.service.base_services import BaseRenewableService from antares.craft.tools.all_optional_meta import all_optional_model from antares.craft.tools.contents_tool import transform_name_to_id @@ -91,9 +90,9 @@ def yield_renewable_cluster_properties(self) -> RenewableClusterProperties: class RenewableCluster: - def __init__( # TODO: Find a way to avoid circular imports + def __init__( # type: ignore # TODO: Find a way to avoid circular imports self, - renewable_service: BaseRenewableService, + renewable_service, area_id: str, name: str, properties: Optional[RenewableClusterProperties] = None, @@ -127,7 +126,7 @@ def update_properties(self, properties: RenewableClusterProperties) -> None: self._properties = new_properties def get_timeseries(self) -> pd.DataFrame: - return self._renewable_service.get_renewable_matrix(self.id, self.area_id) + return cast(pd.DataFrame, self._renewable_service.get_renewable_matrix(self.id, self.area_id)) def update_renewable_matrix(self, matrix: pd.DataFrame) -> None: self._renewable_service.update_renewable_matrix(self, matrix) diff --git a/src/antares/craft/model/st_storage.py b/src/antares/craft/model/st_storage.py index 08b5a037..ddada7c1 100644 --- a/src/antares/craft/model/st_storage.py +++ b/src/antares/craft/model/st_storage.py @@ -11,11 +11,10 @@ # This file is part of the Antares project. from enum import Enum -from typing import Optional +from typing import Optional, cast import pandas as pd -from antares.craft.service.base_services import BaseShortTermStorageService from antares.craft.tools.all_optional_meta import all_optional_model from antares.craft.tools.contents_tool import transform_name_to_id from pydantic import BaseModel @@ -91,16 +90,9 @@ def yield_st_storage_properties(self) -> STStorageProperties: class STStorage: - def __init__( - self, - storage_service: BaseShortTermStorageService, - area_id: str, - name: str, - properties: Optional[STStorageProperties] = None, - ): # TODO: Find a way to avoid circular imports + def __init__(self, storage_service, area_id: str, name: str, properties: Optional[STStorageProperties] = None): # type: ignore # TODO: Find a way to avoid circular imports self._area_id = area_id self._storage_service = storage_service - self._storage_service = storage_service self._name = name self._id = transform_name_to_id(name) self._properties = properties or STStorageProperties() @@ -128,39 +120,31 @@ def update_properties(self, properties: STStorageProperties) -> None: self._properties = new_properties def get_pmax_injection(self) -> pd.DataFrame: - return self._storage_service.get_storage_matrix(self, STStorageMatrixName.PMAX_INJECTION) + return cast(pd.DataFrame, self._storage_service.get_storage_matrix(self, STStorageMatrixName.PMAX_INJECTION)) def get_pmax_withdrawal(self) -> pd.DataFrame: - return self._storage_service.get_storage_matrix(self, STStorageMatrixName.PMAX_WITHDRAWAL) + return cast(pd.DataFrame, self._storage_service.get_storage_matrix(self, STStorageMatrixName.PMAX_WITHDRAWAL)) def get_lower_rule_curve(self) -> pd.DataFrame: - return self._storage_service.get_storage_matrix(self, STStorageMatrixName.LOWER_CURVE_RULE) + return cast(pd.DataFrame, self._storage_service.get_storage_matrix(self, STStorageMatrixName.LOWER_CURVE_RULE)) def get_upper_rule_curve(self) -> pd.DataFrame: - return self._storage_service.get_storage_matrix(self, STStorageMatrixName.UPPER_RULE_CURVE) + return cast(pd.DataFrame, self._storage_service.get_storage_matrix(self, STStorageMatrixName.UPPER_RULE_CURVE)) def get_storage_inflows(self) -> pd.DataFrame: - return self._storage_service.get_storage_matrix(self, STStorageMatrixName.INFLOWS) + return cast(pd.DataFrame, self._storage_service.get_storage_matrix(self, STStorageMatrixName.INFLOWS)) def upload_pmax_injection(self, p_max_injection_matrix: pd.DataFrame) -> None: - return self._storage_service.upload_storage_matrix( - self, STStorageMatrixName.PMAX_INJECTION, p_max_injection_matrix - ) + self._storage_service.upload_storage_matrix(self, STStorageMatrixName.PMAX_INJECTION, p_max_injection_matrix) def upload_pmax_withdrawal(self, p_max_withdrawal_matrix: pd.DataFrame) -> None: - return self._storage_service.upload_storage_matrix( - self, STStorageMatrixName.PMAX_WITHDRAWAL, p_max_withdrawal_matrix - ) + self._storage_service.upload_storage_matrix(self, STStorageMatrixName.PMAX_WITHDRAWAL, p_max_withdrawal_matrix) def upload_lower_rule_curve(self, lower_rule_curve_matrix: pd.DataFrame) -> None: - return self._storage_service.upload_storage_matrix( - self, STStorageMatrixName.LOWER_CURVE_RULE, lower_rule_curve_matrix - ) + self._storage_service.upload_storage_matrix(self, STStorageMatrixName.LOWER_CURVE_RULE, lower_rule_curve_matrix) def upload_upper_rule_curve(self, upper_rule_curve_matrix: pd.DataFrame) -> None: - return self._storage_service.upload_storage_matrix( - self, STStorageMatrixName.UPPER_RULE_CURVE, upper_rule_curve_matrix - ) + self._storage_service.upload_storage_matrix(self, STStorageMatrixName.UPPER_RULE_CURVE, upper_rule_curve_matrix) def upload_storage_inflows(self, inflows_matrix: pd.DataFrame) -> None: - return self._storage_service.upload_storage_matrix(self, STStorageMatrixName.INFLOWS, inflows_matrix) + self._storage_service.upload_storage_matrix(self, STStorageMatrixName.INFLOWS, inflows_matrix) diff --git a/src/antares/craft/model/study.py b/src/antares/craft/model/study.py index 9668f3b6..ecb4aea9 100644 --- a/src/antares/craft/model/study.py +++ b/src/antares/craft/model/study.py @@ -16,7 +16,7 @@ from pathlib import Path, PurePath from types import MappingProxyType -from typing import List, Optional, Union, cast +from typing import List, Optional, cast import pandas as pd @@ -40,7 +40,7 @@ from antares.craft.model.output import Output from antares.craft.model.settings.study_settings import StudySettings from antares.craft.model.simulation import AntaresSimulationParameters, Job -from antares.craft.service.base_services import BaseStudyService, BaseLinkService +from antares.craft.service.base_services import BaseLinkService, BaseStudyService from antares.craft.service.local_services.services.settings import edit_study_settings from antares.craft.service.service_factory import ServiceFactory from antares.craft.tools.ini_tool import IniFile, InitializationFilesTypes diff --git a/src/antares/craft/model/thermal.py b/src/antares/craft/model/thermal.py index a2f7cfc3..b53695b9 100644 --- a/src/antares/craft/model/thermal.py +++ b/src/antares/craft/model/thermal.py @@ -11,12 +11,11 @@ # This file is part of the Antares project. from enum import Enum -from typing import Optional +from typing import Optional, cast import pandas as pd from antares.craft.model.cluster import ClusterProperties -from antares.craft.service.base_services import BaseThermalService from antares.craft.tools.all_optional_meta import all_optional_model from antares.craft.tools.contents_tool import transform_name_to_id @@ -172,13 +171,7 @@ class ThermalClusterMatrixName(Enum): class ThermalCluster: - def __init__( - self, - thermal_service: BaseThermalService, - area_id: str, - name: str, - properties: Optional[ThermalClusterProperties] = None, - ): # TODO: Find a way to avoid circular imports + def __init__(self, thermal_service, area_id: str, name: str, properties: Optional[ThermalClusterProperties] = None): # type: ignore # TODO: Find a way to avoid circular imports self._area_id = area_id self._thermal_service = thermal_service self._name = name @@ -208,19 +201,25 @@ def update_properties(self, properties: ThermalClusterProperties) -> None: self._properties = new_properties def get_prepro_data_matrix(self) -> pd.DataFrame: - return self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.PREPRO_DATA) + return cast(pd.DataFrame, self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.PREPRO_DATA)) def get_prepro_modulation_matrix(self) -> pd.DataFrame: - return self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.PREPRO_MODULATION) + return cast( + pd.DataFrame, self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.PREPRO_MODULATION) + ) def get_series_matrix(self) -> pd.DataFrame: - return self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.SERIES) + return cast(pd.DataFrame, self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.SERIES)) def get_co2_cost_matrix(self) -> pd.DataFrame: - return self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.SERIES_CO2_COST) + return cast( + pd.DataFrame, self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.SERIES_CO2_COST) + ) def get_fuel_cost_matrix(self) -> pd.DataFrame: - return self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.SERIES_FUEL_COST) + return cast( + pd.DataFrame, self._thermal_service.get_thermal_matrix(self, ThermalClusterMatrixName.SERIES_FUEL_COST) + ) def update_thermal_matrix(self, matrix: pd.DataFrame) -> None: self._thermal_service.update_thermal_matrix(self, matrix) diff --git a/src/antares/craft/service/base_services.py b/src/antares/craft/service/base_services.py index ccc6a111..cfbb5505 100644 --- a/src/antares/craft/service/base_services.py +++ b/src/antares/craft/service/base_services.py @@ -12,11 +12,12 @@ from abc import ABC, abstractmethod from pathlib import Path, PurePath -from typing import TYPE_CHECKING, Dict, List, Optional +from typing import TYPE_CHECKING, Optional import pandas as pd from antares.craft.config.base_configuration import BaseConfiguration +from antares.craft.model.area import Area, AreaProperties, AreaUi from antares.craft.model.binding_constraint import ( BindingConstraint, BindingConstraintProperties, @@ -38,7 +39,6 @@ if TYPE_CHECKING: from antares.craft.model.study import Study - from antares.craft.model.area import Area, AreaProperties, AreaUi class BaseAreaService(ABC): @@ -208,29 +208,29 @@ def delete_area(self, area_id: str) -> None: pass @abstractmethod - def delete_thermal_clusters(self, area_id: str, thermal_clusters: List[ThermalCluster]) -> None: + def delete_thermal_clusters(self, area_id: str, thermal_clusters: list[ThermalCluster]) -> None: """ Args: area_id: area containing the cluster - thermal_clusters: List of thermal clusters object to be deleted + thermal_clusters: list of thermal clusters object to be deleted """ pass @abstractmethod - def delete_renewable_clusters(self, area_id: str, renewable_clusters: List[RenewableCluster]) -> None: + def delete_renewable_clusters(self, area_id: str, renewable_clusters: list[RenewableCluster]) -> None: """ Args: area_id: area containing the cluster - renewable_clusters: List of renewable clusters object to be deleted + renewable_clusters: list of renewable clusters object to be deleted """ pass @abstractmethod - def delete_st_storages(self, area_id: str, storages: List[STStorage]) -> None: + def delete_st_storages(self, area_id: str, storages: list[STStorage]) -> None: """ Args: area_id: area containing the cluster - storages: List of short term storage objects to be deleted + storages: list of short term storage objects to be deleted """ pass @@ -289,7 +289,7 @@ def create_hydro( self, area_id: str, properties: Optional[HydroProperties], - matrices: Optional[Dict[HydroMatrixName, pd.DataFrame]], + matrices: Optional[dict[HydroMatrixName, pd.DataFrame]], ) -> Hydro: """ Args: @@ -389,7 +389,7 @@ def create_parameters(self, series: pd.DataFrame, area_from: str, area_to: str) pass @abstractmethod - def read_links(self) -> List[Link]: + def read_links(self) -> list[Link]: pass @abstractmethod @@ -444,7 +444,7 @@ def get_thermal_matrix(self, thermal_cluster: ThermalCluster, ts_name: ThermalCl pass @abstractmethod - def read_thermal_clusters(self, area_id: str) -> List[ThermalCluster]: + def read_thermal_clusters(self, area_id: str) -> list[ThermalCluster]: pass @@ -456,7 +456,7 @@ def create_binding_constraint( self, name: str, properties: Optional[BindingConstraintProperties] = None, - terms: Optional[List[ConstraintTerm]] = None, + terms: Optional[list[ConstraintTerm]] = None, less_term_matrix: Optional[pd.DataFrame] = None, equal_term_matrix: Optional[pd.DataFrame] = None, greater_term_matrix: Optional[pd.DataFrame] = None, @@ -476,7 +476,7 @@ def create_binding_constraint( pass @abstractmethod - def add_constraint_terms(self, constraint: BindingConstraint, terms: List[ConstraintTerm]) -> List[ConstraintTerm]: + def add_constraint_terms(self, constraint: BindingConstraint, terms: list[ConstraintTerm]) -> list[ConstraintTerm]: """ Args: constraint: the concerned binding constraint @@ -655,7 +655,7 @@ def update_renewable_matrix(self, renewable_cluster: RenewableCluster, matrix: p pass @abstractmethod - def read_renewables(self, area_id: str) -> List[RenewableCluster]: + def read_renewables(self, area_id: str) -> list[RenewableCluster]: pass @@ -672,7 +672,7 @@ def update_st_storage_properties( pass @abstractmethod - def read_st_storages(self, area_id: str) -> List[STStorage]: + def read_st_storages(self, area_id: str) -> list[STStorage]: pass @abstractmethod diff --git a/src/antares/craft/service/local_services/renewable_local.py b/src/antares/craft/service/local_services/renewable_local.py index 19032643..6db87de8 100644 --- a/src/antares/craft/service/local_services/renewable_local.py +++ b/src/antares/craft/service/local_services/renewable_local.py @@ -42,10 +42,12 @@ def get_renewable_matrix(self, cluster_id: str, area_id: str) -> pd.DataFrame: def _extract_renewable_properties(self, renewable_data: dict[str, Any]) -> RenewableClusterProperties: property_types = get_type_hints(RenewableClusterPropertiesLocal) + key_mapping = {"name": "renewable_name"} + parsed_data = { - key: property_types[key](value) if value is not None else None + key_mapping.get(key, key): property_types[key_mapping.get(key, key)](value) if value is not None else None for key, value in renewable_data.items() - if key in property_types + if key_mapping.get(key, key) in property_types } return RenewableClusterPropertiesLocal(**parsed_data).yield_renewable_cluster_properties() diff --git a/src/antares/craft/service/local_services/thermal_local.py b/src/antares/craft/service/local_services/thermal_local.py index 06208e87..879252ec 100644 --- a/src/antares/craft/service/local_services/thermal_local.py +++ b/src/antares/craft/service/local_services/thermal_local.py @@ -60,10 +60,12 @@ def get_thermal_matrix(self, thermal_cluster: ThermalCluster, ts_name: ThermalCl def _extract_thermal_properties(self, thermal_data: dict[str, Any]) -> ThermalClusterProperties: property_types = get_type_hints(ThermalClusterPropertiesLocal) + key_mapping = {"name": "thermal_name"} + parsed_data = { - key: property_types[key](value) if value is not None else None + key_mapping.get(key, key): property_types[key_mapping.get(key, key)](value) if value is not None else None for key, value in thermal_data.items() - if key in property_types + if key_mapping.get(key, key) in property_types } return ThermalClusterPropertiesLocal(**parsed_data).yield_thermal_cluster_properties() diff --git a/tests/antares/delete/test_delete_api.py b/tests/antares/delete/test_delete_api.py index c82838cb..7b1a7268 100644 --- a/tests/antares/delete/test_delete_api.py +++ b/tests/antares/delete/test_delete_api.py @@ -43,11 +43,11 @@ class TestDeleteAPI: api = APIconf("https://antares.com", "token", verify=False) study_id = "22c52f44-4c2a-407b-862b-490887f93dd8" study_service = StudyApiService(api, study_id) - area_service = AreaApiService(api, study_id) thermal_service = ThermalApiService(api, study_id) renewable_service = RenewableApiService(api, study_id) st_storage_service = ShortTermStorageApiService(api, study_id) hydro_service = HydroApiService(api, study_id) + area_service = AreaApiService(api, study_id, st_storage_service, thermal_service, renewable_service, hydro_service) area_fr = Area("fr", area_service, st_storage_service, thermal_service, renewable_service, hydro_service) area_be = Area("be", area_service, st_storage_service, thermal_service, renewable_service, hydro_service) link_service = LinkApiService(api, study_id) diff --git a/tests/antares/services/api_services/test_area_api.py b/tests/antares/services/api_services/test_area_api.py index fbc7fc47..8f96a26e 100644 --- a/tests/antares/services/api_services/test_area_api.py +++ b/tests/antares/services/api_services/test_area_api.py @@ -37,15 +37,21 @@ class TestCreateAPI: api = APIconf("https://antares.com", "token", verify=False) study_id = "22c52f44-4c2a-407b-862b-490887f93dd8" - area = Area( - "area_test", - ServiceFactory(api, study_id).create_area_service(), - ServiceFactory(api, study_id).create_st_storage_service(), - ServiceFactory(api, study_id).create_thermal_service(), - ServiceFactory(api, study_id).create_renewable_service(), - ServiceFactory(api, study_id).create_hydro_service(), + area_service = ServiceFactory(api, study_id).create_area_service() + st_storage_service = ServiceFactory(api, study_id).create_st_storage_service() + thermal_service = ServiceFactory(api, study_id).create_thermal_service() + renewable_service = ServiceFactory(api, study_id).create_renewable_service() + hydro_service = ServiceFactory(api, study_id).create_hydro_service() + area = Area("area_test", area_service, st_storage_service, thermal_service, renewable_service, hydro_service) + + area_api = AreaApiService( + api, + "248bbb99-c909-47b7-b239-01f6f6ae7de7", + st_storage_service, + thermal_service, + renewable_service, + hydro_service, ) - area_api = AreaApiService(api, "248bbb99-c909-47b7-b239-01f6f6ae7de7") antares_web_description_msg = "Mocked Server KO" matrix = pd.DataFrame(data=[[0]]) study = Study("TestStudy", "880", ServiceFactory(api, study_id)) diff --git a/tests/antares/services/api_services/test_renewable_api.py b/tests/antares/services/api_services/test_renewable_api.py index 6fc47aa0..7c03ccc1 100644 --- a/tests/antares/services/api_services/test_renewable_api.py +++ b/tests/antares/services/api_services/test_renewable_api.py @@ -9,10 +9,11 @@ # SPDX-License-Identifier: MPL-2.0 # # This file is part of the Antares project. - import pytest import requests_mock +from unittest.mock import Mock + import pandas as pd from antares.craft.api_conf.api_conf import APIconf @@ -136,8 +137,13 @@ def test_read_renewables(self): with requests_mock.Mocker() as mocker: mocker.get(url + "clusters/renewable", json=json_renewable) - area_api = AreaApiService(self.api, study_id_test) renewable_api = RenewableApiService(self.api, study_id_test) + storage_service = Mock() + thermal_service = Mock() + hydro_service = Mock() + area_api = AreaApiService( + self.api, study_id_test, storage_service, thermal_service, renewable_api, hydro_service + ) actual_renewable_list = renewable_api.read_renewables(area_id) diff --git a/tests/antares/services/api_services/test_st_storage_api.py b/tests/antares/services/api_services/test_st_storage_api.py index 79f81348..0ad79538 100644 --- a/tests/antares/services/api_services/test_st_storage_api.py +++ b/tests/antares/services/api_services/test_st_storage_api.py @@ -9,10 +9,11 @@ # SPDX-License-Identifier: MPL-2.0 # # This file is part of the Antares project. - import pytest import requests_mock +from unittest.mock import Mock + import pandas as pd from antares.craft.api_conf.api_conf import APIconf @@ -138,8 +139,14 @@ def test_read_st_storages(self): with requests_mock.Mocker() as mocker: mocker.get(url + "storages", json=json_storage) - area_api = AreaApiService(self.api, study_id_test) + storage_api = ShortTermStorageApiService(self.api, study_id_test) + renewable_service = Mock() + thermal_service = Mock() + hydro_service = Mock() + area_api = AreaApiService( + self.api, study_id_test, storage_api, thermal_service, renewable_service, hydro_service + ) actual_storage_list = storage_api.read_st_storages(area_id) diff --git a/tests/antares/services/api_services/test_thermal_api.py b/tests/antares/services/api_services/test_thermal_api.py index 2feaf5a1..4e837a6e 100644 --- a/tests/antares/services/api_services/test_thermal_api.py +++ b/tests/antares/services/api_services/test_thermal_api.py @@ -9,10 +9,11 @@ # SPDX-License-Identifier: MPL-2.0 # # This file is part of the Antares project. - import pytest import requests_mock +from unittest.mock import Mock + import pandas as pd from antares.craft.api_conf.api_conf import APIconf @@ -152,8 +153,14 @@ def test_read_thermals(self): with requests_mock.Mocker() as mocker: mocker.get(url + "clusters/thermal", json=json_thermal) - area_api = AreaApiService(self.api, study_id_test) + thermal_api = ThermalApiService(self.api, study_id_test) + renewable_service = Mock() + storage_service = Mock() + hydro_service = Mock() + area_api = AreaApiService( + self.api, study_id_test, storage_service, thermal_api, renewable_service, hydro_service + ) actual_thermal_list = thermal_api.read_thermal_clusters(area_id)