Skip to content

Commit 368ad60

Browse files
committed
add + resolve PGM mypy support
Signed-off-by: Martijn Govers <[email protected]>
1 parent 2f566ef commit 368ad60

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

.github/workflows/check-code-quality.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ jobs:
5353
run: |
5454
pip install pylint .
5555
pylint power_grid_model_io
56+
57+
- name: If needed raise error
58+
run: |
59+
60+
if [[ `git status --porcelain --untracked-files=no` ]]; then
61+
echo "Formatting not correct! See below the files which need to be reformatted!"
62+
git status --porcelain --untracked-files=no
63+
exit 1
64+
fi

src/power_grid_model_io/converters/pandapower_converter.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
import numpy as np
1313
import pandas as pd
1414
import structlog
15-
from power_grid_model import Branch3Side, BranchSide, LoadGenType, WindingType, initialize_array, power_grid_meta_data
15+
from power_grid_model import (
16+
Branch3Side,
17+
BranchSide,
18+
DatasetType,
19+
LoadGenType,
20+
WindingType,
21+
initialize_array,
22+
power_grid_meta_data,
23+
)
1624
from power_grid_model.data_types import Dataset, SingleDataset
1725

1826
from power_grid_model_io.converters.base_converter import BaseConverter
@@ -113,10 +121,10 @@ def _serialize_data(self, data: Dataset, extra_info: Optional[ExtraInfo]) -> Pan
113121
self._extra_info_to_pgm_input_data(extra_info)
114122

115123
# Convert
116-
def pgm_output_dtype_checker(check_type: str) -> bool:
124+
def pgm_output_dtype_checker(check_type: DatasetType | str) -> bool:
117125
return all(
118126
(
119-
comp_array.dtype == power_grid_meta_data[check_type][component]
127+
comp_array.dtype == power_grid_meta_data[DatasetType[check_type]][component]
120128
for component, comp_array in self.pgm_output_data.items()
121129
)
122130
)
@@ -248,7 +256,10 @@ def _extra_info_to_pgm_input_data(self, extra_info: ExtraInfo): # pylint: disab
248256
nan = np.iinfo(dtype).min
249257
all_other_cols = ["i_n"]
250258
for component, data in self.pgm_output_data.items():
251-
input_cols = power_grid_meta_data["input"][component].dtype.names
259+
input_cols = power_grid_meta_data[DatasetType.input][component].dtype.names
260+
if input_cols is None:
261+
input_cols = tuple()
262+
252263
node_cols = [col for col in input_cols if is_node_ref(col)]
253264
other_cols = [col for col in input_cols if col in all_other_cols]
254265
if not node_cols + other_cols:
@@ -1594,9 +1605,11 @@ def join_currents(table: str, bus_name: str, i_name: str) -> pd.DataFrame:
15941605
# Combine all branch bus, current and et in one dataframe
15951606
all_i_df = pd.concat(
15961607
[
1597-
join_currents(table, bus_name, i_name)
1598-
if not rest_switches_absent[table]
1599-
else pd.DataFrame(columns=["bus", "element", "et", "i_ka"])
1608+
(
1609+
join_currents(table, bus_name, i_name)
1610+
if not rest_switches_absent[table]
1611+
else pd.DataFrame(columns=["bus", "element", "et", "i_ka"])
1612+
)
16001613
for table, attr_names in switch_attrs.items()
16011614
for bus_name, i_name in attr_names.items()
16021615
]

src/power_grid_model_io/converters/pgm_json_converter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ def _parse_component(
151151

152152
# If an attribute doesn't exist, it is added to the extra_info lookup table
153153
elif extra_info is not None:
154-
if obj["id"] not in extra_info:
155-
extra_info[obj["id"]] = {}
156-
extra_info[obj["id"]][attribute] = value
154+
obj_id = obj["id"]
155+
if not isinstance(obj_id, int):
156+
raise ValueError(f"Invalid 'id' value for {component} {data_type} data")
157+
if obj_id not in extra_info:
158+
extra_info[obj_id] = {}
159+
extra_info[obj_id][attribute] = value
157160
return array
158161

159162
def _serialize_data(self, data: Dataset, extra_info: Optional[ExtraInfo]) -> StructuredData:
@@ -271,7 +274,11 @@ def _extract_extra_info(
271274
self._extract_extra_component_info(component, entry, reserialized_data, extra_info)
272275

273276
def _extract_extra_component_info(
274-
self, component: str, attributes: Dict[str, Any], reserialized_data: SingleDataset, extra_info: ExtraInfo
277+
self,
278+
component: str,
279+
attributes: Dict[str, Any],
280+
reserialized_data: Dict[str, List[Dict[str, Any]]],
281+
extra_info: ExtraInfo,
275282
):
276283
entry_id = attributes["id"]
277284
reserialized_entry = self._get_first_by(reserialized_data[component], "id", entry_id)

src/power_grid_model_io/converters/tabular_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def _parse_table_filters(self, data: TabularData, table: str, filtering_function
221221
for filtering_fn in filtering_functions:
222222
for fn_name, kwargs in filtering_fn.items():
223223
fn_ptr = get_function(fn_name)
224-
table_mask &= data[table].apply(fn_ptr, axis=1, **kwargs).values
224+
table_mask &= cast(pd.DataFrame, data[table]).apply(fn_ptr, axis=1, **kwargs).values
225225
return table_mask
226226

227227
# pylint: disable = too-many-arguments

0 commit comments

Comments
 (0)