Skip to content

Commit a694d28

Browse files
committed
fix(types): commons
1 parent 077a4af commit a694d28

File tree

5 files changed

+28
-23
lines changed

5 files changed

+28
-23
lines changed

openfisca_core/commons/formulas.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
from typing import Any, Dict, Sequence, TypeVar
1+
from typing import Any, Dict, Union
22

33
from openfisca_core.types import Array, ArrayLike
44

55
import numpy
66

7-
T = TypeVar("T")
8-
97

108
def apply_thresholds(
11-
input: Array[float],
9+
input: Array[numpy.float_],
1210
thresholds: ArrayLike[float],
1311
choices: ArrayLike[float],
14-
) -> Array[float]:
12+
) -> Array[numpy.float_]:
1513
"""Makes a choice based on an input and thresholds.
1614
1715
From a list of ``choices``, this function selects one of these values
@@ -40,7 +38,7 @@ def apply_thresholds(
4038
4139
"""
4240

43-
condlist: Sequence[Array[bool]]
41+
condlist: list[Union[Array[numpy.bool_], bool]]
4442
condlist = [input <= threshold for threshold in thresholds]
4543

4644
if len(condlist) == len(choices) - 1:
@@ -58,7 +56,9 @@ def apply_thresholds(
5856
return numpy.select(condlist, choices)
5957

6058

61-
def concat(this: ArrayLike[str], that: ArrayLike[str]) -> Array[str]:
59+
def concat(
60+
this: Union[Array[Any], ArrayLike[str]], that: Union[Array[Any], ArrayLike[str]]
61+
) -> Array[numpy.str_]:
6262
"""Concatenates the values of two arrays.
6363
6464
Args:
@@ -88,8 +88,8 @@ def concat(this: ArrayLike[str], that: ArrayLike[str]) -> Array[str]:
8888

8989
def switch(
9090
conditions: Array[Any],
91-
value_by_condition: Dict[float, T],
92-
) -> Array[T]:
91+
value_by_condition: Dict[float, Any],
92+
) -> Array[Any]:
9393
"""Mimicks a switch statement.
9494
9595
Given an array of conditions, returns an array of the same size,
@@ -120,4 +120,4 @@ def switch(
120120

121121
condlist = [conditions == condition for condition in value_by_condition.keys()]
122122

123-
return numpy.select(condlist, value_by_condition.values())
123+
return numpy.select(condlist, tuple(value_by_condition.values()))

openfisca_core/commons/misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypeVar
1+
from typing import Any, TypeVar, Union
22

33
from openfisca_core.types import Array
44

@@ -43,7 +43,7 @@ def empty_clone(original: T) -> T:
4343
return new
4444

4545

46-
def stringify_array(array: Array) -> str:
46+
def stringify_array(array: Union[Array[Any], None]) -> str:
4747
"""Generates a clean string representation of a numpy array.
4848
4949
Args:

openfisca_core/commons/rates.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77

88
def average_rate(
9-
target: Array[float],
9+
target: Array[numpy.float_],
1010
varying: ArrayLike[float],
1111
trim: Optional[ArrayLike[float]] = None,
12-
) -> Array[float]:
12+
) -> Array[numpy.float_]:
1313
"""Computes the average rate of a target net income.
1414
1515
Given a ``target`` net income, and according to the ``varying`` gross
@@ -41,7 +41,7 @@ def average_rate(
4141
4242
"""
4343

44-
average_rate: Array[float]
44+
average_rate: Array[numpy.float_]
4545

4646
average_rate = 1 - target / varying
4747

@@ -62,10 +62,10 @@ def average_rate(
6262

6363

6464
def marginal_rate(
65-
target: Array[float],
66-
varying: Array[float],
65+
target: Array[numpy.float_],
66+
varying: Array[numpy.float_],
6767
trim: Optional[ArrayLike[float]] = None,
68-
) -> Array[float]:
68+
) -> Array[numpy.float_]:
6969
"""Computes the marginal rate of a target net income.
7070
7171
Given a ``target`` net income, and according to the ``varying`` gross
@@ -97,7 +97,7 @@ def marginal_rate(
9797
9898
"""
9999

100-
marginal_rate: Array[float]
100+
marginal_rate: Array[numpy.float_]
101101

102102
marginal_rate = +1 - (target[:-1] - target[1:]) / (varying[:-1] - varying[1:])
103103

openfisca_tasks/lint.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ lint-doc-%:
3939
## Run static type checkers for type errors.
4040
check-types:
4141
@$(call print_help,$@:)
42-
@mypy openfisca_core/entities openfisca_core/projectors
42+
@mypy \
43+
openfisca_core/commons \
44+
openfisca_core/types.py
4345
@$(call print_pass,$@:)
4446

4547
## Run code formatters to correct style errors.

setup.cfg

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ python_files = **/*.py
6565
testpaths = tests
6666

6767
[mypy]
68-
ignore_missing_imports = True
69-
install_types = True
70-
non_interactive = True
68+
disallow_any_unimported = true
69+
ignore_missing_imports = true
70+
install_types = true
71+
non_interactive = true
72+
plugins = numpy.typing.mypy_plugin
73+
python_version = 3.9
7174

7275
[mypy-openfisca_core.commons.tests.*]
7376
ignore_errors = True

0 commit comments

Comments
 (0)