Skip to content

Commit

Permalink
Enforce type annotations (#307)
Browse files Browse the repository at this point in the history
Add missing type annotations, and enforce type definitions through mypy check.
  • Loading branch information
sylvlecl authored Feb 8, 2022
1 parent fc7982f commit 42548ce
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 153 deletions.
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[mypy]
disallow_untyped_defs = True

[mypy-networkx.*]
ignore_missing_imports = True
Expand Down
19 changes: 5 additions & 14 deletions pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class ContingencyContextType:
def name(self) -> str: ...

class ContingencyResult:
def __init__(self, *args, **kwargs) -> None: ...
@property
def contingency_id(self) -> str: ...
@property
Expand All @@ -120,7 +119,6 @@ class ContingencyResult:
def status(self) -> LoadFlowComponentStatus: ...

class ContingencyResultArray:
def __init__(self, *args, **kwargs) -> None: ...
def __iter__(self) -> Iterator: ...
def __len__(self) -> int: ...

Expand Down Expand Up @@ -181,7 +179,7 @@ class FilterAttributesType:
def name(self) -> str: ...

class JavaHandle:
def __init__(self, *args, **kwargs) -> None: ...
...

class LimitType:
__members__: ClassVar[dict] = ... # read-only
Expand All @@ -201,7 +199,6 @@ class LimitType:
def name(self) -> str: ...

class LimitViolation:
def __init__(self, *args, **kwargs) -> None: ...
@property
def acceptable_duration(self) -> int: ...
@property
Expand All @@ -222,12 +219,10 @@ class LimitViolation:
def value(self) -> float: ...

class LimitViolationArray:
def __init__(self, *args, **kwargs) -> None: ...
def __iter__(self) -> Iterator: ...
def __len__(self) -> int: ...

class LoadFlowComponentResult:
def __init__(self, *args, **kwargs) -> None: ...
@property
def connected_component_num(self) -> int: ...
@property
Expand All @@ -242,7 +237,6 @@ class LoadFlowComponentResult:
def synchronous_component_num(self) -> int: ...

class LoadFlowComponentResultArray:
def __init__(self, *args, **kwargs) -> None: ...
def __iter__(self) -> Iterator: ...
def __len__(self) -> int: ...

Expand Down Expand Up @@ -281,10 +275,9 @@ class LoadFlowParameters:
def __init__(self) -> None: ...

class Matrix:
def __init__(self, *args, **kwargs) -> None: ...
...

class NetworkMetadata:
def __init__(self, *args, **kwargs) -> None: ...
@property
def case_date(self) -> float: ...
@property
Expand All @@ -299,7 +292,6 @@ class NetworkMetadata:
class PyPowsyblError(Exception): ...

class Series:
def __init__(self, *args, **kwargs) -> None: ...
@property
def data(self) -> object: ...
@property
Expand All @@ -308,7 +300,6 @@ class Series:
def name(self) -> str: ...

class SeriesArray:
def __init__(self, *args, **kwargs) -> None: ...
def __iter__(self) -> Iterator: ...
def __len__(self) -> int: ...

Expand Down Expand Up @@ -386,8 +377,8 @@ def add_contingency(analysis_context: JavaHandle, contingency_id: str, elements_
def add_monitored_elements(security_analysis_context: JavaHandle, contingency_context_type: ContingencyContextType, branch_ids: List[str], voltage_level_ids: List[str], three_windings_transformer_ids: List[str], contingency_ids: List[str]) -> None: ...
def clone_variant(network: JavaHandle, src: str, variant: str, may_overwrite: bool) -> None: ...
def create_dataframe(columns_values: list, columns_names: List[str], columns_types: List[int], is_index: List[bool]) -> ArrayStruct: ...
def create_exporter_parameters_series_array(*args, **kwargs) -> Any: ...
def create_importer_parameters_series_array(*args, **kwargs) -> Any: ...
def create_exporter_parameters_series_array(format: str) -> SeriesArray: ...
def create_importer_parameters_series_array(format: str) -> SeriesArray: ...
def create_network(name: str, id: str) -> JavaHandle: ...
def create_network_elements_series_array(network: JavaHandle, element_type: ElementType, filter_attributes_type: FilterAttributesType, attributes: List[str]) -> SeriesArray: ...
def create_security_analysis() -> JavaHandle: ...
Expand Down Expand Up @@ -426,7 +417,7 @@ def merge(arg0: JavaHandle, arg1: List[JavaHandle]) -> None: ...
def reduce_network(network: JavaHandle, v_min: float, v_max: float, ids: List[str], vls: List[str], depths: List[int], with_dangling_lines: bool) -> None: ...
def remove_variant(network: JavaHandle, variant: str) -> None: ...
def run_load_flow(network: JavaHandle, dc: bool, parameters: LoadFlowParameters, provider: str) -> LoadFlowComponentResultArray: ...
def run_load_flow_validation(*args, **kwargs) -> Any: ...
def run_load_flow_validation(network: JavaHandle, validation_type: ValidationType) -> SeriesArray: ...
def run_security_analysis(security_analysis_context: JavaHandle, network: JavaHandle, parameters: LoadFlowParameters, provider: str) -> JavaHandle: ...
def run_sensitivity_analysis(sensitivity_analysis_context: JavaHandle, network: JavaHandle, dc: bool, parameters: LoadFlowParameters, provider: str) -> JavaHandle: ...
def set_branch_flow_factor_matrix(sensitivity_analysis_context: JavaHandle, branches_ids: List[str], variables_ids: List[str]) -> None: ...
Expand Down
47 changes: 26 additions & 21 deletions pypowsybl/loadflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from pypowsybl.util import create_data_frame_from_series_array as _create_data_frame_from_series_array
from typing import (
List as _List,
Sequence as _Sequence
Sequence as _Sequence,
Optional as _Optional
)

# enforcing some class metadata on classes imported from C extension,
Expand All @@ -40,36 +41,36 @@ def __init__(self, res: _pypowsybl.LoadFlowComponentResult):
self._res = res

@property
def status(self):
def status(self) -> ComponentStatus:
"""Status of the loadflow for this component."""
return self._res.status

@property
def connected_component_num(self):
def connected_component_num(self) -> int:
"""Number of the connected component."""
return self._res.connected_component_num

@property
def synchronous_component_num(self):
def synchronous_component_num(self) -> int:
"""Number of the synchronous component."""
return self._res.synchronous_component_num

@property
def iteration_count(self):
def iteration_count(self) -> int:
"""The number of iterations performed by the loadflow."""
return self._res.iteration_count

@property
def slack_bus_id(self):
def slack_bus_id(self) -> str:
"""ID of the slack bus used for this component."""
return self._res.slack_bus_id

@property
def slack_bus_active_power_mismatch(self):
def slack_bus_active_power_mismatch(self) -> float:
"""Remaining active power slack at the end of the loadflow"""
return self._res.slack_bus_active_power_mismatch

def __repr__(self):
def __repr__(self) -> str:
return f"{self.__class__.__name__}("\
f"connected_component_num={self.connected_component_num!r}"\
f", synchronous_component_num={self.synchronous_component_num!r}"\
Expand Down Expand Up @@ -167,7 +168,7 @@ def __init__(self,
if connected_component_mode is not None:
self.connected_component_mode = connected_component_mode

def __repr__(self):
def __repr__(self) -> str:
return f"{self.__class__.__name__}("\
f"voltage_init_mode={self.voltage_init_mode.name}"\
f", transformer_voltage_control_on={self.transformer_voltage_control_on!r}"\
Expand All @@ -185,7 +186,7 @@ def __repr__(self):
f")"


def run_ac(network: _Network, parameters: Parameters = None, provider='OpenLoadFlow') -> _List[ComponentResult]:
def run_ac(network: _Network, parameters: Parameters = None, provider : str = 'OpenLoadFlow') -> _List[ComponentResult]:
"""
Run an AC loadflow on a network.
Expand All @@ -201,7 +202,7 @@ def run_ac(network: _Network, parameters: Parameters = None, provider='OpenLoadF
return [ComponentResult(res) for res in _pypowsybl.run_load_flow(network._handle, False, p, provider)]


def run_dc(network: _Network, parameters: Parameters = None, provider='OpenLoadFlow') -> _List[ComponentResult]:
def run_dc(network: _Network, parameters: Parameters = None, provider : str ='OpenLoadFlow') -> _List[ComponentResult]:
"""
Run a DC loadflow on a network.
Expand All @@ -221,12 +222,16 @@ def run_dc(network: _Network, parameters: Parameters = None, provider='OpenLoadF
ValidationType.SVCS, ValidationType.TWTS, ValidationType.TWTS3W]


_OptionalDf = _Optional[_DataFrame]


class ValidationResult:
"""
The result of a loadflow validation.
"""

def __init__(self, branch_flows, buses, generators, svcs, shunts, twts, t3wts):
def __init__(self, branch_flows: _OptionalDf, buses: _OptionalDf, generators: _OptionalDf, svcs: _OptionalDf,
shunts: _OptionalDf, twts: _OptionalDf, t3wts: _OptionalDf):
self._branch_flows = branch_flows
self._buses = buses
self._generators = generators
Expand All @@ -240,60 +245,60 @@ def __init__(self, branch_flows, buses, generators, svcs, shunts, twts, t3wts):
and self._is_valid_or_unchecked(self.t3wts)

@staticmethod
def _is_valid_or_unchecked(df: _DataFrame) -> bool:
def _is_valid_or_unchecked(df: _OptionalDf) -> bool:
return df is None or df['validated'].all()

@property
def branch_flows(self) -> _DataFrame:
def branch_flows(self) -> _OptionalDf:
"""
Validation results for branch flows.
"""
return self._branch_flows

@property
def buses(self) -> _DataFrame:
def buses(self) -> _OptionalDf:
"""
Validation results for buses.
"""
return self._buses

@property
def generators(self) -> _DataFrame:
def generators(self) -> _OptionalDf:
"""
Validation results for generators.
"""
return self._generators

@property
def svcs(self) -> _DataFrame:
def svcs(self) -> _OptionalDf:
"""
Validation results for SVCs.
"""
return self._svcs

@property
def shunts(self) -> _DataFrame:
def shunts(self) -> _OptionalDf:
"""
Validation results for shunts.
"""
return self._shunts

@property
def twts(self) -> _DataFrame:
def twts(self) -> _OptionalDf:
"""
Validation results for two winding transformers.
"""
return self._twts

@property
def t3wts(self) -> _DataFrame:
def t3wts(self) -> _OptionalDf:
"""
Validation results for three winding transformers.
"""
return self._t3wts

@property
def valid(self):
def valid(self) -> bool:
"""
True if all checked data is valid.
"""
Expand Down
Loading

0 comments on commit 42548ce

Please sign in to comment.