diff --git a/openfisca_core/commons/_types.py b/openfisca_core/commons/_types.py index 56cce44d16..606b78215c 100644 --- a/openfisca_core/commons/_types.py +++ b/openfisca_core/commons/_types.py @@ -1,39 +1,44 @@ """Module's shared types. -Todo: +Note: - * Refactor once numpy version >= 1.21 is used. + It is possible since numpy version 1.21 to specify the type of an + array, thanks to :class:`numpy.typing.mypy_plugin.NDArray`: -""" + from numpy.typing import NDArray + NDArray[numpy.float64] + ... -from typing import TYPE_CHECKING + Also note that :module:`mypy` provides duck type compatibility, so an + :obj:`int` is considered to be valid whenever an :obj:`float` is expected. -if TYPE_CHECKING: - from typing import Sequence, TypeVar, Union +Attributes: - import numpy + _T (:obj:`typing.TypeVar`): + A 'wildcard' type used by other types. - """'Wildcard' type used by other types.""" - _T = TypeVar("_T", float, str) + ArrayLike (:obj:`typing._GenericAlias`): + The type of any object that can be coerced to a :obj:`numpy.array`. + These include any :obj:`numpy.array` and 'sequence' (like + :class:`list`, :class:`tuple`, and so on). - """Type of any object that can be casted to a :obj:`numpy.array`. +Examples: - These include any :obj:`numpy.array` and 'sequence' (like :class:`list`, - :class:`tuple`, and so on). + >>> ArrayLike[float] + typing.Union[numpy.ndarray, typing.Sequence[float]] - Note: + >>> ArrayLike[str] + typing.Union[numpy.ndarray, typing.Sequence[str]] - It is possible since numpy version 1.21 to specify the type of an - array, thanks to :class:`numpy.typing.mypy_plugin.NDArray`. +Todo: - Examples: + * Refactor once numpy version >= 1.21 is used. + +""" - >>> ArrayLike[float] - typing.Union[numpy.ndarray, typing.Sequence[float]] +from typing import Sequence, TypeVar, Union - >>> from numpy.typing import NDArray - >>> NDArray[numpy.float64] - numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]] +import numpy - """ - ArrayLike = Union[numpy.ndarray, Sequence[_T]] +_T = TypeVar("_T", float, str) +ArrayLike = Union[numpy.ndarray, Sequence[_T]] diff --git a/openfisca_core/commons/dummy.py b/openfisca_core/commons/dummy.py index 9fc7306eea..76b3f7b75b 100644 --- a/openfisca_core/commons/dummy.py +++ b/openfisca_core/commons/dummy.py @@ -18,6 +18,7 @@ class Dummy: choices.""" + + input_ = numpy.array([6]) + thresholds = [5, 7, 9, 11] + choices = [10, 15, 20] + + with pytest.raises(AssertionError): + result = commons.apply_thresholds(input_, thresholds, choices) + assert result + + +def test_apply_thresholds_when_too_many_choices(): """Raises an AssertionError when thresholds < choices - 1.""" input_ = numpy.array([6]) diff --git a/setup.cfg b/setup.cfg index d29fdeb24d..9fa3fbfd87 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,7 +13,7 @@ docstring-convention = google docstring_style = google hang-closing = true in-place = true -rst-roles = class, obj, attr +rst-roles = module, class, obj, attr rst-directives = deprecated strictness = long ignore = diff --git a/tests/core/test_formula_helpers.py b/tests/core/test_formula_helpers.py deleted file mode 100644 index 51bc2a2e20..0000000000 --- a/tests/core/test_formula_helpers.py +++ /dev/null @@ -1,47 +0,0 @@ -# -*- coding: utf-8 -*- - -import numpy -import pytest - -from openfisca_core.formula_helpers import apply_thresholds as apply_thresholds -from openfisca_core.tools import assert_near - - -def test_apply_thresholds_with_too_many_thresholds(): - input = numpy.array([10]) - thresholds = [5, 4] - choice_list = [10] - with pytest.raises(AssertionError): - return apply_thresholds(input, thresholds, choice_list) - - -def test_apply_thresholds_with_too_few_thresholds(): - input = numpy.array([10]) - thresholds = [5] - choice_list = [10, 15, 20] - with pytest.raises(AssertionError): - return apply_thresholds(input, thresholds, choice_list) - - -def test_apply_thresholds(): - input = numpy.array([4, 5, 6, 7, 8]) - thresholds = [5, 7] - choice_list = [10, 15, 20] - result = apply_thresholds(input, thresholds, choice_list) - assert_near(result, [10, 10, 15, 15, 20]) - - -def test_apply_thresholds_with_as_many_thresholds_than_choices(): - input = numpy.array([4, 6, 8]) - thresholds = [5, 7] - choice_list = [10, 20] - result = apply_thresholds(input, thresholds, choice_list) - assert_near(result, [10, 20, 0]) - - -def test_apply_thresholds_with_variable_threshold(): - input = numpy.array([1000, 1000, 1000]) - thresholds = [numpy.array([500, 1500, 1000])] # Only one thresold, but varies with the person - choice_list = [True, False] # True if input <= threshold, false otherwise - result = apply_thresholds(input, thresholds, choice_list) - assert_near(result, [False, True, True])