Skip to content

Commit 1e19f7d

Browse files
authored
Merge pull request #1023 from PowerGridModel/feature/add-more-ruff-checks
Formatting and Linting: add a couple more Ruff checks
2 parents e7c961b + f1ba2e8 commit 1e19f7d

File tree

16 files changed

+31
-59
lines changed

16 files changed

+31
-59
lines changed

code_generation/code_gen.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ def render_dataset_class_maps(self, template_path: Path, data_path: Path, output
102102
# create list
103103
all_map = {}
104104
for dataset in dataset_meta_data:
105-
if dataset.is_template:
106-
prefixes = ["sym_", "asym_"]
107-
else:
108-
prefixes = [""]
105+
prefixes = ["sym_", "asym_"] if dataset.is_template else [""]
109106
for prefix in prefixes:
110107
all_components = {}
111108
for component in dataset.components:

code_generation/templates/src/power_grid_model/_core/dataset_class_maps.py.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class _MetaEnum(EnumMeta):
2525
Returns:
2626
bool: True if the member is part of the Enum, False otherwise.
2727
"""
28-
return member in cls.__members__.keys()
28+
return member in cls.__members__
2929

3030

3131
class DatasetType(str, Enum, metaclass=_MetaEnum):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ select = [
113113
"FURB",
114114
"FLY",
115115
"SLOT",
116+
"NPY",
116117
]
117-
ignore = ["SIM108", "SIM118", "SIM110", "SIM211"]
118118

119119
[tool.ruff.lint.isort]
120120
# Imports that are imported using keyword "as" and are from the same source - are combined.

setup.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ def get_tag(self):
7676
class MyBuildExt(build_ext):
7777
def build_extensions(self):
7878
if not if_win:
79-
if "CXX" in os.environ:
80-
cxx = os.environ["CXX"]
81-
else:
82-
cxx = self.compiler.compiler_cxx[0]
79+
cxx = os.environ.get("CXX", self.compiler.compiler_cxx[0])
8380
# check setuptools has an update change in the version 72.2 about cxx compiler options
8481
# to be compatible with both version, we check if compiler_so_cxx exists
8582
if not hasattr(self.compiler, "compiler_so_cxx"):
@@ -93,10 +90,7 @@ def build_extensions(self):
9390
linker_so_cxx[0] = cxx
9491
self.compiler.compiler_cxx = [cxx]
9592
# add link time optimization
96-
if "clang" in cxx:
97-
lto_flag = "-flto=thin"
98-
else:
99-
lto_flag = "-flto"
93+
lto_flag = "-flto=thin" if "clang" in cxx else "-flto"
10094
compiler_so_cxx += [lto_flag]
10195
linker_so_cxx += [lto_flag]
10296
# remove debug and optimization flags

src/power_grid_model/_core/dataset_definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __contains__(cls, member):
2222
Returns:
2323
bool: True if the member is part of the Enum, False otherwise.
2424
"""
25-
return member in cls.__members__.keys()
25+
return member in cls.__members__
2626

2727

2828
class DatasetType(str, Enum, metaclass=_MetaEnum):

src/power_grid_model/_core/power_grid_core.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ def _load_core() -> CDLL:
126126
127127
"""
128128
# first try to find the DLL local
129-
if platform.system() == "Windows":
130-
dll_file = "_power_grid_core.dll"
131-
else:
132-
dll_file = "_power_grid_core.so"
129+
dll_file = "_power_grid_core.dll" if platform.system() == "Windows" else "_power_grid_core.so"
133130
dll_path = Path(__file__).parent / dll_file
134131

135132
# if local DLL is not found, try to find the DLL from conda environment
@@ -192,10 +189,7 @@ def make_c_binding(func: Callable):
192189

193190
# binding function
194191
def cbind_func(self, *args, **kwargs):
195-
if "destroy" in name:
196-
c_inputs = []
197-
else:
198-
c_inputs = [self._handle]
192+
c_inputs = [] if "destroy" in name else [self._handle]
199193
args = chain(args, (kwargs[key] for key in py_argnames[len(args) :]))
200194
for arg in args:
201195
if isinstance(arg, str):

src/power_grid_model/_core/power_grid_model.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ def _get_output_component_count(self, calculation_type: CalculationType):
189189
}.get(calculation_type, [])
190190

191191
def include_type(component_type: ComponentType):
192-
for exclude_type in exclude_types:
193-
if exclude_type.value in component_type.value:
194-
return False
195-
return True
192+
return all(exclude_type.value not in component_type.value for exclude_type in exclude_types)
196193

197194
return {ComponentType[k]: v for k, v in self.all_component_count.items() if include_type(k)}
198195

src/power_grid_model/_core/utils.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,12 @@ def _extract_columnar_data(
651651
"""
652652
not_columnar_data_message = "Expected columnar data"
653653

654-
if is_batch is not None:
655-
allowed_dims = [2, 3] if is_batch else [1, 2]
656-
else:
654+
if is_batch is None:
657655
allowed_dims = [1, 2, 3]
656+
elif is_batch:
657+
allowed_dims = [2, 3]
658+
else:
659+
allowed_dims = [1, 2]
658660

659661
sub_data = data["data"] if is_sparse(data) else data
660662

@@ -683,10 +685,12 @@ def _extract_row_based_data(
683685
Returns:
684686
SingleArray | DenseBatchArray: the contents of row based data
685687
"""
686-
if is_batch is not None:
687-
allowed_dims = [2] if is_batch else [1]
688-
else:
688+
if is_batch is None:
689689
allowed_dims = [1, 2]
690+
elif is_batch:
691+
allowed_dims = [2]
692+
else:
693+
allowed_dims = [1]
690694

691695
sub_data = data["data"] if is_sparse(data) else data
692696

src/power_grid_model/validation/_rules.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,7 @@ def none_missing(data: SingleDataset, component: ComponentType, fields: str | li
892892
fields = [fields]
893893
for field in fields:
894894
nan = _nan_type(component, field)
895-
if np.isnan(nan):
896-
invalid = np.isnan(data[component][field])
897-
else:
898-
invalid = np.equal(data[component][field], nan)
895+
invalid = np.isnan(data[component][field]) if np.isnan(nan) else np.equal(data[component][field], nan)
899896

900897
if invalid.any():
901898
# handle both symmetric and asymmetric values

src/power_grid_model/validation/utils.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def _update_input_data(input_data: SingleDataset, update_data: SingleDataset):
101101
"""
102102

103103
merged_data = {component: array.copy() for component, array in input_data.items()}
104-
for component in update_data.keys():
104+
for component in update_data:
105105
_update_component_data(component, merged_data[component], update_data[component])
106106
return merged_data
107107

@@ -140,10 +140,7 @@ def _update_component_array_data(
140140
if field == "id":
141141
continue
142142
nan = _nan_type(component, field, DatasetType.update)
143-
if np.isnan(nan):
144-
mask = ~np.isnan(update_data[field])
145-
else:
146-
mask = np.not_equal(update_data[field], nan)
143+
mask = ~np.isnan(update_data[field]) if np.isnan(nan) else np.not_equal(update_data[field], nan)
147144

148145
if mask.ndim == 2:
149146
for phase in range(mask.shape[1]):

0 commit comments

Comments
 (0)