Skip to content

Commit

Permalink
[commons] Cleanup leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Aug 23, 2021
1 parent 102e9e0 commit 1a0060b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 81 deletions.
51 changes: 28 additions & 23 deletions openfisca_core/commons/_types.py
Original file line number Diff line number Diff line change
@@ -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]]
24 changes: 16 additions & 8 deletions openfisca_core/commons/rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ def average_rate(
Args:
target (ndarray): The targeted net income.
varying (:obj:`ArrayLike[float]`): The varying gross income.
trim (:obj:`ArrayLike[float]`, optional): The lower and upper bounds of
the average rate.
target (ndarray):
The targeted net income.
varying (:obj:`ArrayLike[float]`):
The varying gross income.
trim (:obj:`ArrayLike[float]`, optional):
The lower and upper bounds of the average rate.
Returns:
Expand Down Expand Up @@ -70,10 +74,14 @@ def marginal_rate(
Args:
target (ndarray): The targeted net income.
varying (:obj:`ArrayLike[float]`): The varying gross income.
trim (:obj:`ArrayLike[float]`, optional): The lower and upper bounds of
the marginal rate.
target (ndarray):
The targeted net income.
varying (:obj:`ArrayLike[float]`):
The varying gross income.
trim (:obj:`ArrayLike[float]`, optional):
The lower and upper bounds of the marginal rate.
Returns:
Expand Down
16 changes: 14 additions & 2 deletions openfisca_core/commons/tests/test_formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from openfisca_core import commons


def test_apply_thresholds_when_two_or_more_inputs():
def test_apply_thresholds_when_several_inputs():
"""Makes a choice for any given input."""

input_ = numpy.array([4, 5, 6, 7, 8, 9, 10])
Expand All @@ -19,7 +19,19 @@ def test_apply_thresholds_when_two_or_more_inputs():
assert_array_equal(result, [10, 10, 15, 15, 20, 20, 25])


def test_apply_thresholds_when_two_or_more_choices():
def test_apply_thresholds_when_too_many_thresholds():
"""Raises an AssertionError when thresholds > 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])
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
47 changes: 0 additions & 47 deletions tests/core/test_formula_helpers.py

This file was deleted.

0 comments on commit 1a0060b

Please sign in to comment.