Skip to content

Commit f889967

Browse files
authored
Merge pull request #16 from debpal/main
`mypy` Static Type Checking
2 parents ec6dc5a + ec38ad4 commit f889967

8 files changed

Lines changed: 44 additions & 35 deletions

File tree

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
21+
python-version: ["3.10", "3.11", "3.12", "3.13"]
2222

2323
steps:
2424
- uses: actions/checkout@v4

pySWATPlus/FileReader.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
def read_csv(
1111
path: Union[str, Path],
12-
skip_rows: List[int],
13-
usecols: List[str],
14-
filter_by: Dict[str, Union[Any, List[Any], re.Pattern]],
12+
skip_rows: list[int],
13+
usecols: list[str],
14+
filter_by: dict[str, Union[Any, list[Any], re.Pattern[str]]],
1515
separator: str,
1616
encoding: str,
1717
engine: Literal['c', 'python'],
@@ -97,11 +97,11 @@ class FileReader:
9797

9898
def __init__(
9999
self,
100-
path: str,
100+
path: str | os.PathLike,
101101
has_units: bool = False,
102102
index: Optional[str] = None,
103-
usecols: List[str] = None,
104-
filter_by: Dict[str, Union[Any, List[Any], re.Pattern]] = {}
103+
usecols: Optional[List[str]] = None,
104+
filter_by: Dict[str, Union[Any, List[Any], re.Pattern[str]]] = {}
105105
):
106106

107107
'''

pySWATPlus/PymooBestSolution.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import itertools
33
import shutil
44
import multiprocessing
5-
from typing import Dict, List, Tuple
65

76

87
class SolutionManager:
@@ -13,7 +12,7 @@ class SolutionManager:
1312

1413
def __init__(
1514
self
16-
):
15+
) -> None:
1716

1817
self.X = None
1918
self.path = None
@@ -23,7 +22,7 @@ def __init__(
2322
def add_solution(
2423
self,
2524
X: np.ndarray,
26-
path: Dict[str, str],
25+
path: dict[str, str],
2726
error: float
2827
) -> None:
2928

@@ -39,7 +38,7 @@ def add_solution(
3938

4039
def get_solution(
4140
self
42-
) -> Tuple[np.ndarray, Dict[str, str], float]:
41+
) -> tuple[np.ndarray, dict[str, str], float]:
4342

4443
"""
4544
Retrieve the best solution.
@@ -51,7 +50,7 @@ def get_solution(
5150
def add_solutions(
5251
self,
5352
X_array: np.ndarray,
54-
paths_array: List[Dict[str, str]],
53+
paths_array: list[dict[str, str]],
5554
errors_array: np.ndarray
5655
) -> None:
5756

pySWATPlus/SWATProblem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from typing import Callable, Tuple, Any, Dict, List
1+
import typing
22
from .SWATProblemMultimodel import SWATProblemMultimodel
33

44

55
class SWATProblem(SWATProblemMultimodel):
66

77
def __init__(
88
self,
9-
params: Dict[str, Tuple[str, List[Tuple[str, str, float, float]]]],
10-
function_to_evaluate: Callable,
9+
params: dict[str, tuple[str, list[tuple[str, str, float, float]]]],
10+
function_to_evaluate: typing.Callable[[dict[typing.Any, typing.Any]], tuple[float, dict[str, str]]],
1111
param_arg_name: str,
1212
n_workers: int = 1,
1313
parallelization: str = 'threads',
1414
debug: bool = False,
15-
**kwargs: Dict[str, Any]
15+
**kwargs: dict[str, typing.Any]
1616
) -> None:
1717

1818
"""

pySWATPlus/SWATProblemMultimodel.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .PymooBestSolution import SolutionManager
33
import copy
44
import numpy as np
5-
from typing import Optional, Callable, Tuple, Any, Dict, List
5+
from typing import Optional, Callable, Any
66
from pymoo.optimize import minimize
77
from concurrent.futures import ThreadPoolExecutor
88
import multiprocessing
@@ -16,7 +16,7 @@ def minimize_pymoo(
1616
seed: Optional[int] = None,
1717
verbose: bool = False,
1818
callback: Optional[Callable] = None
19-
) -> Tuple[Optional[np.ndarray], Optional[str], Optional[float]]:
19+
) -> tuple[Optional[np.ndarray], Optional[str], Optional[float]]:
2020

2121
"""
2222
Perform optimization using the pymoo library.
@@ -64,18 +64,18 @@ class SWATProblemMultimodel(Problem):
6464

6565
def __init__(
6666
self,
67-
params: Dict[str, Tuple[str, List[Tuple[str, str, float, float]]]],
67+
params: dict[str, tuple[str, list[tuple[str, str, float, float]]]],
6868
function_to_evaluate: Callable,
6969
param_arg_name: str,
7070
n_workers: int = 1,
7171
parallelization: str = 'threads',
72-
ub_prior: Optional[List[int]] = None,
73-
lb_prior: Optional[List[int]] = None,
72+
ub_prior: Optional[list[int]] = None,
73+
lb_prior: Optional[list[int]] = None,
7474
function_to_evaluate_prior: Optional[Callable] = None,
75-
args_function_to_evaluate_prior: Optional[Dict[str, Any]] = None,
75+
args_function_to_evaluate_prior: Optional[dict[str, Any]] = None,
7676
param_arg_name_to_modificate_by_prior_function: Optional[str] = None,
7777
debug: bool = False,
78-
**kwargs: Dict[str, Any]
78+
**kwargs: dict[str, Any]
7979
) -> None:
8080

8181
"""
@@ -143,10 +143,10 @@ def __init__(
143143
def _evaluate(
144144
self,
145145
X: np.ndarray,
146-
out: Dict[str, Any],
146+
out: dict[str, Any],
147147
*args: Any,
148148
**kwargs: Any
149-
):
149+
) -> None:
150150

151151
"""
152152
Evaluate the objective function for a given set of input parameters.

pySWATPlus/TxtinoutReader.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TxtinoutReader:
1515

1616
def __init__(
1717
self,
18-
path: str
18+
path: Union[str, os.PathLike]
1919
) -> None:
2020

2121
"""
@@ -298,7 +298,7 @@ def register_file(
298298
has_units: bool = False,
299299
index: Optional[str] = None,
300300
usecols: Optional[List[str]] = None,
301-
filter_by: Dict[str, Union[Any, List[Any], re.Pattern]] = {}
301+
filter_by: dict[str, Union[Any, list[Any], re.Pattern]] = {}
302302
) -> FileReader:
303303

304304
"""
@@ -451,7 +451,7 @@ def _run_swat(
451451

452452
def run_swat(
453453
self,
454-
params: Dict[str, Tuple[str, List[Tuple[Union[None, str, List[str], re.Pattern], str, Any]]]] = {},
454+
params: dict[str, tuple[str, list[tuple[Union[None, str, list[str], re.Pattern[str]], str, Any]]]] = {},
455455
show_output: bool = True
456456
) -> str:
457457

@@ -501,7 +501,7 @@ def run_swat(
501501

502502
def run_swat_star(
503503
self,
504-
args: Tuple[Dict[str, Tuple[str, List[Tuple[Union[None, str, List[str], re.Pattern], str, Any]]]], bool]
504+
args: tuple[dict[str, tuple[str, list[tuple[Union[None, str, list[str], re.Pattern[str]], str, Any]]]], bool]
505505
) -> str:
506506

507507
"""
@@ -523,7 +523,7 @@ def copy_and_run(
523523
self,
524524
target_dir: str,
525525
overwrite: bool = False,
526-
params: Dict[str, Tuple[str, List[Tuple[Union[None, str, List[str], re.Pattern], str, Any]]]] = {},
526+
params: dict[str, tuple[str, list[tuple[Union[None, str, list[str], re.Pattern[str]], str, Any]]]] = {},
527527
show_output: bool = True
528528
) -> str:
529529

@@ -549,7 +549,7 @@ def copy_and_run(
549549

550550
def copy_and_run_star(
551551
self,
552-
args: Tuple[str, bool, Dict[str, Tuple[str, List[Tuple[Union[None, str, List[str], re.Pattern], str, Any]]]], bool]
552+
args: Tuple[str, bool, Dict[str, Tuple[str, List[Tuple[Union[None, str, List[str], re.Pattern[str]], str, Any]]]], bool]
553553
) -> str:
554554

555555
"""
@@ -569,9 +569,9 @@ def copy_and_run_star(
569569

570570
def run_parallel_swat(
571571
self,
572-
params: List[Dict[str, Tuple[str, List[Tuple[Union[None, str, List[str], re.Pattern], str, Any]]]]],
572+
params: list[dict[str, tuple[str, list[tuple[Union[None, str, list[str], re.Pattern[str]], str, Any]]]]],
573573
n_workers: int = 1,
574-
target_dir: str = None,
574+
target_dir: Optional[str] = None,
575575
parallelization: str = 'threads'
576576
) -> List[str]:
577577

pyproject.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "pySWATPlus"
77
description = "Running and calibrating default or custom SWAT+ projects with Python"
88
dynamic = ["version"]
99
readme = "README.md"
10-
requires-python = ">=3.9"
10+
requires-python = ">=3.10"
1111
license = {text = "GPL-3.0"}
1212
authors = [
1313
{ name = "Joan Saló", email = "joansalograu@gmail.com" }
@@ -54,4 +54,12 @@ testpaths = [
5454

5555
[tool.setuptools_scm]
5656
version_scheme = "post-release"
57-
local_scheme = "no-local-version"
57+
local_scheme = "no-local-version"
58+
59+
60+
[tool.mypy]
61+
files = [
62+
"pySWATPlus"
63+
]
64+
ignore_missing_imports = true
65+
strict = true

tests/test_filereader.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ def test_get_df(
3030
assert df.shape[0] == 260
3131

3232

33+
def test_github():
3334

35+
assert str(1) == '1'

0 commit comments

Comments
 (0)