Types

Data types and protocols used by OpenFisca Core.

The type definitions included in this sub-package are intented for contributors, to help them better understand and document contracts and expected behaviours.

Official Public API:

Note

How imports are being used today:

from openfisca_core.types import *  # Bad
from openfisca_core.types.data_types.arrays import ArrayLike  # Bad

The previous examples provoke cyclic dependency problems, that prevents us from modularizing the different components of the library, so as to make them easier to test and to maintain.

How could them be used after the next major release:

from openfisca_core.types import ArrayLike

ArrayLike # Good: import types as publicly exposed
openfisca_core.types.ArrayType

alias of NDArray

openfisca_core.types.data_types.arrays.ArrayLike = typing.Union[nptyping.types._ndarray.NDArray, typing.Sequence[~T]]

typing.Generic – Type of any castable to numpy.ndarray.

These include any numpy.ndarray and sequences (like list, tuple, and so on).

Examples

>>> ArrayLike[float]
typing.Union[numpy.ndarray, typing.Sequence[float]]
>>> ArrayLike[str]
typing.Union[numpy.ndarray, typing.Sequence[str]]

Note

It is possible since numpy version 1.21 to specify the type of an array, thanks to numpy.typing.NDArray:

from numpy.typing import NDArray
NDArray[numpy.float64]

mypy provides duck type compatibility, so an int is considered to be valid whenever a float is expected.

New in version 35.5.0.

Changed in version 35.6.0: Moved to types