Skip to content

Commit 9062334

Browse files
authored
Merge pull request #314 from PowerGridModel/feature/add-ruff
Linting and Formatting: replacing current linters and formatters with Ruff, fixing issues caught by Ruff
2 parents 21b8e75 + f9bacdd commit 9062334

33 files changed

+137
-119
lines changed

.github/workflows/check-code-quality.yml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,16 @@ jobs:
2929
- name: Upgrade pip
3030
run: pip install --upgrade pip
3131

32-
- name: Install and run isort
33-
run: |
34-
pip install isort
35-
isort .
36-
37-
- name: Install and run black
38-
run: |
39-
pip install black[jupyter]
40-
black .
41-
4232
- name: Install and run mypy
4333
run: |
4434
pip install mypy
4535
mypy src
4636
47-
- name: Install and run pylint
37+
- name: Install and run ruff
4838
run: |
49-
pip install pylint .
50-
pylint power_grid_model_io
39+
pip install ruff .
40+
ruff check .
41+
ruff format .
5142
5243
- name: If needed raise error
5344
run: |

.pre-commit-config.yaml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,22 @@ repos:
77
rev: v5.0.2
88
hooks:
99
- id: reuse
10-
- repo: https://github.com/pycqa/isort
11-
rev: 6.0.1
10+
- repo: https://github.com/astral-sh/ruff-pre-commit
11+
# Ruff version.
12+
rev: v0.11.12
1213
hooks:
13-
- id: isort
14-
- repo: https://github.com/psf/black
15-
rev: 25.1.0
16-
hooks:
17-
- id: black-jupyter
14+
# Run the linter.
15+
- id: ruff-check
16+
args: [ --fix ]
17+
# Run the formatter.
18+
- id: ruff-format
1819
- repo: https://github.com/pre-commit/mirrors-mypy
1920
rev: v1.16.0
2021
hooks:
2122
- id: mypy
2223
additional_dependencies: [numpy, pandas]
2324
- repo: local
2425
hooks:
25-
- id: pylint
26-
name: pylint
27-
entry: pylint
28-
files: ^src/.+\.py$
29-
language: system
30-
types: [ python ]
31-
args: [ "--rcfile=pyproject.toml" ]
32-
require_serial: true
3326
- id: pytest
3427
name: pytest
3528
entry: pytest

docs/examples/arrow_example.ipynb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,20 @@
2626
"outputs": [],
2727
"source": [
2828
"%%capture cap --no-stderr\n",
29-
"from IPython.display import display\n",
3029
"from typing import Iterable\n",
3130
"\n",
31+
"import numpy as np\n",
32+
"import pandas as pd\n",
33+
"import pyarrow as pa\n",
34+
"from IPython.display import display\n",
3235
"from power_grid_model import (\n",
33-
" PowerGridModel,\n",
34-
" initialize_array,\n",
35-
" CalculationMethod,\n",
36-
" power_grid_meta_data,\n",
36+
" ComponentAttributeFilterOptions,\n",
3737
" ComponentType,\n",
3838
" DatasetType,\n",
39-
" ComponentAttributeFilterOptions,\n",
39+
" PowerGridModel,\n",
40+
" power_grid_meta_data,\n",
4041
")\n",
41-
"from power_grid_model.data_types import SingleColumnarData\n",
42-
"import pyarrow as pa\n",
43-
"import pandas as pd\n",
44-
"import numpy as np"
42+
"from power_grid_model.data_types import SingleColumnarData"
4543
]
4644
},
4745
{

docs/examples/pandapower_example.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"outputs": [],
4343
"source": [
4444
"import warnings\n",
45+
"\n",
4546
"import pandapower as pp\n",
4647
"\n",
4748
"warnings.filterwarnings(\"ignore\", module=\"pandapower\", category=FutureWarning) # Hide warnings related to pandas\n",
@@ -1046,8 +1047,9 @@
10461047
"source": [
10471048
"%%capture cap --no-stderr\n",
10481049
"\n",
1049-
"from power_grid_model import PowerGridModel, CalculationType\n",
1050+
"from power_grid_model import CalculationType, PowerGridModel\n",
10501051
"from power_grid_model.validation import assert_valid_input_data\n",
1052+
"\n",
10511053
"from power_grid_model_io.converters import PandaPowerConverter\n",
10521054
"\n",
10531055
"output_file = \"data/pandapower/example_simple_output.json\"\n",

docs/examples/pgm_json_example.ipynb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
],
106106
"source": [
107107
"from pathlib import Path\n",
108-
"from IPython.display import display, Markdown\n",
108+
"\n",
109+
"from IPython.display import Markdown, display\n",
109110
"\n",
110111
"with Path(source_file).open() as json_file:\n",
111112
" display(Markdown(f\"<pre style='max-height: 160px; overflow: scroll; white-space: pre'>{json_file.read()}</pre>\"))"
@@ -578,7 +579,8 @@
578579
],
579580
"source": [
580581
"from pathlib import Path\n",
581-
"from IPython.display import display, Markdown\n",
582+
"\n",
583+
"from IPython.display import Markdown, display\n",
582584
"\n",
583585
"with Path(destination_file).open() as json_file:\n",
584586
" display(Markdown(f\"<pre style='max-height: 160px; overflow: scroll; white-space: pre'>{json_file.read()}</pre>\"))"
@@ -599,6 +601,7 @@
599601
"source": [
600602
"%%capture cap --no-stderr\n",
601603
"from power_grid_model import PowerGridModel\n",
604+
"\n",
602605
"from power_grid_model_io.converters import PgmJsonConverter\n",
603606
"\n",
604607
"source_file = \"data/tiny-net/input.json\"\n",

docs/examples/vision_example.ipynb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@
552552
],
553553
"source": [
554554
"from pathlib import Path\n",
555-
"from IPython.display import display, Markdown\n",
555+
"\n",
556+
"from IPython.display import Markdown, display\n",
556557
"\n",
557558
"with Path(destination_file).open() as json_file:\n",
558559
" display(Markdown(f\"<pre style='max-height: 160px; overflow: scroll; white-space: pre'>{json_file.read()}</pre>\"))"
@@ -651,9 +652,10 @@
651652
"source": [
652653
"%%capture cap --no-stderr\n",
653654
"\n",
654-
"from power_grid_model import PowerGridModel, CalculationType\n",
655+
"from power_grid_model import CalculationType, PowerGridModel\n",
655656
"from power_grid_model.validation import assert_valid_input_data\n",
656-
"from power_grid_model_io.converters import VisionExcelConverter, PgmJsonConverter\n",
657+
"\n",
658+
"from power_grid_model_io.converters import PgmJsonConverter, VisionExcelConverter\n",
657659
"\n",
658660
"source_file = \"data/vision/example.xlsx\"\n",
659661
"destination_file = \"data/vision/sym_output.json\"\n",

pyproject.toml

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dev = [
4444
"pylint",
4545
"pytest",
4646
"pytest-cov",
47+
"ruff",
4748
"pydantic>2", # Used in unit tests
4849
"pandapower>2.11.1",
4950
]
@@ -89,33 +90,43 @@ addopts = [
8990
]
9091
xfail_strict = true
9192

92-
[tool.black]
93+
[tool.ruff]
94+
# Same as Black.
9395
line-length = 120
94-
target-version = ["py311", "py312", "py313"]
96+
indent-width = 4
97+
show-fixes = true
9598

96-
[tool.isort]
97-
profile = "black"
98-
line_length = 120
99+
# Assume Python 3.11
100+
target-version = "py311"
99101

100-
[tool.pylint]
101-
max-line-length = 120
102-
ignore-paths = [
103-
"docs/",
104-
"examples/",
105-
"tests/",
106-
"setup.py",
107-
]
108-
disable = [
109-
"fixme", # allow todos
110-
]
111-
good-names=[
112-
"ex", # exception
113-
"i", # iterator or current
114-
"p", # active power
115-
"q", # reactive power
116-
"s", # power (p + q)
117-
"v", # voltage
102+
[tool.ruff.lint]
103+
select = [
104+
# pycodestyle
105+
"E",
106+
# Pyflakes
107+
"F",
108+
# isort
109+
"I",
110+
"SIM",
111+
"YTT",
112+
"BLE",
113+
"PERF",
114+
"Q",
115+
"ICN",
116+
"ISC",
117+
"G",
118+
"LOG",
119+
"EXE",
120+
"FA",
121+
"FURB",
122+
"FLY",
123+
"SLOT",
118124
]
125+
ignore = ["SIM108", "SIM118", "SIM110", "SIM211"]
126+
127+
[tool.ruff.lint.isort]
128+
# Imports that are imported using keyword "as" and are from the same source - are combined.
129+
combine-as-imports = true
119130

120131
[tool.mypy]
121132
follow_imports = "silent"

set_pypi_version.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import os
1010
from pathlib import Path
11-
from typing import cast
1211

1312
import requests
1413

@@ -26,7 +25,7 @@ def set_version(pkg_dir: Path):
2625
ref = os.environ["GITHUB_REF"]
2726
build_number = os.environ["GITHUB_RUN_NUMBER"]
2827
# short hash number in numeric
29-
short_hash = f'{int(f"0x{sha[0:6]}", base=16):08}'
28+
short_hash = f"{int(f'0x{sha[0:6]}', base=16):08}"
3029

3130
if "main" in ref:
3231
# main branch

src/power_grid_model_io/converters/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
from power_grid_model_io.converters.pandapower_converter import PandaPowerConverter
99
from power_grid_model_io.converters.pgm_json_converter import PgmJsonConverter
1010
from power_grid_model_io.converters.vision_excel_converter import VisionExcelConverter
11+
12+
__all__ = ["PandaPowerConverter", "PgmJsonConverter", "VisionExcelConverter"]

src/power_grid_model_io/converters/base_converter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
Abstract converter class
66
"""
7+
78
import logging
89
from abc import ABC, abstractmethod
910
from typing import Generic, Optional, Tuple, TypeVar

0 commit comments

Comments
 (0)