Skip to content

Commit

Permalink
Merge branch 'main' into feature/optional-id-refactor-part2
Browse files Browse the repository at this point in the history
  • Loading branch information
figueroa1395 authored Nov 15, 2024
2 parents cc76897 + e31e05c commit 1a89659
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 80 deletions.
13 changes: 6 additions & 7 deletions code_generation/meta_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# define dataclass for meta data

from dataclasses import dataclass
from typing import Optional

from dataclasses_json import DataClassJsonMixin

Expand All @@ -15,19 +14,19 @@ class Attribute(DataClassJsonMixin):
data_type: str
names: str | list[str]
description: str
nan_value: Optional[str] = None
nan_value: str | None = None


@dataclass
class AttributeClass(DataClassJsonMixin):
name: str
attributes: list[Attribute]
full_attributes: Optional[list[Attribute]] = None
base: Optional[str] = None
full_attributes: list[Attribute] | None = None
base: str | None = None
is_template: bool = False
full_name: Optional[str] = None
specification_names: Optional[list[str]] = None
base_attributes: Optional[dict[str, list[Attribute]]] = None
full_name: str | None = None
specification_names: list[str] | None = None
base_attributes: dict[str, list[Attribute]] | None = None


@dataclass
Expand Down
11 changes: 5 additions & 6 deletions src/power_grid_model/_core/error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""

import re
from typing import Optional

import numpy as np

Expand Down Expand Up @@ -115,7 +114,7 @@ def _interpret_error(message: str, decode_error: bool = True) -> PowerGridError:
return PowerGridError(message)


def find_error(batch_size: int = 1, decode_error: bool = True) -> Optional[RuntimeError]:
def find_error(batch_size: int = 1, decode_error: bool = True) -> RuntimeError | None:
"""
Check if there is an error and return it
Expand Down Expand Up @@ -171,7 +170,7 @@ def assert_no_error(batch_size: int = 1, decode_error: bool = True):

def handle_errors(
continue_on_batch_error: bool, batch_size: int = 1, decode_error: bool = True
) -> Optional[PowerGridBatchError]:
) -> PowerGridBatchError | None:
"""
Handle any errors in the way that is specified.
Expand All @@ -184,10 +183,10 @@ def handle_errors(
error: Any errors previously encountered, unless it was a batch error and continue_on_batch_error was True.
Returns:
Optional[PowerGridBatchError]: None if there were no errors, or the previously encountered
error if it was a batch error and continue_on_batch_error was True.
PowerGridBatchError | None: None if there were no errors, or the previously encountered
error if it was a batch error and continue_on_batch_error was True.
"""
error: Optional[RuntimeError] = find_error(batch_size=batch_size, decode_error=decode_error)
error: RuntimeError | None = find_error(batch_size=batch_size, decode_error=decode_error)
if error is None:
return None

Expand Down
4 changes: 2 additions & 2 deletions src/power_grid_model/_core/power_grid_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from inspect import signature
from itertools import chain
from pathlib import Path
from typing import Callable, Optional
from typing import Callable

from power_grid_model._core.index_integer import IdC, IdxC

Expand Down Expand Up @@ -206,7 +206,7 @@ class PowerGridCore:
"""

_handle: HandlePtr
_instance: Optional["PowerGridCore"] = None
_instance: "PowerGridCore | None" = None

# singleton of power grid core
def __new__(cls, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions src/power_grid_model/_core/power_grid_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Power grid model raw dataset handler
"""

from typing import Any, Mapping, Optional
from typing import Any, Mapping

from power_grid_model._core.buffer_handling import (
BufferProperties,
Expand Down Expand Up @@ -283,7 +283,7 @@ class CConstDataset:
_const_dataset: ConstDatasetPtr
_buffer_views: list[CBuffer]

def __new__(cls, data: Dataset, dataset_type: Optional[DatasetType] = None):
def __new__(cls, data: Dataset, dataset_type: DatasetType | None = None):
instance = super().__new__(cls)
instance._const_dataset = ConstDatasetPtr()

Expand Down
22 changes: 11 additions & 11 deletions src/power_grid_model/_core/power_grid_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from enum import IntEnum
from typing import Optional, Type
from typing import Type

import numpy as np

Expand Down Expand Up @@ -45,11 +45,11 @@ class PowerGridModel:
"""

_model_ptr: ModelPtr
_all_component_count: Optional[dict[ComponentType, int]]
_batch_error: Optional[PowerGridBatchError]
_all_component_count: dict[ComponentType, int] | None
_batch_error: PowerGridBatchError | None

@property
def batch_error(self) -> Optional[PowerGridBatchError]:
def batch_error(self) -> PowerGridBatchError | None:
"""
Get the batch error object, if present
Expand Down Expand Up @@ -242,7 +242,7 @@ def _calculate_impl( # pylint: disable=too-many-positional-arguments
self,
calculation_type: CalculationType,
symmetric: bool,
update_data: Optional[Dataset],
update_data: Dataset | None,
output_component_types: ComponentAttributeMapping,
options: Options,
continue_on_batch_error: bool,
Expand Down Expand Up @@ -310,7 +310,7 @@ def _calculate_power_flow(
error_tolerance: float = 1e-8,
max_iterations: int = 20,
calculation_method: CalculationMethod | str = CalculationMethod.newton_raphson,
update_data: Optional[Dataset] = None,
update_data: Dataset | None = None,
threading: int = -1,
output_component_types: ComponentAttributeMapping = None,
continue_on_batch_error: bool = False,
Expand Down Expand Up @@ -347,7 +347,7 @@ def _calculate_state_estimation(
error_tolerance: float = 1e-8,
max_iterations: int = 20,
calculation_method: CalculationMethod | str = CalculationMethod.iterative_linear,
update_data: Optional[Dataset] = None,
update_data: Dataset | None = None,
threading: int = -1,
output_component_types: ComponentAttributeMapping = None,
continue_on_batch_error: bool = False,
Expand Down Expand Up @@ -379,7 +379,7 @@ def _calculate_short_circuit(
self,
*,
calculation_method: CalculationMethod | str = CalculationMethod.iec60909,
update_data: Optional[Dataset] = None,
update_data: Dataset | None = None,
threading: int = -1,
output_component_types: ComponentAttributeMapping = None,
continue_on_batch_error: bool = False,
Expand Down Expand Up @@ -416,7 +416,7 @@ def calculate_power_flow(
error_tolerance: float = 1e-8,
max_iterations: int = 20,
calculation_method: CalculationMethod | str = CalculationMethod.newton_raphson,
update_data: Optional[dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset] = None,
update_data: dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset | None = None,
threading: int = -1,
output_component_types: ComponentAttributeMapping = None,
continue_on_batch_error: bool = False,
Expand Down Expand Up @@ -514,7 +514,7 @@ def calculate_state_estimation(
error_tolerance: float = 1e-8,
max_iterations: int = 20,
calculation_method: CalculationMethod | str = CalculationMethod.iterative_linear,
update_data: Optional[dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset] = None,
update_data: dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset | None = None,
threading: int = -1,
output_component_types: ComponentAttributeMapping = None,
continue_on_batch_error: bool = False,
Expand Down Expand Up @@ -604,7 +604,7 @@ def calculate_short_circuit(
self,
*,
calculation_method: CalculationMethod | str = CalculationMethod.iec60909,
update_data: Optional[dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset] = None,
update_data: dict[str, np.ndarray | dict[str, np.ndarray]] | Dataset | None = None,
threading: int = -1,
output_component_types: ComponentAttributeMapping = None,
continue_on_batch_error: bool = False,
Expand Down
7 changes: 2 additions & 5 deletions src/power_grid_model/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

from copy import deepcopy
from typing import Optional, Sequence, cast
from typing import Sequence, cast

import numpy as np

Expand Down Expand Up @@ -218,8 +218,6 @@ def _split_numpy_array_in_batches(
Args:
data: A 1D or 2D Numpy structured array. A 1D array is a single table / batch, a 2D array is a batch per table.
component: The name of the component to which the data belongs; only used for errors.
attribute [optional]: The name of the attribute to which the data belongs; only used for errors.
Returns:
A list with a single numpy structured array per batch
Expand All @@ -239,7 +237,6 @@ def split_dense_batch_data_in_batches(
Args:
data: A 1D or 2D Numpy structured array. A 1D array is a single table / batch, a 2D array is a batch per table.
component: The name of the component to which the data belongs, only used for errors.
batch_size: size of batch
Returns:
Expand Down Expand Up @@ -326,7 +323,7 @@ def convert_dataset_to_python_dataset(data: Dataset) -> PythonDataset:

# Check if the dataset is a single dataset or batch dataset
# It is batch dataset if it is 2D array or a indptr/data structure
is_batch: Optional[bool] = None
is_batch: bool | None = None
for component, array in data.items():
is_dense_batch = isinstance(array, np.ndarray) and array.ndim == 2
is_sparse_batch = isinstance(array, dict) and "indptr" in array and "data" in array
Expand Down
12 changes: 6 additions & 6 deletions src/power_grid_model/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import tempfile
import warnings
from pathlib import Path
from typing import Optional, cast as cast_type
from typing import cast as cast_type

import numpy as np

Expand Down Expand Up @@ -142,9 +142,9 @@ def json_deserialize_from_file(
def json_serialize_to_file(
file_path: Path,
data: Dataset,
dataset_type: Optional[DatasetType] = None,
dataset_type: DatasetType | None = None,
use_compact_list: bool = False,
indent: Optional[int] = 2,
indent: int | None = 2,
):
"""
Export JSON data in most recent format.
Expand Down Expand Up @@ -189,7 +189,7 @@ def msgpack_deserialize_from_file(


def msgpack_serialize_to_file(
file_path: Path, data: Dataset, dataset_type: Optional[DatasetType] = None, use_compact_list: bool = False
file_path: Path, data: Dataset, dataset_type: DatasetType | None = None, use_compact_list: bool = False
):
"""
Export msgpack data in most recent format.
Expand Down Expand Up @@ -234,7 +234,7 @@ def import_json_data(json_file: Path, data_type: str, *args, **kwargs) -> Datase


def export_json_data(
json_file: Path, data: Dataset, indent: Optional[int] = 2, compact: bool = False, use_deprecated_format: bool = True
json_file: Path, data: Dataset, indent: int | None = 2, compact: bool = False, use_deprecated_format: bool = True
):
"""
[deprecated] Export json data in a deprecated serialization format.
Expand Down Expand Up @@ -268,7 +268,7 @@ def export_json_data(


def _compatibility_deprecated_export_json_data(
json_file: Path, data: Dataset, indent: Optional[int] = 2, compact: bool = False
json_file: Path, data: Dataset, indent: int | None = 2, compact: bool = False
):
serialized_data = json_serialize(data=data, use_compact_list=compact, indent=-1 if indent is None else indent)
old_format_serialized_data = json.dumps(json.loads(serialized_data)["data"])
Expand Down
6 changes: 2 additions & 4 deletions src/power_grid_model/validation/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
Helper functions to assert valid data. They basically call validate_input_data or validate_batch_data and raise a
ValidationException if the validation results in one or more errors.
"""
from typing import Optional

from power_grid_model.data_types import BatchDataset, SingleDataset
from power_grid_model.enum import CalculationType
from power_grid_model.validation.errors import ValidationError
Expand All @@ -31,7 +29,7 @@ def __str__(self):


def assert_valid_input_data(
input_data: SingleDataset, calculation_type: Optional[CalculationType] = None, symmetric: bool = True
input_data: SingleDataset, calculation_type: CalculationType | None = None, symmetric: bool = True
):
"""
Validates the entire input dataset:
Expand Down Expand Up @@ -60,7 +58,7 @@ def assert_valid_input_data(
def assert_valid_batch_data(
input_data: SingleDataset,
update_data: BatchDataset,
calculation_type: Optional[CalculationType] = None,
calculation_type: CalculationType | None = None,
symmetric: bool = True,
):
"""
Expand Down
20 changes: 10 additions & 10 deletions src/power_grid_model/validation/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
from abc import ABC
from enum import Enum
from typing import Any, Iterable, Optional, Type
from typing import Any, Iterable, Type

from power_grid_model import ComponentType

Expand All @@ -32,18 +32,18 @@ class ValidationError(ABC):
"""

component: Optional[ComponentType | list[ComponentType]] = None
component: ComponentType | list[ComponentType] | None = None
"""
The component, or components, to which the error applies.
"""

field: Optional[str | list[str] | list[tuple[ComponentType, str]]] = None
field: str | list[str] | list[tuple[ComponentType, str]] | None = None
"""
The field, or fields, to which the error applies. A field can also be a tuple (component, field) when multiple
components are being addressed.
"""

ids: Optional[list[int] | list[tuple[ComponentType, int]]] = None
ids: list[int] | list[tuple[ComponentType, int]] | None = None
"""
The object identifiers to which the error applies. A field object identifier can also be a tuple (component, id)
when multiple components are being addressed.
Expand Down Expand Up @@ -79,7 +79,7 @@ def _unpack(field: str | tuple[ComponentType, str]) -> str:
return self._delimiter.join(_unpack(field) for field in self.field)
return _unpack(self.field) if self.field else str(self.field)

def get_context(self, id_lookup: Optional[list[str] | dict[int, str]] = None) -> dict[str, Any]:
def get_context(self, id_lookup: list[str] | dict[int, str] | None = None) -> dict[str, Any]:
"""
Returns a dictionary that supplies (human readable) information about this error. Each member variable is
included in the dictionary. If a function {field_name}_str() exists, the value is overwritten by that function.
Expand Down Expand Up @@ -129,9 +129,9 @@ class SingleFieldValidationError(ValidationError):
_message = "Field {field} is not valid for {n} {objects}."
component: ComponentType
field: str
ids: Optional[list[int]]
ids: list[int] | None

def __init__(self, component: ComponentType, field: str, ids: Optional[Iterable[int]]):
def __init__(self, component: ComponentType, field: str, ids: Iterable[int] | None):
"""
Args:
component: Component name
Expand Down Expand Up @@ -325,9 +325,9 @@ def __init__( # pylint: disable=too-many-arguments
self,
component: ComponentType,
field: str,
ids: Optional[list[int]] = None,
ref_components: Optional[ComponentType | list[ComponentType]] = None,
filters: Optional[dict[str, Any]] = None,
ids: list[int] | None = None,
ref_components: ComponentType | list[ComponentType] | None = None,
filters: dict[str, Any] | None = None,
):
# pylint: disable=too-many-positional-arguments
super().__init__(component=component, field=field, ids=ids)
Expand Down
Loading

0 comments on commit 1a89659

Please sign in to comment.