Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix doc & type definitions in the projectors module #1221

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Changelog

### 41.5.8 [#1220](https://github.com/openfisca/openfisca-core/pull/1221)

#### Technical changes

- Fix doc & type definitions in the projectors module

### 41.5.7 [#1224](https://github.com/openfisca/openfisca-core/pull/1224)

#### Technical changes

- Fix doc & type definitions in the enums module

### 41.5.6 [#1223](https://github.com/openfisca/openfisca-core/pull/1223)

#### Technical changes

- Update `pendulum'
- Remove run-time type-checks
- Add typing to the periods module

### 41.5.5 [#1220](https://github.com/openfisca/openfisca-core/pull/1220)

#### Technical changes
Expand Down
16 changes: 8 additions & 8 deletions openfisca_core/commons/formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

import numpy

from openfisca_core import types as t
from . import types as t


def apply_thresholds(
input: t.Array[numpy.float_],
input: t.Array[t.ArrayFloat],
thresholds: t.ArrayLike[float],
choices: t.ArrayLike[float],
) -> t.Array[numpy.float_]:
) -> t.Array[t.ArrayBool]:
"""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 @@ -39,7 +39,7 @@ def apply_thresholds(

"""

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

if len(condlist) == len(choices) - 1:
Expand All @@ -58,8 +58,8 @@ def apply_thresholds(


def concat(
this: Union[t.Array[numpy.str_], t.ArrayLike[str]],
that: Union[t.Array[numpy.str_], t.ArrayLike[str]],
this: Union[t.Array[t.ArrayStr], t.ArrayLike[str]],
that: Union[t.Array[t.ArrayStr], t.ArrayLike[str]],
) -> t.Array[numpy.str_]:
"""Concatenates the values of two arrays.

Expand Down Expand Up @@ -89,9 +89,9 @@ def concat(


def switch(
conditions: t.Array[numpy.float_],
conditions: t.Array[t.ArrayFloat],
value_by_condition: Mapping[float, float],
) -> t.Array[numpy.float_]:
) -> t.Array[t.ArrayFloat]:
"""Mimicks a switch statement.

Given an array of conditions, returns an array of the same size,
Expand Down
4 changes: 1 addition & 3 deletions openfisca_core/commons/misc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Optional, TypeVar

import numpy

from openfisca_core import types as t

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


def stringify_array(array: Optional[t.Array[numpy.generic]]) -> str:
def stringify_array(array: Optional[t.Array[t.ArrayAny]]) -> str:
"""Generates a clean string representation of a numpy array.

Args:
Expand Down
24 changes: 12 additions & 12 deletions openfisca_core/commons/rates.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Optional

from openfisca_core.types import Array, ArrayLike

import numpy

from . import types as t


def average_rate(
target: Array[numpy.float_],
varying: ArrayLike[float],
trim: Optional[ArrayLike[float]] = None,
) -> Array[numpy.float_]:
target: t.Array[t.ArrayFloat],
varying: t.ArrayLike[float],
trim: Optional[t.ArrayLike[float]] = None,
) -> t.Array[t.ArrayFloat]:
"""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[numpy.float_]
average_rate: t.Array[t.ArrayFloat]

average_rate = 1 - target / varying

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


def marginal_rate(
target: Array[numpy.float_],
varying: Array[numpy.float_],
trim: Optional[ArrayLike[float]] = None,
) -> Array[numpy.float_]:
target: t.Array[t.ArrayFloat],
varying: t.Array[t.ArrayFloat],
trim: Optional[t.ArrayLike[float]] = None,
) -> t.Array[t.ArrayFloat]:
"""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[numpy.float_]
marginal_rate: t.Array[t.ArrayFloat]

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

Expand Down
6 changes: 6 additions & 0 deletions openfisca_core/commons/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from openfisca_core.types import Array as Array # noqa: F401
from openfisca_core.types import ArrayAny as ArrayAny # noqa: F401
from openfisca_core.types import ArrayBool as ArrayBool # noqa: F401
from openfisca_core.types import ArrayFloat as ArrayFloat # noqa: F401
from openfisca_core.types import ArrayLike as ArrayLike # noqa: F401
from openfisca_core.types import ArrayStr as ArrayStr # noqa: F401
7 changes: 4 additions & 3 deletions openfisca_core/indexed_enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#
# See: https://www.python.org/dev/peps/pep-0008/#imports

from .config import ENUM_ARRAY_DTYPE # noqa: F401
from .enum import Enum # noqa: F401
from .enum_array import EnumArray # noqa: F401
from .enum import Enum
from .enum_array import EnumArray

__all__ = ["Enum", "EnumArray"]
3 changes: 0 additions & 3 deletions openfisca_core/indexed_enums/config.py

This file was deleted.

31 changes: 15 additions & 16 deletions openfisca_core/indexed_enums/enum.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from __future__ import annotations

from typing import Union

import enum

import numpy

from .config import ENUM_ARRAY_DTYPE
from . import types as t
from .enum_array import EnumArray


class Enum(enum.Enum):
class Enum(t.Enum):
"""
Enum based on `enum34 <https://pypi.python.org/pypi/enum34/>`_, whose items
have an index.
Expand All @@ -21,7 +17,7 @@ def __init__(self, name: str) -> None:
# When the enum item is initialized, self._member_names_ contains the
# names of the previously initialized items, so its length is the index
# of this item.
self.index = len(self._member_names_)
self.index = len(self._member_names_) # type: ignore[attr-defined]

# Bypass the slow Enum.__eq__
__eq__ = object.__eq__
Expand All @@ -33,13 +29,14 @@ def __init__(self, name: str) -> None:
@classmethod
def encode(
cls,
array: Union[
EnumArray,
numpy.int_,
numpy.float_,
numpy.object_,
],
) -> EnumArray:
array: (
t.EnumArray
| t.Array[t.ArrayBytes]
| t.Array[t.ArrayEnum]
| t.Array[t.ArrayInt]
| t.Array[t.ArrayStr]
),
) -> t.EnumArray:
"""
Encode a string numpy array, an enum item numpy array, or an int numpy
array into an :any:`EnumArray`. See :any:`EnumArray.decode` for
Expand All @@ -65,6 +62,7 @@ def encode(
>>> encoded_array[0]
2 # Encoded value
"""

if isinstance(array, EnumArray):
return array

Expand All @@ -73,7 +71,7 @@ def encode(
array = numpy.select(
[array == item.name for item in cls],
[item.index for item in cls],
).astype(ENUM_ARRAY_DTYPE)
).astype(t.ArrayEnum)

# Enum items arrays
elif isinstance(array, numpy.ndarray) and array.dtype.kind == "O":
Expand All @@ -94,6 +92,7 @@ def encode(
array = numpy.select(
[array == item for item in cls],
[item.index for item in cls],
).astype(ENUM_ARRAY_DTYPE)
).astype(t.ArrayEnum)

array = numpy.asarray(array, dtype=t.ArrayEnum)
return EnumArray(array, cls)
Loading
Loading