Skip to content

Commit

Permalink
fix: remove type ignore and fix circular imports (type_checking)
Browse files Browse the repository at this point in the history
  • Loading branch information
salemsd committed Feb 10, 2025
1 parent 653ec16 commit 4dc6b00
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 166 deletions.
51 changes: 29 additions & 22 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, Mapping, Optional, cast
from typing import Any, Mapping, Optional

import pandas as pd

Expand All @@ -26,6 +26,13 @@
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 @@ -189,14 +196,14 @@ def yield_area_ui(self) -> AreaUi:


class Area:
def __init__( # type: ignore # TODO: Find a way to avoid circular imports
def __init__( # TODO: Find a way to avoid circular imports
self,
name: str,
area_service,
storage_service,
thermal_service,
renewable_service,
hydro_service,
area_service: BaseAreaService,
storage_service: BaseShortTermStorageService,
thermal_service: BaseThermalService,
renewable_service: BaseRenewableService,
hydro_service: BaseHydroService,
*,
renewables: Optional[dict[str, RenewableCluster]] = None,
thermals: Optional[dict[str, ThermalCluster]] = None,
Expand Down Expand Up @@ -251,9 +258,9 @@ def ui(self) -> AreaUi:
def create_thermal_cluster(
self, thermal_name: str, properties: Optional[ThermalClusterProperties] = None
) -> ThermalCluster:
thermal = self._area_service.create_thermal_cluster(self.id, thermal_name, properties)
thermal: ThermalCluster = self._area_service.create_thermal_cluster(self.id, thermal_name, properties)
self._thermals[thermal.id] = thermal
return cast(ThermalCluster, thermal)
return thermal

def create_thermal_cluster_with_matrices(
self,
Expand All @@ -269,35 +276,35 @@ def create_thermal_cluster_with_matrices(
self.id, cluster_name, parameters, prepro, modulation, series, CO2Cost, fuelCost
)
self._thermals[thermal.id] = thermal
return cast(ThermalCluster, thermal)
return 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 cast(RenewableCluster, renewable)
return 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 cast(STStorage, storage)
return storage

def get_load_matrix(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._area_service.get_load_matrix(self.id))
return self._area_service.get_load_matrix(self.id)

def get_wind_matrix(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._area_service.get_wind_matrix(self.id))
return self._area_service.get_wind_matrix(self.id)

def get_solar_matrix(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._area_service.get_solar_matrix(self.id))
return self._area_service.get_solar_matrix(self.id)

def get_reserves_matrix(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._area_service.get_reserves_matrix(self.id))
return self._area_service.get_reserves_matrix(self.id)

def get_misc_gen_matrix(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._area_service.get_misc_gen_matrix(self.id))
return self._area_service.get_misc_gen_matrix(self.id)

def delete_thermal_clusters(self, thermal_clusters: list[ThermalCluster]) -> None:
self._area_service.delete_thermal_clusters(self.id, thermal_clusters)
Expand Down Expand Up @@ -354,24 +361,24 @@ def create_hydro(
# todo: is it necessary to create allocation or correlation ?
hydro = self._area_service.create_hydro(self.id, properties, matrices)
self._hydro = hydro
return cast(Hydro, hydro)
return hydro

def read_st_storages(
self,
) -> list[STStorage]:
return cast(list[STStorage], self._storage_service.read_st_storages(self.id))
return self._storage_service.read_st_storages(self.id)

def read_renewables(
self,
) -> list[RenewableCluster]:
return cast(list[RenewableCluster], self._renewable_service.read_renewables(self.id))
return self._renewable_service.read_renewables(self.id)

def read_thermal_clusters(
self,
) -> list[ThermalCluster]:
return cast(list[ThermalCluster], self._thermal_service.read_thermal_clusters(self.id))
return self._thermal_service.read_thermal_clusters(self.id)

def read_hydro(
self,
) -> Hydro:
return cast(Hydro, self._area_service.read_hydro(self.id))
return self._area_service.read_hydro(self.id)
20 changes: 7 additions & 13 deletions src/antares/craft/model/binding_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
# This file is part of the Antares project.

from enum import Enum
from typing import Any, Optional, Union, cast
from typing import Any, Optional, Union

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 @@ -120,10 +121,10 @@ class BindingConstraintProperties(DefaultBindingConstraintProperties):


class BindingConstraint:
def __init__( # type: ignore # TODO: Find a way to avoid circular imports
def __init__( # TODO: Find a way to avoid circular imports
self,
name: str,
binding_constraint_service,
binding_constraint_service: BaseBindingConstraintService,
properties: Optional[BindingConstraintProperties] = None,
terms: Optional[list[ConstraintTerm]] = None,
):
Expand Down Expand Up @@ -166,20 +167,13 @@ def update_properties(self, properties: BindingConstraintProperties) -> None:
self._properties = new_properties

def get_less_term_matrix(self) -> pd.DataFrame:
return cast(
pd.DataFrame, self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.LESS_TERM)
)
return self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.LESS_TERM)

def get_equal_term_matrix(self) -> pd.DataFrame:
return cast(
pd.DataFrame, self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.EQUAL_TERM)
)
return self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.EQUAL_TERM)

def get_greater_term_matrix(self) -> pd.DataFrame:
return cast(
pd.DataFrame,
self._binding_constraint_service.get_constraint_matrix(self, ConstraintMatrixName.GREATER_TERM),
)
return 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
21 changes: 11 additions & 10 deletions src/antares/craft/model/hydro.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
# This file is part of the Antares project.

from enum import Enum
from typing import Dict, Optional, cast
from typing import Optional

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 @@ -90,12 +91,12 @@ def yield_hydro_properties(self) -> HydroProperties:


class Hydro:
def __init__( # type: ignore
def __init__(
self,
service,
service: BaseHydroService,
area_id: str,
properties: Optional[HydroProperties] = None,
matrices: Optional[Dict[HydroMatrixName, pd.DataFrame]] = None,
matrices: Optional[dict[HydroMatrixName, pd.DataFrame]] = None,
):
self._area_id = area_id
self._service = service
Expand All @@ -111,20 +112,20 @@ def properties(self) -> Optional[HydroProperties]:
return self._properties

@property
def matrices(self) -> Optional[Dict[HydroMatrixName, pd.DataFrame]]:
def matrices(self) -> Optional[dict[HydroMatrixName, pd.DataFrame]]:
return self._matrices

def get_maxpower(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._service.get_maxpower(self.area_id))
return self._service.get_maxpower(self.area_id)

def get_reservoir(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._service.get_reservoir(self.area_id))
return self._service.get_reservoir(self.area_id)

def get_inflow_pattern(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._service.get_inflow_pattern(self.area_id))
return self._service.get_inflow_pattern(self.area_id)

def get_credit_modulations(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._service.get_credit_modulations(self.area_id))
return self._service.get_credit_modulations(self.area_id)

def get_water_values(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._service.get_water_values(self.area_id))
return self._service.get_water_values(self.area_id)
17 changes: 9 additions & 8 deletions src/antares/craft/model/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
# This file is part of the Antares project.

from enum import Enum
from typing import Mapping, Optional, Set, cast
from typing import Mapping, Optional, Set

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
Expand Down Expand Up @@ -133,11 +134,11 @@ def yield_link_ui(self) -> LinkUi:


class Link:
def __init__( # type: ignore # TODO: Find a way to avoid circular imports
def __init__( # TODO: Find a way to avoid circular imports
self,
area_from: str,
area_to: str,
link_service,
link_service: BaseLinkService,
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
):
Expand Down Expand Up @@ -169,12 +170,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 cast(LinkProperties, new_properties)
return new_properties

def update_ui(self, ui: LinkUi) -> LinkUi:
new_ui = self._link_service.update_link_ui(self, ui)
self._ui = new_ui
return cast(LinkUi, new_ui)
return new_ui

def create_parameters(self, series: pd.DataFrame) -> None:
self._link_service.create_parameters(series, self.area_from_id, self.area_to_id)
Expand All @@ -186,10 +187,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 cast(pd.DataFrame, self._link_service.get_capacity_direct(self.area_from_id, self.area_to_id))
return self._link_service.get_capacity_direct(self.area_from_id, self.area_to_id)

def get_capacity_indirect(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._link_service.get_capacity_indirect(self.area_from_id, self.area_to_id))
return self._link_service.get_capacity_indirect(self.area_from_id, self.area_to_id)

def get_parameters(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._link_service.get_parameters(self.area_from_id, self.area_to_id))
return self._link_service.get_parameters(self.area_from_id, self.area_to_id)
17 changes: 9 additions & 8 deletions src/antares/craft/model/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#
# This file is part of the Antares project.
from enum import Enum
from typing import Optional, Union, cast
from typing import Optional, Union

import pandas as pd

from antares.craft.service.base_services import BaseOutputService
from pydantic import BaseModel


Expand Down Expand Up @@ -75,10 +76,10 @@ def to_api_query(self, object_type: str) -> str:


class Output:
def __init__(self, name: str, archived: bool, output_service): # type: ignore
def __init__(self, name: str, archived: bool, output_service: BaseOutputService):
self._name = name
self._archived = archived
self._output_service = output_service
self._output_service: BaseOutputService = output_service

@property
def name(self) -> str:
Expand All @@ -97,7 +98,7 @@ def get_matrix(self, path: str) -> pd.DataFrame:
Returns: Pandas DataFrame
"""
return cast(pd.DataFrame, self._output_service.get_matrix(self.name, path))
return self._output_service.get_matrix(self.name, path)

def aggregate_areas_mc_ind(
self,
Expand All @@ -124,7 +125,7 @@ def aggregate_areas_mc_ind(
columns_names=columns_names,
)

return cast(pd.DataFrame, self._output_service.aggregate_values(self.name, aggregation_entry, "areas", "ind"))
return self._output_service.aggregate_values(self.name, aggregation_entry, "areas", "ind")

def aggregate_links_mc_ind(
self,
Expand All @@ -151,7 +152,7 @@ def aggregate_links_mc_ind(
columns_names=columns_names,
)

return cast(pd.DataFrame, self._output_service.aggregate_values(self.name, aggregation_entry, "links", "ind"))
return self._output_service.aggregate_values(self.name, aggregation_entry, "links", "ind")

def aggregate_areas_mc_all(
self,
Expand All @@ -178,7 +179,7 @@ def aggregate_areas_mc_all(
columns_names=columns_names,
)

return cast(pd.DataFrame, self._output_service.aggregate_values(self.name, aggregation_entry, "areas", "all"))
return self._output_service.aggregate_values(self.name, aggregation_entry, "areas", "all")

def aggregate_links_mc_all(
self,
Expand All @@ -205,4 +206,4 @@ def aggregate_links_mc_all(
columns_names=columns_names,
)

return cast(pd.DataFrame, self._output_service.aggregate_values(self.name, aggregation_entry, "links", "all"))
return self._output_service.aggregate_values(self.name, aggregation_entry, "links", "all")
9 changes: 5 additions & 4 deletions src/antares/craft/model/renewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
# This file is part of the Antares project.

from enum import Enum
from typing import Optional, cast
from typing import Optional

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

Expand Down Expand Up @@ -90,9 +91,9 @@ def yield_renewable_cluster_properties(self) -> RenewableClusterProperties:


class RenewableCluster:
def __init__( # type: ignore # TODO: Find a way to avoid circular imports
def __init__( # TODO: Find a way to avoid circular imports
self,
renewable_service,
renewable_service: BaseRenewableService,
area_id: str,
name: str,
properties: Optional[RenewableClusterProperties] = None,
Expand Down Expand Up @@ -126,7 +127,7 @@ def update_properties(self, properties: RenewableClusterProperties) -> None:
self._properties = new_properties

def get_timeseries(self) -> pd.DataFrame:
return cast(pd.DataFrame, self._renewable_service.get_renewable_matrix(self.id, self.area_id))
return 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)
Loading

0 comments on commit 4dc6b00

Please sign in to comment.