Skip to content

Commit

Permalink
fix(types): commons
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Sep 16, 2024
1 parent 077a4af commit a694d28
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
20 changes: 10 additions & 10 deletions openfisca_core/commons/formulas.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from typing import Any, Dict, Sequence, TypeVar
from typing import Any, Dict, Union

from openfisca_core.types import Array, ArrayLike

import numpy

T = TypeVar("T")


def apply_thresholds(
input: Array[float],
input: Array[numpy.float_],
thresholds: ArrayLike[float],
choices: ArrayLike[float],
) -> Array[float]:
) -> Array[numpy.float_]:
"""Makes a choice based on an input and thresholds.
From a list of ``choices``, this function selects one of these values
Expand Down Expand Up @@ -40,7 +38,7 @@ def apply_thresholds(
"""

condlist: Sequence[Array[bool]]
condlist: list[Union[Array[numpy.bool_], bool]]
condlist = [input <= threshold for threshold in thresholds]

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


def concat(this: ArrayLike[str], that: ArrayLike[str]) -> Array[str]:
def concat(
this: Union[Array[Any], ArrayLike[str]], that: Union[Array[Any], ArrayLike[str]]
) -> Array[numpy.str_]:
"""Concatenates the values of two arrays.
Args:
Expand Down Expand Up @@ -88,8 +88,8 @@ def concat(this: ArrayLike[str], that: ArrayLike[str]) -> Array[str]:

def switch(
conditions: Array[Any],
value_by_condition: Dict[float, T],
) -> Array[T]:
value_by_condition: Dict[float, Any],
) -> Array[Any]:
"""Mimicks a switch statement.
Given an array of conditions, returns an array of the same size,
Expand Down Expand Up @@ -120,4 +120,4 @@ def switch(

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

return numpy.select(condlist, value_by_condition.values())
return numpy.select(condlist, tuple(value_by_condition.values()))
4 changes: 2 additions & 2 deletions openfisca_core/commons/misc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypeVar
from typing import Any, TypeVar, Union

from openfisca_core.types import Array

Expand Down Expand Up @@ -43,7 +43,7 @@ def empty_clone(original: T) -> T:
return new


def stringify_array(array: Array) -> str:
def stringify_array(array: Union[Array[Any], None]) -> str:
"""Generates a clean string representation of a numpy array.
Args:
Expand Down
14 changes: 7 additions & 7 deletions openfisca_core/commons/rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@


def average_rate(
target: Array[float],
target: Array[numpy.float_],
varying: ArrayLike[float],
trim: Optional[ArrayLike[float]] = None,
) -> Array[float]:
) -> Array[numpy.float_]:
"""Computes the average rate of a target net income.
Given a ``target`` net income, and according to the ``varying`` gross
Expand Down Expand Up @@ -41,7 +41,7 @@ def average_rate(
"""

average_rate: Array[float]
average_rate: Array[numpy.float_]

average_rate = 1 - target / varying

Expand All @@ -62,10 +62,10 @@ def average_rate(


def marginal_rate(
target: Array[float],
varying: Array[float],
target: Array[numpy.float_],
varying: Array[numpy.float_],
trim: Optional[ArrayLike[float]] = None,
) -> Array[float]:
) -> Array[numpy.float_]:
"""Computes the marginal rate of a target net income.
Given a ``target`` net income, and according to the ``varying`` gross
Expand Down Expand Up @@ -97,7 +97,7 @@ def marginal_rate(
"""

marginal_rate: Array[float]
marginal_rate: Array[numpy.float_]

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

Expand Down
4 changes: 3 additions & 1 deletion openfisca_tasks/lint.mk
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ lint-doc-%:
## Run static type checkers for type errors.
check-types:
@$(call print_help,$@:)
@mypy openfisca_core/entities openfisca_core/projectors
@mypy \
openfisca_core/commons \
openfisca_core/types.py
@$(call print_pass,$@:)

## Run code formatters to correct style errors.
Expand Down
9 changes: 6 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ python_files = **/*.py
testpaths = tests

[mypy]
ignore_missing_imports = True
install_types = True
non_interactive = True
disallow_any_unimported = true
ignore_missing_imports = true
install_types = true
non_interactive = true
plugins = numpy.typing.mypy_plugin
python_version = 3.9

[mypy-openfisca_core.commons.tests.*]
ignore_errors = True
Expand Down

0 comments on commit a694d28

Please sign in to comment.