Skip to content

Commit

Permalink
fix(mypy): enforce strict typing check (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvlecl authored Feb 10, 2025
1 parent 7f10ab2 commit 05bacae
Show file tree
Hide file tree
Showing 34 changed files with 377 additions and 348 deletions.
3 changes: 1 addition & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[mypy]
mypy_path = src
packages= antares
disallow_untyped_defs = true
disallow_untyped_calls = true
strict = true

61 changes: 34 additions & 27 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

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 @@ -55,14 +62,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 @@ -121,11 +128,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 @@ -151,7 +158,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 @@ -163,17 +170,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 @@ -189,18 +196,18 @@ def yield_area_ui(self) -> AreaUi:


class Area:
def __init__( # type: ignore # TODO: Find a way to avoid circular imports
def __init__(
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,
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 @@ -299,23 +306,23 @@ def get_reserves_matrix(self) -> pd.DataFrame:
def get_misc_gen_matrix(self) -> pd.DataFrame:
return 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 @@ -349,7 +356,7 @@ 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)
Expand All @@ -358,17 +365,17 @@ def create_hydro(

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

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

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

def read_hydro(
Expand Down
17 changes: 9 additions & 8 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, Dict, List, Optional, Union
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 @@ -76,12 +77,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 @@ -120,12 +121,12 @@ class BindingConstraintProperties(DefaultBindingConstraintProperties):


class BindingConstraint:
def __init__( # type: ignore # TODO: Find a way to avoid circular imports
def __init__(
self,
name: str,
binding_constraint_service,
binding_constraint_service: BaseBindingConstraintService,
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 @@ -149,10 +150,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 Down
11 changes: 6 additions & 5 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
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,7 +112,7 @@ 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:
Expand Down
5 changes: 3 additions & 2 deletions src/antares/craft/model/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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__(
self,
area_from: str,
area_to: str,
link_service,
link_service: BaseLinkService,
properties: Optional[LinkProperties] = None,
ui: Optional[LinkUi] = None,
):
Expand Down
5 changes: 3 additions & 2 deletions src/antares/craft/model/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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 Down
5 changes: 3 additions & 2 deletions src/antares/craft/model/renewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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__(
self,
renewable_service,
renewable_service: BaseRenewableService,
area_id: str,
name: str,
properties: Optional[RenewableClusterProperties] = None,
Expand Down
37 changes: 18 additions & 19 deletions src/antares/craft/model/st_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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
Expand Down Expand Up @@ -90,12 +91,18 @@ def yield_st_storage_properties(self) -> STStorageProperties:


class STStorage:
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._name = name
self._id = transform_name_to_id(name)
self._properties = properties or STStorageProperties()
def __init__(
self,
storage_service: BaseShortTermStorageService,
area_id: str,
name: str,
properties: Optional[STStorageProperties] = None,
):
self._area_id: str = area_id
self._storage_service: BaseShortTermStorageService = storage_service
self._name: str = name
self._id: str = transform_name_to_id(name)
self._properties: STStorageProperties = properties or STStorageProperties()

# TODO: Add matrices.

Expand Down Expand Up @@ -135,24 +142,16 @@ def get_storage_inflows(self) -> pd.DataFrame:
return 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)
Loading

0 comments on commit 05bacae

Please sign in to comment.