Skip to content

Commit

Permalink
fix: ignore circular imports and fix related typing
Browse files Browse the repository at this point in the history
  • Loading branch information
salemsd committed Feb 10, 2025
1 parent 19b1fde commit 41af0d4
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 169 deletions.
89 changes: 41 additions & 48 deletions src/antares/craft/model/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -276,53 +269,53 @@ 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)

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)

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)
Expand Down Expand Up @@ -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))
30 changes: 18 additions & 12 deletions src/antares/craft/model/binding_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions src/antares/craft/model/hydro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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))
Loading

0 comments on commit 41af0d4

Please sign in to comment.