Skip to content

Commit fd4a52b

Browse files
authored
Merge pull request PowerGridModel#375 from furqan463/update_ruff
Update Ruff Rules (PT)
2 parents 218ee9c + f98a0f4 commit fd4a52b

18 files changed

+126
-122
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ select = [
164164
# raise vanilla args
165165
"TRY002",
166166
# pytest-style
167-
#"PT",
167+
"PT",
168168
]
169169
ignore = []
170170

tests/unit/converters/test_pandapower_converter_input.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,12 @@ def create_input_data():
125125
create_input_data_mock.assert_called_once_with()
126126
fill_pgm_extra_info_mock.assert_not_called()
127127
fill_pp_extra_info_mock.assert_not_called()
128-
assert len(converter.pp_input_data) == 1 and "bus" in converter.pp_input_data
129-
assert len(converter.pgm_input_data) == 1 and ComponentType.node in converter.pgm_input_data
130-
assert len(result) == 1 and ComponentType.node in result
128+
assert len(converter.pp_input_data) == 1
129+
assert "bus" in converter.pp_input_data
130+
assert len(converter.pgm_input_data) == 1
131+
assert ComponentType.node in converter.pgm_input_data
132+
assert len(result) == 1
133+
assert ComponentType.node in result
131134

132135

133136
@patch("power_grid_model_io.converters.pandapower_converter.PandaPowerConverter._fill_pgm_extra_info")
@@ -155,7 +158,7 @@ def test_parse_data__update_data():
155158
converter = PandaPowerConverter()
156159

157160
# Act/Assert
158-
with pytest.raises(ValueError):
161+
with pytest.raises(ValueError, match=r"Data type: 'update' is not implemented"):
159162
converter._parse_data(data={}, data_type=DatasetType.update, extra_info=None)
160163

161164

@@ -265,9 +268,12 @@ def create_output_data():
265268
create_output_data_mock.assert_called_once_with()
266269
extra_info_to_idx_lookup.assert_not_called()
267270
extra_info_pgm_input_data.assert_not_called()
268-
assert len(converter.pp_output_data) == 1 and "res_line" in converter.pp_output_data
269-
assert len(converter.pgm_output_data) == 1 and ComponentType.line in converter.pgm_output_data
270-
assert len(result) == 1 and "res_line" in result
271+
assert len(converter.pp_output_data) == 1
272+
assert "res_line" in converter.pp_output_data
273+
assert len(converter.pgm_output_data) == 1
274+
assert ComponentType.line in converter.pgm_output_data
275+
assert len(result) == 1
276+
assert "res_line" in result
271277

272278

273279
@patch("power_grid_model_io.converters.pandapower_converter.PandaPowerConverter._extra_info_to_idx_lookup")
@@ -292,9 +298,12 @@ def create_output_data_3ph():
292298
create_output_data_3ph_mock.assert_called_once_with()
293299
extra_info_to_idx_lookup.assert_not_called()
294300
extra_info_pgm_input_data.assert_not_called()
295-
assert len(converter.pp_output_data) == 1 and "res_line_3ph" in converter.pp_output_data
296-
assert len(converter.pgm_output_data) == 1 and ComponentType.line in converter.pgm_output_data
297-
assert len(result) == 1 and "res_line_3ph" in result
301+
assert len(converter.pp_output_data) == 1
302+
assert "res_line_3ph" in converter.pp_output_data
303+
assert len(converter.pgm_output_data) == 1
304+
assert ComponentType.line in converter.pgm_output_data
305+
assert len(result) == 1
306+
assert "res_line_3ph" in result
298307

299308

300309
def test__serialize_data__invalid_output():
@@ -1786,7 +1795,6 @@ def test_create_pgm_input_motors__existing_loads() -> None:
17861795
PandaPowerConverter._create_pgm_input_transformers,
17871796
PandaPowerConverter._create_pgm_input_wards,
17881797
PandaPowerConverter._create_pgm_input_xwards,
1789-
PandaPowerConverter._create_pgm_input_sources,
17901798
PandaPowerConverter._create_pgm_input_generators,
17911799
PandaPowerConverter._create_pgm_input_dclines,
17921800
],
@@ -2026,7 +2034,7 @@ def test_get_winding_types__value_error():
20262034
converter.pp_input_data = {"trafo": pd.DataFrame([(1, "ADyn")], columns=["id", "vector_group"])}
20272035

20282036
# Act / Assert
2029-
with pytest.raises(ValueError):
2037+
with pytest.raises(ValueError, match=r"Invalid transformer connection string: 'ADyn'"):
20302038
converter.get_trafo_winding_types()
20312039

20322040

@@ -2036,7 +2044,7 @@ def test_get_trafo3w_winding_types__value_error():
20362044
converter.pp_input_data = {"trafo3w": pd.DataFrame([(1, "ADyndrr")], columns=["id", "vector_group"])}
20372045

20382046
# Act / Assert
2039-
with pytest.raises(ValueError):
2047+
with pytest.raises(ValueError, match=r"Invalid three winding transformer connection string: 'ADyndrr'"):
20402048
converter.get_trafo3w_winding_types()
20412049

20422050

tests/unit/converters/test_pandapower_converter_output.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ def test_pp_switch_output():
834834

835835

836836
@patch("power_grid_model_io.converters.pandapower_converter.np.matmul")
837-
def test_output_bus_3ph(_mock_np_array: MagicMock, converter):
837+
def test_output_bus_3ph(mock_np_array: MagicMock, converter):
838838
# Arrange
839839
mock_pgm_array = MagicMock()
840840
converter.pgm_output_data[ComponentType.node] = mock_pgm_array
@@ -867,6 +867,7 @@ def test_output_bus_3ph(_mock_np_array: MagicMock, converter):
867867

868868
# result
869869
converter.pp_output_data.__setitem__.assert_called_once_with("res_bus_3ph", mock_pp_df.return_value)
870+
mock_np_array.assert_called_once()
870871

871872

872873
def test_output_line_3ph(converter):

tests/unit/converters/test_tabular_converter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_convert_table_to_component(converter: TabularConverter, tabular_data_no
168168
"power_grid_model_io.converters.tabular_converter.TabularConverter._parse_table_filters",
169169
)
170170
def test_convert_table_to_component__filters(
171-
_parse_table_filters: MagicMock,
171+
parse_table_filters: MagicMock,
172172
converter: TabularConverter,
173173
tabular_data_no_units_no_substitutions: TabularData,
174174
):
@@ -185,7 +185,7 @@ def test_convert_table_to_component__filters(
185185
attributes=node_attributes_with_filter,
186186
extra_info=None,
187187
)
188-
_parse_table_filters.assert_called_once_with(
188+
parse_table_filters.assert_called_once_with(
189189
data=tabular_data_no_units_no_substitutions,
190190
table="nodes",
191191
filtering_functions=node_attributes_with_filter["filters"],
@@ -200,8 +200,8 @@ def test_convert_table_to_component__filters(
200200
side_effect=np.array([False, False]),
201201
)
202202
def test_convert_table_to_component__filters_all_false(
203-
_parse_table_filters: MagicMock,
204-
_convert_col_def_to_attribute: MagicMock,
203+
parse_table_filters: MagicMock,
204+
convert_col_def_to_attribute: MagicMock,
205205
converter: TabularConverter,
206206
tabular_data_no_units_no_substitutions: TabularData,
207207
):
@@ -220,12 +220,12 @@ def test_convert_table_to_component__filters_all_false(
220220
)
221221

222222
assert actual is None
223-
_parse_table_filters.assert_called_once_with(
223+
parse_table_filters.assert_called_once_with(
224224
data=tabular_data_no_units_no_substitutions,
225225
table="nodes",
226226
filtering_functions=node_attributes_with_filter["filters"],
227227
)
228-
_convert_col_def_to_attribute.assert_not_called()
228+
convert_col_def_to_attribute.assert_not_called()
229229

230230

231231
def test_convert_col_def_to_attribute(

tests/unit/data_stores/test_csv_dir_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from power_grid_model_io.data_types import TabularData
1515

1616

17-
@pytest.fixture()
17+
@pytest.fixture
1818
def temp_dir():
1919
with tempfile.TemporaryDirectory() as tmp:
2020
yield Path(tmp).resolve()

tests/unit/data_stores/test_excel_file_store.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import numpy as np
99
import pandas as pd
1010
import pytest
11-
from pytest import param
1211
from structlog.testing import capture_logs
1312

1413
from power_grid_model_io.data_stores.excel_file_store import ExcelFileStore
@@ -18,15 +17,15 @@
1817
PandasExcelData = dict[str, pd.DataFrame]
1918

2019

21-
@pytest.fixture()
20+
@pytest.fixture
2221
def objects_excel() -> PandasExcelData:
2322
return {
2423
"Nodes": pd.DataFrame([["A", 111.0], ["B", 222.0]], columns=("NAME", "U_NOM")),
2524
"Lines": pd.DataFrame([["C", "A", "B", "X"]], columns=("NAME", "NODE0", "NODE1", "TYPE")),
2625
}
2726

2827

29-
@pytest.fixture()
28+
@pytest.fixture
3029
def specs_excel() -> PandasExcelData:
3130
return {
3231
"Colors": pd.DataFrame([["A", "Red"], ["B", "Yellow"]], columns=("NODE", "COLOR")),
@@ -233,11 +232,11 @@ def test_save__multiple_files(mock_to_excel: MagicMock, mock_excel_writer):
233232
@pytest.mark.parametrize(
234233
("column_name", "is_unnamed"),
235234
[
236-
param("", False, id="empty"),
237-
param("id", False, id="id"),
238-
param("Unnamed: 123_level_1", True, id="unnamed"),
239-
param("Unnamed", False, id="not_unnamed"),
240-
param("123", False, id="number_as_str"),
235+
pytest.param("", False, id="empty"),
236+
pytest.param("id", False, id="id"),
237+
pytest.param("Unnamed: 123_level_1", True, id="unnamed"),
238+
pytest.param("Unnamed", False, id="not_unnamed"),
239+
pytest.param("123", False, id="number_as_str"),
241240
],
242241
)
243242
def test_unnamed_pattern(column_name: str, is_unnamed: bool):

tests/unit/data_stores/test_json_file_store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from power_grid_model_io.utils.json import JsonEncoder
1212

1313

14-
@pytest.fixture()
14+
@pytest.fixture
1515
def single_data() -> StructuredData:
1616
return {"node": [{"id": 0, "u_rated": 10.5e3}]}
1717

1818

19-
@pytest.fixture()
19+
@pytest.fixture
2020
def batch_data() -> StructuredData:
2121
return [{"source": [{"id": 1, "p_specified": 1e6}]}, {"source": [{"id": 1, "p_specified": 2e6}]}]
2222

tests/unit/data_types/test_tabular_data.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,36 @@
1515
from tests.utils import assert_log_exists
1616

1717

18-
@pytest.fixture()
18+
@pytest.fixture
1919
def nodes() -> pd.DataFrame:
2020
return pd.DataFrame([(0, 150e3), (1, 10.5e3), (2, 400.0)], columns=["id", "u_rated"])
2121

2222

23-
@pytest.fixture()
23+
@pytest.fixture
2424
def nodes_iso() -> pd.DataFrame:
2525
return pd.DataFrame(
2626
[(0, 150e3), (1, 10.5e3), (2, 400.0)], columns=pd.MultiIndex.from_tuples((("id", ""), ("u_rated", "V")))
2727
)
2828

2929

30-
@pytest.fixture()
30+
@pytest.fixture
3131
def nodes_kv() -> pd.DataFrame:
3232
return pd.DataFrame(
3333
[(0, 150), (1, 10.5), (2, 0.4)], columns=pd.MultiIndex.from_tuples((("id", ""), ("u_rated", "kV")))
3434
)
3535

3636

37-
@pytest.fixture()
37+
@pytest.fixture
3838
def nodes_vl() -> pd.DataFrame:
3939
return pd.DataFrame([(0, "hv"), (1, "mv"), (2, "lv")], columns=("id", "u_rated"))
4040

4141

42-
@pytest.fixture()
42+
@pytest.fixture
4343
def nodes_np() -> np.ndarray:
4444
return np.array([(0, 150e3), (1, 10.5e3), (2, 400.0)], dtype=[("id", "i4"), ("u_rated", "f4")])
4545

4646

47-
@pytest.fixture()
47+
@pytest.fixture
4848
def lines() -> pd.DataFrame:
4949
return pd.DataFrame([(2, 0, 1), (3, 2, 3)], columns=("id", "from_node", "to_node"))
5050

tests/unit/functions/test_functions.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from unittest.mock import MagicMock, patch
55

66
import numpy as np
7+
import pytest
78
from power_grid_model import WindingType
8-
from pytest import approx, mark
99

1010
from power_grid_model_io.functions import (
1111
both_zeros_to_nan,
@@ -20,7 +20,7 @@
2020
)
2121

2222

23-
@mark.parametrize(
23+
@pytest.mark.parametrize(
2424
("value", "expected"),
2525
[
2626
(None, False),
@@ -37,7 +37,7 @@
3737
],
3838
)
3939
def test_has_value(value, expected):
40-
assert has_value(value) == approx(expected)
40+
assert has_value(value) == pytest.approx(expected)
4141

4242

4343
@patch("power_grid_model_io.functions.has_value")
@@ -58,7 +58,7 @@ def test_value_or_zero(mock_value_or_default: MagicMock):
5858
mock_value_or_default.assert_called_once_with(value=1.23, default=0.0)
5959

6060

61-
@mark.parametrize(
61+
@pytest.mark.parametrize(
6262
("real", "imag", "expected"),
6363
[
6464
(float("nan"), float("nan"), float("nan")),
@@ -70,10 +70,10 @@ def test_value_or_zero(mock_value_or_default: MagicMock):
7070
)
7171
def test_complex_inverse_real_part(real: float, imag: float, expected: float):
7272
actual = complex_inverse_real_part(real, imag)
73-
assert actual == approx(expected) or (np.isnan(actual) and np.isnan(expected))
73+
assert actual == pytest.approx(expected) or (np.isnan(actual) and np.isnan(expected))
7474

7575

76-
@mark.parametrize(
76+
@pytest.mark.parametrize(
7777
("real", "imag", "expected"),
7878
[
7979
(float("nan"), float("nan"), float("nan")),
@@ -85,10 +85,10 @@ def test_complex_inverse_real_part(real: float, imag: float, expected: float):
8585
)
8686
def test_complex_inverse_imaginary_part(real: float, imag: float, expected: float):
8787
actual = complex_inverse_imaginary_part(real, imag)
88-
assert actual == approx(expected) or (np.isnan(actual) and np.isnan(expected))
88+
assert actual == pytest.approx(expected) or (np.isnan(actual) and np.isnan(expected))
8989

9090

91-
@mark.parametrize(
91+
@pytest.mark.parametrize(
9292
("winding", "neutral_grounding", "expected"),
9393
[
9494
("Y", True, WindingType.wye),
@@ -118,7 +118,7 @@ def test_get_winding(winding: str, neutral_grounding: bool, expected: WindingTyp
118118
assert actual == expected
119119

120120

121-
@mark.parametrize(
121+
@pytest.mark.parametrize(
122122
("degrees", "expected"),
123123
[
124124
(360, 0),
@@ -129,10 +129,10 @@ def test_get_winding(winding: str, neutral_grounding: bool, expected: WindingTyp
129129
)
130130
def test_degrees_to_clock(degrees: float, expected: int):
131131
actual = degrees_to_clock(degrees)
132-
assert actual == approx(expected) or (np.isnan(actual) and np.isnan(expected))
132+
assert actual == pytest.approx(expected) or (np.isnan(actual) and np.isnan(expected))
133133

134134

135-
@mark.parametrize(
135+
@pytest.mark.parametrize(
136136
("left_side", "right_side", "expected"),
137137
[
138138
(float("nan"), float("nan"), False),
@@ -146,7 +146,7 @@ def test_is_greater_than(left_side: float, right_side: list[float], expected: fl
146146
assert actual == expected
147147

148148

149-
@mark.parametrize(
149+
@pytest.mark.parametrize(
150150
("value", "other_value", "expected"),
151151
[
152152
(float("nan"), float("nan"), float("nan")),
@@ -162,4 +162,4 @@ def test_is_greater_than(left_side: float, right_side: list[float], expected: fl
162162
)
163163
def test_both_zeros_to_nan(value: float, other_value: float, expected: float):
164164
actual = both_zeros_to_nan(value, other_value)
165-
assert actual == approx(expected) or (np.isnan(actual) and np.isnan(expected))
165+
assert actual == pytest.approx(expected) or (np.isnan(actual) and np.isnan(expected))

0 commit comments

Comments
 (0)