Skip to content

Commit 4b450da

Browse files
committed
Union to |
Signed-off-by: Santiago Figueroa Manrique <[email protected]>
1 parent aa0b210 commit 4b450da

File tree

16 files changed

+58
-59
lines changed

16 files changed

+58
-59
lines changed

src/power_grid_model_io/converters/pandapower_converter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import logging
1010
from functools import lru_cache
11-
from typing import Dict, List, MutableMapping, Optional, Tuple, Type, Union
11+
from typing import Dict, List, MutableMapping, Optional, Tuple, Type
1212

1313
import numpy as np
1414
import pandas as pd
@@ -2237,7 +2237,7 @@ def _generate_ids(self, pp_table: str, pp_idx: pd.Index, name: Optional[str] = N
22372237
def _get_pgm_ids(
22382238
self,
22392239
pp_table: str,
2240-
pp_idx: Optional[Union[pd.Series, np.ndarray]] = None,
2240+
pp_idx: Optional[pd.Series | np.ndarray] = None,
22412241
name: Optional[str] = None,
22422242
) -> pd.Series:
22432243
"""
@@ -2515,7 +2515,7 @@ def _get_pp_attr(
25152515
table: str,
25162516
attribute: str,
25172517
expected_type: Optional[str] = None,
2518-
default: Optional[Union[float, bool, str]] = None,
2518+
default: Optional[float | bool | str] = None,
25192519
) -> np.ndarray:
25202520
"""
25212521
Returns the selected PandaPower attribute from the selected PandaPower table.
@@ -2529,7 +2529,7 @@ def _get_pp_attr(
25292529
"""
25302530
pp_component_data = self.pp_input_data[table]
25312531

2532-
exp_dtype: Union[str, Type] = "O"
2532+
exp_dtype: str | Type = "O"
25332533
if expected_type is not None:
25342534
exp_dtype = expected_type
25352535
elif default is not None:
@@ -2569,7 +2569,7 @@ def get_id(self, pp_table: str, pp_idx: int, name: Optional[str] = None) -> int:
25692569
"""
25702570
return self.idx[(pp_table, name)][pp_idx]
25712571

2572-
def lookup_id(self, pgm_id: int) -> Dict[str, Union[str, int]]:
2572+
def lookup_id(self, pgm_id: int) -> Dict[str, str | int]:
25732573
"""
25742574
Retrieve the original name / key combination of a pgm object
25752575

src/power_grid_model_io/converters/pgm_json_converter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import warnings
1111
from enum import Enum
1212
from pathlib import Path
13-
from typing import Any, Dict, List, Optional, Union
13+
from typing import Any, Dict, List, Optional
1414

1515
import numpy as np
1616
from power_grid_model import ComponentType, DatasetType, initialize_array
@@ -45,8 +45,8 @@ class PgmJsonConverter(BaseConverter[StructuredData]):
4545

4646
def __init__(
4747
self,
48-
source_file: Optional[Union[Path, str]] = None,
49-
destination_file: Optional[Union[Path, str]] = None,
48+
source_file: Optional[Path | str] = None,
49+
destination_file: Optional[Path | str] = None,
5050
log_level: int = logging.INFO,
5151
):
5252
source = JsonFileStore(file_path=Path(source_file)) if source_file else None

src/power_grid_model_io/converters/tabular_converter.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import logging
1010
from enum import Enum
1111
from pathlib import Path
12-
from typing import Any, Collection, Dict, List, Mapping, Optional, Union, cast
12+
from typing import Any, Collection, Dict, List, Mapping, Optional, cast
1313

1414
import numpy as np
1515
import pandas as pd
@@ -146,9 +146,9 @@ def _parse_data(self, data: TabularData, data_type: DatasetType, extra_info: Opt
146146
def _convert_table_to_component( # pylint: disable = too-many-arguments,too-many-positional-arguments
147147
self,
148148
data: TabularData,
149-
data_type: Union[str, Enum],
149+
data_type: str | Enum,
150150
table: str,
151-
component: Union[str, Enum],
151+
component: str | Enum,
152152
attributes: InstanceAttributes,
153153
extra_info: Optional[ExtraInfo],
154154
) -> Optional[np.ndarray]:
@@ -166,9 +166,9 @@ def _convert_table_to_component( # pylint: disable = too-many-arguments,too-man
166166
extra_info: an optional dictionary where extra component info (that can't be specified in
167167
power-grid-model data) can be specified
168168
data: TabularData:
169-
data_type: Union[str, Enum]:
169+
data_type: str | Enum:
170170
table: str:
171-
component: Union[str, Enum]:
171+
component: str | Enum:
172172
attributes: InstanceAttributes:
173173
extra_info: Optional[ExtraInfo]:
174174
@@ -236,7 +236,7 @@ def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-m
236236
data: TabularData,
237237
pgm_data: np.ndarray,
238238
table: str,
239-
component: Union[str, Enum],
239+
component: str | Enum,
240240
attr: str,
241241
col_def: Any,
242242
table_mask: Optional[np.ndarray],
@@ -258,7 +258,7 @@ def _convert_col_def_to_attribute( # pylint: disable = too-many-arguments,too-m
258258
data: TabularData:
259259
pgm_data: np.ndarray:
260260
table: str:
261-
component: Union[str, Enum]:
261+
component: str | Enum:
262262
attr: str:
263263
col_def: Any:
264264
extra_info: Optional[ExtraInfo]:
@@ -428,16 +428,15 @@ def _parse_col_def( # pylint: disable = too-many-arguments,too-many-positional-
428428
def _parse_col_def_const(
429429
data: TabularData,
430430
table: str,
431-
col_def: Union[int, float],
431+
col_def: int | float,
432432
table_mask: Optional[np.ndarray] = None,
433433
) -> pd.DataFrame:
434434
"""Create a single column pandas DataFrame containing the const value.
435435
436436
Args:
437437
data: TabularData:
438438
table: str:
439-
col_def: Union[int:
440-
float]:
439+
col_def: int | float:
441440
442441
Returns:
443442
@@ -609,7 +608,7 @@ def _parse_auto_id( # pylint: disable = too-many-arguments,too-many-positional-
609608
table: str,
610609
ref_table: Optional[str],
611610
ref_name: Optional[str],
612-
key_col_def: Union[str, List[str], Dict[str, str]],
611+
key_col_def: str | List[str] | Dict[str, str],
613612
table_mask: Optional[np.ndarray],
614613
extra_info: Optional[ExtraInfo],
615614
) -> pd.DataFrame:
@@ -852,7 +851,7 @@ def get_id(row: pd.Series) -> int:
852851

853852
return keys.apply(get_id, axis=1).to_list()
854853

855-
def lookup_id(self, pgm_id: int) -> Dict[str, Union[str, Dict[str, int]]]:
854+
def lookup_id(self, pgm_id: int) -> Dict[str, str | Dict[str, int]]:
856855
"""
857856
Retrieve the original name / key combination of a pgm object
858857
Args:

src/power_grid_model_io/converters/vision_excel_converter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
from dataclasses import dataclass
1010
from pathlib import Path
11-
from typing import Any, Mapping, Optional, Union
11+
from typing import Any, Mapping, Optional
1212

1313
from power_grid_model_io.converters.tabular_converter import TabularConverter
1414
from power_grid_model_io.data_stores.base_data_store import LANGUAGE_EN
@@ -37,10 +37,10 @@ class VisionExcelConverter(TabularConverter):
3737

3838
def __init__( # pylint: disable=too-many-arguments,too-many-positional-arguments
3939
self,
40-
source_file: Optional[Union[Path, str]] = None,
40+
source_file: Optional[Path | str] = None,
4141
language: str = LANGUAGE_EN,
4242
terms_changed: Optional[dict] = None,
43-
mapping_file: Optional[Union[Path, str]] = None,
43+
mapping_file: Optional[Path | str] = None,
4444
log_level: int = logging.INFO,
4545
):
4646
_mapping_file = Path(

src/power_grid_model_io/data_stores/excel_file_store.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import re
99
from pathlib import Path
10-
from typing import Dict, List, Optional, Set, Tuple, Union
10+
from typing import Dict, List, Optional, Set, Tuple
1111

1212
import pandas as pd
1313

@@ -185,10 +185,10 @@ def _handle_duplicate_columns(self, data: pd.DataFrame, sheet_name: str) -> pd.D
185185

186186
return data
187187

188-
def _check_duplicate_values(self, sheet_name: str, data: pd.DataFrame) -> Dict[int, Union[str, Tuple[str, ...]]]:
188+
def _check_duplicate_values(self, sheet_name: str, data: pd.DataFrame) -> Dict[int, str | Tuple[str, ...]]:
189189
grouped = self._group_columns_by_index(data=data)
190190

191-
to_rename: Dict[int, Union[str, Tuple[str, ...]]] = {}
191+
to_rename: Dict[int, str | Tuple[str, ...]] = {}
192192

193193
for col_name, col_idxs in grouped.items():
194194
# No duplicate column names
@@ -251,8 +251,8 @@ def _update_column_names(self, data: pd.DataFrame) -> pd.DataFrame:
251251
return data
252252

253253
@staticmethod
254-
def _group_columns_by_index(data: pd.DataFrame) -> Dict[Union[str, Tuple[str, ...]], Set[int]]:
255-
grouped: Dict[Union[str, Tuple[str, ...]], Set[int]] = {}
254+
def _group_columns_by_index(data: pd.DataFrame) -> Dict[str | Tuple[str, ...], Set[int]]:
255+
grouped: Dict[str | Tuple[str, ...], Set[int]] = {}
256256
columns = data.columns.values
257257
for col_idx, col_name in enumerate(columns):
258258
if col_name[0] not in grouped:

src/power_grid_model_io/data_stores/json_file_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ def _validate(self, data: StructuredData) -> None:
105105
if len(type_names) == 1:
106106
type_str = type_names.pop()
107107
else:
108-
type_str = "Union[" + ", ".join(type_names) + "]"
108+
type_str = type_str = " | ".join(type_names)
109109
raise TypeError(f"Invalid data type for {type(self).__name__}: List[{type_str}]")

src/power_grid_model_io/data_types/_data_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Common data types used in the Power Grid Model project
66
"""
77

8-
from typing import Any, Dict, List, Union
8+
from typing import Any, Dict, List
99

1010
ExtraInfo = Dict[int, Any]
1111
"""
@@ -36,7 +36,7 @@
3636
Legacy type name; use ExtraInfo instead!
3737
"""
3838

39-
StructuredData = Union[Dict[str, List[Dict[str, Any]]], List[Dict[str, List[Dict[str, Any]]]]]
39+
StructuredData = Dict[str, List[Dict[str, Any]]] | List[Dict[str, List[Dict[str, Any]]]]
4040
"""
4141
Structured data is a multi dimensional structure (component_type -> objects -> attribute -> value) or a list of those
4242
dictionaries:

src/power_grid_model_io/data_types/tabular_data.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#
33
# SPDX-License-Identifier: MPL-2.0
44
"""
5-
The TabularData class is a wrapper around Dict[str, Union[pd.DataFrame, np.ndarray]],
5+
The TabularData class is a wrapper around Dict[str, pd.DataFrame | np.ndarray],
66
which supports unit conversions and value substitutions
77
"""
88

99
import logging
10-
from typing import Callable, Dict, Generator, Iterable, Optional, Tuple, Union
10+
from typing import Callable, Dict, Generator, Iterable, Optional, Tuple
1111

1212
import numpy as np
1313
import pandas as pd
@@ -21,11 +21,11 @@
2121

2222
class TabularData:
2323
"""
24-
The TabularData class is a wrapper around Dict[str, Union[pd.DataFrame, np.ndarray]],
24+
The TabularData class is a wrapper around Dict[str, pd.DataFrame | np.ndarray],
2525
which supports unit conversions and value substitutions
2626
"""
2727

28-
def __init__(self, logger=None, **tables: Union[pd.DataFrame, np.ndarray, LazyDataFrame]):
28+
def __init__(self, logger=None, **tables: pd.DataFrame | np.ndarray | LazyDataFrame):
2929
"""
3030
Tabular data can either be a collection of pandas DataFrames and/or numpy structured arrays.
3131
The key word arguments will define the keys of the data.
@@ -48,7 +48,7 @@ def __init__(self, logger=None, **tables: Union[pd.DataFrame, np.ndarray, LazyDa
4848
f"Invalid data type for table '{table_name}'; "
4949
f"expected a pandas DataFrame or NumPy array, got {type(table_data).__name__}."
5050
)
51-
self._data: Dict[str, Union[pd.DataFrame, np.ndarray, LazyDataFrame]] = tables
51+
self._data: Dict[str, pd.DataFrame | np.ndarray | LazyDataFrame] = tables
5252
self._units: Optional[UnitMapping] = None
5353
self._substitution: Optional[ValueMapping] = None
5454

@@ -181,7 +181,7 @@ def __contains__(self, table_name: str) -> bool:
181181
"""
182182
return table_name in self._data
183183

184-
def __getitem__(self, table_name: str) -> Union[pd.DataFrame, np.ndarray]:
184+
def __getitem__(self, table_name: str) -> pd.DataFrame | np.ndarray:
185185
"""
186186
Mimic the dictionary [] operator. It returns the 'raw' table data as stored in memory. This can be either a
187187
pandas DataFrame or a numpy structured array. It is possible that some unit conversions have been applied by
@@ -206,7 +206,7 @@ def keys(self) -> Iterable[str]:
206206

207207
return self._data.keys()
208208

209-
def items(self) -> Generator[Tuple[str, Union[pd.DataFrame, np.ndarray]], None, None]:
209+
def items(self) -> Generator[Tuple[str, pd.DataFrame | np.ndarray], None, None]:
210210
"""
211211
Mimic the dictionary .items() function
212212

src/power_grid_model_io/functions/filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
These functions can be used in the mapping files to apply filter functions to vision data
66
"""
77

8-
from typing import List, Union
8+
from typing import List
99

1010
import pandas as pd
1111

@@ -24,7 +24,7 @@ def exclude_empty(row: pd.Series, col: str) -> bool:
2424
return result
2525

2626

27-
def exclude_value(row: pd.Series, col: str, value: Union[float, str]) -> bool:
27+
def exclude_value(row: pd.Series, col: str, value: float | str) -> bool:
2828
"""
2929
filter out by match value
3030
"""

src/power_grid_model_io/mappings/tabular_mapping.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
Tabular data mapping helper class
66
"""
77

8-
from typing import Dict, Generator, List, Tuple, Union
8+
from typing import Dict, Generator, List, Tuple
99

1010
import structlog
1111

12-
AttributeValue = Union[int, float, str, Dict, List]
12+
AttributeValue = int | float | str | Dict | List
1313
InstanceAttributes = Dict[str, AttributeValue]
14-
Components = Dict[str, Union[InstanceAttributes, List[InstanceAttributes]]]
14+
Components = Dict[str, InstanceAttributes | List[InstanceAttributes]]
1515
Tables = Dict[str, Components]
1616

1717

0 commit comments

Comments
 (0)