Skip to content

Commit fa55ea6

Browse files
Merge pull request #106 from alliander-opensource/feature/mypy
Add mypy to pre-commit (and introduce data_types.py)
2 parents 3f09476 + 4ab8537 commit fa55ea6

29 files changed

+1639
-1326
lines changed

.github/workflows/black-and-clang-format.yml renamed to .github/workflows/check-code-quality.yml

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55

66
# GitHub Action that uses
7-
# Black to reformat the Python code in an incoming pull request.
7+
# isort, black, mypy and pylint to reformat the Python code in an incoming pull request.
88
# clang-format to reformat the C++ code in an incoming pull request.
99
# If all code in the pull request is compliant with Black and clang-format then this Action
1010
# does nothing. Otherwise, it will print the files which need to be reformatted and raise an error.
1111

12-
name: Format Code
12+
name: Check Code Quality
1313

1414
on:
1515
# run pipeline on push event of main or release branch
@@ -21,7 +21,7 @@ on:
2121
pull_request:
2222

2323
jobs:
24-
code-format-check:
24+
check-code-quality:
2525
if: (github.event_name == 'push') || (!startsWith(github.head_ref, 'release'))
2626
runs-on: ubuntu-latest
2727

@@ -33,23 +33,41 @@ jobs:
3333
uses: actions/setup-python@v4
3434
with:
3535
python-version: 3.8
36-
37-
- name: Install Black and clang-format
36+
37+
- name: Upgrade pip
38+
run: pip install --upgrade pip
39+
40+
- name: Install and run isort
41+
run: |
42+
pip install isort
43+
isort .
44+
45+
- name: Install and run black
3846
run: |
3947
pip install black
40-
sudo apt-get update && sudo apt-get install -y clang-format
48+
black .
4149
42-
- name: Run black
43-
run: black .
50+
- name: Install and run mypy
51+
run: |
52+
pip install mypy
53+
mypy .
54+
55+
- name: Install and run pylint
56+
run: |
57+
pip install pylint .
58+
pylint power_grid_model
59+
git restore README.md
4460
45-
- name: Run clang-format
46-
run: find . -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -style=file -i {} \;
61+
- name: Install and run clang-format
62+
run: |
63+
sudo apt-get update && sudo apt-get install -y clang-format
64+
find . -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -style=file -i {} \;
4765
4866
- name: If needed raise error
4967
run: |
5068
5169
if [[ `git status --porcelain --untracked-files=no` ]]; then
52-
echo "Formatting not correct! See blow the files which need to be reformatted!"
70+
echo "Formatting not correct! See below the files which need to be reformatted!"
5371
git status --porcelain --untracked-files=no
5472
exit 1
5573
fi

.pre-commit-config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ repos:
1010
- repo: https://github.com/pycqa/isort
1111
rev: 5.10.1
1212
hooks:
13-
- id: isort
13+
- id: isort
1414
- repo: https://github.com/psf/black
1515
rev: 22.6.0
1616
hooks:
1717
- id: black
18+
- repo: https://github.com/pre-commit/mirrors-mypy
19+
rev: v0.971
20+
hooks:
21+
- id: mypy
22+
files: ^(src|tests|scripts)/.+\.py$
1823
- repo: local
1924
hooks:
2025
- id: pylint

CONTRIBUTING.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ corrections) to your code (style) before each commit. It is up to the developer
6464
use this tool or not. The goal is to make sure that each commit will pass the quality checks in the github actions
6565
workflow. Currently, these hooks are defined in [`.pre-commit-config.yaml`](.pre-commit-config.yaml):
6666
* **reuse**: check if all licence headers and files are in place
67-
* **isort**: sort import statements
68-
* **black**: check and correct code style
69-
* **pylint**: check code style
67+
* **isort**: group and sort import statements
68+
* **black**: check and correct code style in a very strict manner
69+
* **mypy**: checks type hinting and data types in general (static type checker)
70+
* **pylint**: check code style and comments
7071
* **pytest**: run all unit tests
7172

7273
You can manually run pre-commit whenever you like:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SPDX-License-Identifier: MPL-2.0
66
[![PyPI version](https://badge.fury.io/py/power-grid-model.svg)](https://badge.fury.io/py/power-grid-model)
77
[![License: MIT](https://img.shields.io/badge/License-MPL2.0-informational.svg)](https://github.com/alliander-opensource/power-grid-model/blob/main/LICENSE)
88
[![Build and Test C++ and Python](https://github.com/alliander-opensource/power-grid-model/actions/workflows/main.yml/badge.svg)](https://github.com/alliander-opensource/power-grid-model/actions/workflows/main.yml)
9-
[![Format Code](https://github.com/alliander-opensource/power-grid-model/actions/workflows/black-and-clang-format.yml/badge.svg)](https://github.com/alliander-opensource/power-grid-model/actions/workflows/black-and-clang-format.yml)
9+
[![Check Code Quality](https://github.com/alliander-opensource/power-grid-model/actions/workflows/check-code-quality.yml/badge.svg)](https://github.com/alliander-opensource/power-grid-model/actions/workflows/check-code-quality.yml)
1010
[![REUSE Compliance Check](https://github.com/alliander-opensource/power-grid-model/actions/workflows/reuse-compliance.yml/badge.svg)](https://github.com/alliander-opensource/power-grid-model/actions/workflows/reuse-compliance.yml)
1111

1212
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=alliander-opensource_power-grid-model&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=alliander-opensource_power-grid-model)

docs/python-api-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The Python API consists of the following components:
1717
This is a pure Python module.
1818
* `power_grid_model.PowerGridModel`: the main class.
1919
This is inside a C++ extension module.
20-
* `power_grid_model.manual_testing`: containing the functions for load and save test dataset.
20+
* `power_grid_model.utils`: containing the functions for load and save test dataset.
2121
See [Make Test Dataset](../examples/Make%20Test%20Dataset.ipynb) for examples of how to make test datasets.
2222
* `power_grid_model.validation`: optional validation and assertion functions.
2323
See [Validation Examples](../examples/Validation%20Examples.ipynb) for more information on how to validate input

examples/Make Test Dataset.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@
342342
"source": [
343343
"# Helper Functions to Import and Export\n",
344344
"\n",
345-
"In the module `power_grid_model.manual_testing` we have some helper functions to import a json file to a `power-grid-model` compatible dataset, or the other way around. \n",
345+
"In the module `power_grid_model.utils` we have some helper functions to import a json file to a `power-grid-model` compatible dataset, or the other way around. \n",
346346
"\n",
347-
"Please refer to the [source code](../src/power_grid_model/manual_testing.py) for detailed function signature.\n",
347+
"Please refer to the [source code](../src/power_grid_model/utils.py) for detailed function signature.\n",
348348
"\n",
349349
"In this notebook we export the example network from [Power Flow](./Power%20Flow%20Example.ipynb) to json. "
350350
]
@@ -422,7 +422,7 @@
422422
"metadata": {},
423423
"outputs": [],
424424
"source": [
425-
"from power_grid_model.manual_testing import export_json_data\n",
425+
"from power_grid_model.utils import export_json_data\n",
426426
"import tempfile\n",
427427
"from pathlib import Path\n",
428428
"\n",
@@ -560,7 +560,7 @@
560560
"source": [
561561
"# round trip and run power flow\n",
562562
"\n",
563-
"from power_grid_model.manual_testing import import_json_data\n",
563+
"from power_grid_model.utils import import_json_data\n",
564564
"\n",
565565
"imported_data = import_json_data(temp_path / \"input.json\", \"input\")\n",
566566
"\n",

pyproject.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dynamic = ["version"]
4646
[project.optional-dependencies]
4747
dev = [
4848
"pre-commit",
49+
"pylint",
4950
"pytest",
5051
"pytest-cov",
5152
]
@@ -69,8 +70,15 @@ line-length = 120
6970
target-version = ['py38']
7071

7172
[tool.isort]
72-
src_paths = ["src", "tests/unit", "scripts"]
7373
profile = "black"
74+
line_length = 120
7475

7576
[tool.pylint]
7677
max-line-length = 120
78+
79+
[tool.mypy]
80+
follow_imports = "silent"
81+
ignore_missing_imports = true
82+
show_column_numbers = true
83+
non_interactive = true
84+
install_types = true

scripts/validate_batch_data.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@
33
# SPDX-License-Identifier: MPL-2.0
44

55
from pathlib import Path
6-
from typing import cast
76

87
from power_grid_model import CalculationType
9-
from power_grid_model.manual_testing import import_json_data
10-
from power_grid_model.validation import (
11-
InputData,
12-
UpdateData,
13-
errors_to_string,
14-
validate_batch_data,
15-
)
8+
from power_grid_model.utils import import_input_data, import_update_data
9+
from power_grid_model.validation import errors_to_string, validate_batch_data
1610

1711
input_file = Path("../tests/data/power_flow/dummy-test-batch/input.json")
1812
update_file = Path("../tests/data/power_flow/dummy-test-batch/update_batch.json")
1913

20-
input_data = cast(InputData, import_json_data(json_file=input_file, data_type="input"))
21-
update_data = cast(UpdateData, import_json_data(json_file=update_file, data_type="update"))
14+
input_data = import_input_data(json_file=input_file)
15+
update_data = import_update_data(json_file=update_file)
2216

2317
update_errors = validate_batch_data(
2418
input_data=input_data, update_data=update_data, calculation_type=CalculationType.power_flow, symmetric=True

scripts/validate_input_data.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
# SPDX-License-Identifier: MPL-2.0
44

55
from pathlib import Path
6-
from typing import cast
76

87
from power_grid_model import CalculationType
9-
from power_grid_model.manual_testing import import_json_data
10-
from power_grid_model.validation import InputData, errors_to_string, validate_input_data
8+
from power_grid_model.utils import import_input_data
9+
from power_grid_model.validation import errors_to_string, validate_input_data
1110

1211
input_file = Path("../tests/data/state_estimation/dummy-test-sym/input.json")
1312

14-
input_data = cast(InputData, import_json_data(json_file=input_file, data_type="input"))
13+
input_data = import_input_data(json_file=input_file)
1514

1615
input_errors = validate_input_data(
1716
input_data=input_data, calculation_type=CalculationType.state_estimation, symmetric=True

setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from itertools import chain
1010
from pathlib import Path
1111
from sysconfig import get_paths
12+
from typing import List
1213

1314
# noinspection PyPackageRequirements
1415
import Cython.Compiler.Main as CythonCompiler
@@ -88,10 +89,10 @@ def generate_build_ext(pkg_dir: Path, pkg_name: str):
8889
str(pkg_dir / "include"), # The include-folder of the repo self
8990
]
9091
# compiler and link flag
91-
cflags = []
92-
lflags = []
93-
library_dirs = []
94-
libraries = []
92+
cflags: List[str] = []
93+
lflags: List[str] = []
94+
library_dirs: List[str] = []
95+
libraries: List[str] = []
9596
# macro
9697
define_macros = [
9798
("EIGEN_MPL2_ONLY", "1"), # only MPL-2 part of eigen3

0 commit comments

Comments
 (0)