Skip to content

Linting and formatting: add Ruff #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 64 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
4d42bb8
ruff in pyproject, pre-commit, notebooks not fixed yet. Pylint needs …
Laurynas-Jagutis May 14, 2025
4c91f43
add fixes to notebooks
Laurynas-Jagutis May 14, 2025
e4f1a84
remove local pylint, add ruff ignore based on file location
Laurynas-Jagutis May 19, 2025
b32e306
change lambdas
Laurynas-Jagutis May 19, 2025
dffdfe9
pyproject.toml clean up
Laurynas-Jagutis May 20, 2025
1877a6d
final ruff fixes, pyproject.toml clean up
Laurynas-Jagutis May 20, 2025
5ff504f
clear outputs from validation examples
Laurynas-Jagutis May 20, 2025
d3c98fb
adjust ruff version
Laurynas-Jagutis May 26, 2025
78f3116
adjust python version and ruff version
Laurynas-Jagutis May 26, 2025
27675e3
conflict resolution?
Laurynas-Jagutis May 26, 2025
4a98bf5
remove the required ruff version, restart the notebook
Laurynas-Jagutis May 26, 2025
302d412
reset execution count of power flow example
Laurynas-Jagutis May 26, 2025
3dfe13a
minor
Laurynas-Jagutis May 26, 2025
e8f99d0
fix importing when multiple as is used
Laurynas-Jagutis May 26, 2025
96cc042
add comment
Laurynas-Jagutis May 26, 2025
e52bac9
rerun state estimation notebook
Laurynas-Jagutis May 27, 2025
8d8d5e6
rerun state estimation notebook
Laurynas-Jagutis May 27, 2025
c84e67c
change python version to 3.11 in pyproject
Laurynas-Jagutis May 27, 2025
78f7d63
adjust E402 filter
Laurynas-Jagutis May 27, 2025
4aa04de
add SIM checks, but ignore them
Laurynas-Jagutis May 28, 2025
abccc35
rerun validation examples notebook
Laurynas-Jagutis May 28, 2025
837576d
rerun validation examples, change absolute path into relative
Laurynas-Jagutis May 28, 2025
86c8e3f
fix version and relative paths
Laurynas-Jagutis May 28, 2025
37307b2
make sure that pre-commit has been run on all files
Laurynas-Jagutis May 28, 2025
dcfaf4d
fix errors in code after merging main
Laurynas-Jagutis Jun 3, 2025
069d28c
adjust pre-commit to include fixes
Laurynas-Jagutis Jun 4, 2025
5602c10
adjust pre-commit to remove fix from formatter
Laurynas-Jagutis Jun 4, 2025
171e8e3
applying final fixes
Laurynas-Jagutis Jun 4, 2025
b7e8a40
ruff in pyproject, pre-commit, notebooks not fixed yet. Pylint needs …
Laurynas-Jagutis May 14, 2025
2478050
add fixes to notebooks
Laurynas-Jagutis May 14, 2025
44b3f87
remove local pylint, add ruff ignore based on file location
Laurynas-Jagutis May 19, 2025
a64f566
change lambdas
Laurynas-Jagutis May 19, 2025
c3e0bf4
pyproject.toml clean up
Laurynas-Jagutis May 20, 2025
eb28ba3
final ruff fixes, pyproject.toml clean up
Laurynas-Jagutis May 20, 2025
13c7b6b
clear outputs from validation examples
Laurynas-Jagutis May 20, 2025
9ce8005
adjust ruff version
Laurynas-Jagutis May 26, 2025
5118328
adjust python version and ruff version
Laurynas-Jagutis May 26, 2025
12c8521
conflict resolution?
Laurynas-Jagutis May 26, 2025
fb1f962
remove the required ruff version, restart the notebook
Laurynas-Jagutis May 26, 2025
fcc1671
reset execution count of power flow example
Laurynas-Jagutis May 26, 2025
b042d02
minor
Laurynas-Jagutis May 26, 2025
e6dee6a
fix importing when multiple as is used
Laurynas-Jagutis May 26, 2025
8151dd3
add comment
Laurynas-Jagutis May 26, 2025
7eee25e
rerun state estimation notebook
Laurynas-Jagutis May 27, 2025
3e0dc98
rerun state estimation notebook
Laurynas-Jagutis May 27, 2025
44cfff3
change python version to 3.11 in pyproject
Laurynas-Jagutis May 27, 2025
cfab98b
adjust E402 filter
Laurynas-Jagutis May 27, 2025
b8cea09
add SIM checks, but ignore them
Laurynas-Jagutis May 28, 2025
f69236c
rerun validation examples notebook
Laurynas-Jagutis May 28, 2025
f11ad76
rerun validation examples, change absolute path into relative
Laurynas-Jagutis May 28, 2025
44e4981
fix version and relative paths
Laurynas-Jagutis May 28, 2025
c6e03c0
make sure that pre-commit has been run on all files
Laurynas-Jagutis May 28, 2025
bfad930
fix errors in code after merging main
Laurynas-Jagutis Jun 3, 2025
7e9172b
adjust pre-commit to include fixes
Laurynas-Jagutis Jun 4, 2025
bc07657
adjust pre-commit to remove fix from formatter
Laurynas-Jagutis Jun 4, 2025
f5a7d70
applying final fixes
Laurynas-Jagutis Jun 4, 2025
d90d853
remove pylint comments
Laurynas-Jagutis Jun 4, 2025
76f5626
Merge branch 'feature/add-ruff' of https://github.com/PowerGridModel/…
Laurynas-Jagutis Jun 4, 2025
6e5d3df
remove pylint comments from generator and generated files
Laurynas-Jagutis Jun 10, 2025
26d2f8d
Merge remote-tracking branch 'origin/main' into feature/add-ruff
Laurynas-Jagutis Jun 10, 2025
038f196
add no sonar
Laurynas-Jagutis Jun 10, 2025
191a5fa
remove a file which was removed on main
Laurynas-Jagutis Jun 10, 2025
ea8e4f6
Merge remote-tracking branch 'origin/fix/mypy-version' into feature/a…
Laurynas-Jagutis Jun 10, 2025
129dd46
Merge remote-tracking branch 'origin/main' into feature/add-ruff
Laurynas-Jagutis Jun 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repos:
hooks:
# Run the linter.
- id: ruff-check
args: [ --fix ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ from typing import Any, Mapping, TypeAlias, TypeVar

# fmt: off

# pylint: disable=invalid-name

class _MetaEnum(EnumMeta):
def __contains__(cls, member):
"""
Expand Down Expand Up @@ -58,8 +56,6 @@ class ComponentType(str, Enum, metaclass=_MetaEnum):
{%- endfor %}


# pylint: enable=invalid-name

DatasetTypeLike: TypeAlias = DatasetType | str
DatasetTypeVar = TypeVar("DatasetTypeVar", bound=DatasetTypeLike) # helper used for type deduction

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

# -- Setup

import os
import os # noqa: E402

# Fix linking in github and rtd
link_head_gh = "https://github.com/PowerGridModel/power-grid-model/"
Expand Down
13 changes: 10 additions & 3 deletions docs/examples/Asymmetric Calculation Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType, MeasuredTerminalType\n",
"from power_grid_model import initialize_array"
"from power_grid_model import (\n",
" CalculationMethod,\n",
" CalculationType,\n",
" ComponentType,\n",
" DatasetType,\n",
" LoadGenType,\n",
" MeasuredTerminalType,\n",
" PowerGridModel,\n",
" initialize_array,\n",
")"
]
},
{
Expand Down
15 changes: 11 additions & 4 deletions docs/examples/Asymmetric Line.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from power_grid_model import LoadGenType, DatasetType, ComponentType\n",
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType, MeasuredTerminalType\n",
"from power_grid_model import initialize_array"
"from power_grid_model import (\n",
" CalculationMethod,\n",
" CalculationType,\n",
" ComponentType,\n",
" DatasetType,\n",
" LoadGenType,\n",
" MeasuredTerminalType,\n",
" PowerGridModel,\n",
" initialize_array,\n",
")"
]
},
{
Expand Down Expand Up @@ -329,7 +336,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down
57 changes: 41 additions & 16 deletions docs/examples/Generic Branch Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
}
],
"source": [
"import numpy as np\n",
"import warnings\n",
"\n",
"with warnings.catch_warnings():\n",
Expand All @@ -138,9 +137,15 @@
"\n",
"import pandas as pd\n",
"\n",
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType\n",
"from power_grid_model import initialize_array\n",
"from power_grid_model import (\n",
" CalculationMethod,\n",
" CalculationType,\n",
" ComponentType,\n",
" DatasetType,\n",
" LoadGenType,\n",
" PowerGridModel,\n",
" initialize_array,\n",
")\n",
"from power_grid_model.validation import assert_valid_input_data\n",
"\n",
"# network\n",
Expand Down Expand Up @@ -242,9 +247,17 @@
" symmetric=True, error_tolerance=1e-8, max_iterations=20, calculation_method=CalculationMethod.newton_raphson\n",
")\n",
"\n",
"e3 = lambda x: x / 1e3\n",
"e6 = lambda x: x / 1e6\n",
"percent = lambda x: x * 100\n",
"\n",
"def e3(x):\n",
" return x / 1e3\n",
"\n",
"\n",
"def e6(x):\n",
" return x / 1e6\n",
"\n",
"\n",
"def percent(x):\n",
" return x * 100\n",
"\n",
"\n",
"def print_node_input(input_data):\n",
Expand Down Expand Up @@ -519,18 +532,22 @@
],
"source": [
"# some basic imports\n",
"import numpy as np\n",
"import warnings\n",
"\n",
"with warnings.catch_warnings():\n",
" warnings.simplefilter(\"ignore\", category=DeprecationWarning)\n",
" # Suppress warning about pyarrow as future required dependency\n",
"\n",
"import pandas as pd\n",
"\n",
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType\n",
"from power_grid_model import initialize_array\n",
"from power_grid_model import (\n",
" CalculationMethod,\n",
" CalculationType,\n",
" ComponentType,\n",
" DatasetType,\n",
" LoadGenType,\n",
" PowerGridModel,\n",
" initialize_array,\n",
")\n",
"from power_grid_model.validation import assert_valid_input_data\n",
"\n",
"# network\n",
Expand Down Expand Up @@ -621,9 +638,17 @@
"pd.set_option(\"display.max_columns\", None)\n",
"pd.set_option(\"display.max_colwidth\", None)\n",
"\n",
"e3 = lambda x: x / 1e3\n",
"e6 = lambda x: x / 1e6\n",
"percent = lambda x: x * 100\n",
"\n",
"def e3(x):\n",
" return x / 1e3\n",
"\n",
"\n",
"def e6(x):\n",
" return x / 1e6\n",
"\n",
"\n",
"def percent(x):\n",
" return x * 100\n",
"\n",
"\n",
"def print_node_input(input_data):\n",
Expand Down Expand Up @@ -1039,7 +1064,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.0"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down
7 changes: 3 additions & 4 deletions docs/examples/Make Test Dataset.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
"from power_grid_model import PowerGridModel\n",
"from power_grid_model import initialize_array\n",
"from power_grid_model import ComponentType, DatasetType, LoadGenType, PowerGridModel, initialize_array\n",
"\n",
"# network\n",
"\n",
Expand Down Expand Up @@ -229,10 +227,11 @@
"metadata": {},
"outputs": [],
"source": [
"from power_grid_model.utils import json_serialize_to_file\n",
"import tempfile\n",
"from pathlib import Path\n",
"\n",
"from power_grid_model.utils import json_serialize_to_file\n",
"\n",
"temp_path = Path(tempfile.gettempdir())\n",
"json_serialize_to_file(temp_path / \"input.json\", input_data)"
]
Expand Down
52 changes: 32 additions & 20 deletions docs/examples/Power Flow Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,17 @@
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from power_grid_model import LoadGenType, ComponentType, DatasetType, ComponentAttributeFilterOptions\n",
"from power_grid_model import PowerGridModel, CalculationMethod, CalculationType\n",
"from power_grid_model import initialize_array, attribute_dtype"
"from power_grid_model import (\n",
" CalculationMethod,\n",
" CalculationType,\n",
" ComponentAttributeFilterOptions,\n",
" ComponentType,\n",
" DatasetType,\n",
" LoadGenType,\n",
" PowerGridModel,\n",
" attribute_dtype,\n",
" initialize_array,\n",
")"
]
},
{
Expand Down Expand Up @@ -464,9 +472,12 @@
" max_iterations=20,\n",
" calculation_method=CalculationMethod.newton_raphson,\n",
" output_component_types={\n",
" ComponentType.node: None, # node output as row-based\n",
" ComponentType.line: [\"id\", \"p_from\"], # line output columns id and p_from\n",
" ComponentType.sym_load: ComponentAttributeFilterOptions.everything, # all sym_load attributes as columns\n",
" # node output as row-based\n",
" ComponentType.node: None,\n",
" # line output columns id and p_from\n",
" ComponentType.line: [\"id\", \"p_from\"],\n",
" # all sym_load attributes as columns\n",
" ComponentType.sym_load: ComponentAttributeFilterOptions.everything,\n",
" },\n",
")\n",
"\n",
Expand Down Expand Up @@ -515,7 +526,8 @@
" error_tolerance=1e-8,\n",
" max_iterations=20,\n",
" calculation_method=CalculationMethod.newton_raphson,\n",
" output_component_types=ComponentAttributeFilterOptions.everything, # all attributes for all component types as columns\n",
" # all attributes for all component types as columns\n",
" output_component_types=ComponentAttributeFilterOptions.everything,\n",
")\n",
"\n",
"print(\"List of component types in result dataset\")\n",
Expand Down Expand Up @@ -1216,7 +1228,7 @@
}
],
"source": [
"from power_grid_model.errors import PowerGridError, ConflictVoltage\n",
"from power_grid_model.errors import ConflictVoltage, PowerGridError\n",
"\n",
"# node\n",
"node_error = initialize_array(DatasetType.input, ComponentType.node, 2)\n",
Expand Down Expand Up @@ -1372,7 +1384,7 @@
"\n",
"Failed scenarios: [3 7]\n",
"Succeeded scenarios: [0 1 2 4 5 6 8 9]\n",
"Error messages: ['The id cannot be found: 1000\\n', 'Iteration failed to converge after 20 iterations! Max deviation: 5.81174e+85, error tolerance: 1e-08.\\n']\n"
"Error messages: ['The id cannot be found: 1000\\n', 'Sparse matrix error, possibly singular matrix!\\nIf you get this error from state estimation, it might mean the system is not fully observable, i.e. not enough measurements.\\nIt might also mean that you are running into a corner case where PGM cannot resolve yet.See https://github.com/PowerGridModel/power-grid-model/issues/864.']\n"
]
}
],
Expand Down Expand Up @@ -1403,16 +1415,16 @@
"output_type": "stream",
"text": [
"Node data with invalid results\n",
"[[9.99401170e-001 9.92685785e-001 9.94521366e-001]\n",
" [9.99347687e-001 9.86226389e-001 9.89352855e-001]\n",
" [9.99288384e-001 9.79654011e-001 9.84095542e-001]\n",
" [3.94357132e+180 2.87518198e+161 2.04418455e+214]\n",
" [9.99151380e-001 9.66149483e-001 9.73298790e-001]\n",
" [9.99073166e-001 9.59205860e-001 9.67750710e-001]\n",
" [9.98988099e-001 9.52126208e-001 9.62096474e-001]\n",
" [0.00000000e+000 0.00000000e+000 0.00000000e+000]\n",
" [9.98796126e-001 9.37530046e-001 9.50447962e-001]\n",
" [9.98688504e-001 9.29997471e-001 9.44441670e-001]]\n",
"[[ 9.99401170e-001 9.92685785e-001 9.94521366e-001]\n",
" [ 9.99347687e-001 9.86226389e-001 9.89352855e-001]\n",
" [ 9.99288384e-001 9.79654011e-001 9.84095542e-001]\n",
" [-2.66881060e+116 2.33997016e-302 6.70346672e-198]\n",
" [ 9.99151380e-001 9.66149483e-001 9.73298790e-001]\n",
" [ 9.99073166e-001 9.59205860e-001 9.67750710e-001]\n",
" [ 9.98988099e-001 9.52126208e-001 9.62096474e-001]\n",
" [-2.44756775e+092 5.35663612e-256 1.91838796e-203]\n",
" [ 9.98796126e-001 9.37530046e-001 9.50447962e-001]\n",
" [ 9.98688504e-001 9.29997471e-001 9.44441670e-001]]\n",
"Node data with only valid results\n",
"[[0.99940117 0.99268579 0.99452137]\n",
" [0.99934769 0.98622639 0.98935286]\n",
Expand Down Expand Up @@ -1469,7 +1481,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.0"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down
5 changes: 3 additions & 2 deletions docs/examples/Serialization Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
"outputs": [],
"source": [
"import json\n",
"from pandas import DataFrame\n",
"import pprint\n",
"\n",
"from power_grid_model import PowerGridModel, ComponentType, ComponentAttributeFilterOptions\n",
"from pandas import DataFrame\n",
"\n",
"from power_grid_model import ComponentAttributeFilterOptions, ComponentType, PowerGridModel\n",
"from power_grid_model.utils import json_deserialize, json_serialize"
]
},
Expand Down
15 changes: 8 additions & 7 deletions docs/examples/Short Circuit Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,19 @@
"source": [
"# some basic imports\n",
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"from power_grid_model import LoadGenType, ComponentType, DatasetType\n",
"from power_grid_model import (\n",
" PowerGridModel,\n",
" CalculationMethod,\n",
" CalculationType,\n",
" FaultType,\n",
" ComponentType,\n",
" DatasetType,\n",
" FaultPhase,\n",
" FaultType,\n",
" LoadGenType,\n",
" PowerGridModel,\n",
" ShortCircuitVoltageScaling,\n",
")\n",
"from power_grid_model import initialize_array"
" initialize_array,\n",
")"
]
},
{
Expand Down Expand Up @@ -337,7 +338,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.0"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down
Loading
Loading