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: add types to holders (1/3) #1285

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions .conda/openfisca-extension-template/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ schema_version: 1

context:
name: openfisca-extension-template
version: 1.3.15
version: 2.0.6

package:
name: ${{ name|lower }}
version: ${{ version }}

source:
url: https://pypi.org/packages/source/${{ name[0] }}/${{ name }}/openfisca_extension_template-${{ version }}.tar.gz
sha256: e16ee9cbefdd5e9ddc1c2c0e12bcd74307c8cb1be55353b3b2788d64a90a5df9
sha256: e6ee405e9710e5e07f498173bad1a41531527b8e423fca7d045332cbdb10082f

build:
number: 2
Expand All @@ -21,8 +21,8 @@ requirements:
host:
- numpy
- pip
- poetry >=1.7
- python
- setuptools >=61.0
run:
- numpy
- python
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

### 43.2.3 [#1285](https://github.com/openfisca/openfisca-core/pull/1285)

#### Documentation

- Add some types to `holders` (1 of 3)

### 43.2.2 [#1280](https://github.com/openfisca/openfisca-core/pull/1280)

#### Documentation
Expand Down
2 changes: 0 additions & 2 deletions openfisca_core/commons/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Common tools for contributors and users."""

from . import types
from .dummy import Dummy
from .formulas import apply_thresholds, concat, switch
from .misc import empty_clone, eval_expression, stringify_array
Expand All @@ -16,5 +15,4 @@
"marginal_rate",
"stringify_array",
"switch",
"types",
]
16 changes: 8 additions & 8 deletions openfisca_core/commons/formulas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import numpy

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


def apply_thresholds(
input: t.Array[numpy.float32],
input: t.FloatArray,
thresholds: t.ArrayLike[float],
choices: t.ArrayLike[float],
) -> t.Array[numpy.float32]:
) -> t.FloatArray:
"""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 @@ -52,9 +52,9 @@ def apply_thresholds(


def concat(
this: t.Array[numpy.str_] | t.ArrayLike[object],
that: t.Array[numpy.str_] | t.ArrayLike[object],
) -> t.Array[numpy.str_]:
this: t.StrArray | t.ArrayLike[object],
that: t.StrArray | t.ArrayLike[object],
) -> t.StrArray:
"""Concatenate the values of two arrays.

Args:
Expand Down Expand Up @@ -87,9 +87,9 @@ def concat(


def switch(
conditions: t.Array[numpy.float32] | t.ArrayLike[float],
conditions: t.FloatArray | t.ArrayLike[float],
value_by_condition: Mapping[float, float],
) -> t.Array[numpy.float32]:
) -> t.FloatArray:
"""Mimic a switch statement.

Given an array of conditions, returns an array of the same size,
Expand Down
5 changes: 2 additions & 3 deletions openfisca_core/commons/misc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import numexpr
import numpy

from openfisca_core import types as t

Expand Down Expand Up @@ -43,7 +42,7 @@ def __init__(_: object) -> None: ...
return new


def stringify_array(array: None | t.Array[numpy.generic]) -> str:
def stringify_array(array: None | t.VarArray) -> str:
"""Generate a clean string representation of a numpy array.

Args:
Expand Down Expand Up @@ -79,7 +78,7 @@ def stringify_array(array: None | t.Array[numpy.generic]) -> str:

def eval_expression(
expression: str,
) -> str | t.Array[numpy.bool_] | t.Array[numpy.int32] | t.Array[numpy.float32]:
) -> str | t.BoolArray | t.IntArray | t.FloatArray:
"""Evaluate a string expression to 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 @@ -2,14 +2,14 @@

import numpy

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


def average_rate(
target: t.Array[numpy.float32],
varying: t.Array[numpy.float32] | t.ArrayLike[float],
target: t.FloatArray,
varying: t.FloatArray | t.ArrayLike[float],
trim: None | t.ArrayLike[float] = None,
) -> t.Array[numpy.float32]:
) -> t.FloatArray:
"""Compute the average rate of a target net income.

Given a ``target`` net income, and according to the ``varying`` gross
Expand Down Expand Up @@ -59,10 +59,10 @@ def average_rate(


def marginal_rate(
target: t.Array[numpy.float32],
varying: t.Array[numpy.float32] | t.ArrayLike[float],
target: t.FloatArray,
varying: t.FloatArray | t.ArrayLike[float],
trim: None | t.ArrayLike[float] = None,
) -> t.Array[numpy.float32]:
) -> t.FloatArray:
"""Compute the marginal rate of a target net income.

Given a ``target`` net income, and according to the ``varying`` gross
Expand Down
3 changes: 0 additions & 3 deletions openfisca_core/commons/types.py

This file was deleted.

3 changes: 1 addition & 2 deletions openfisca_core/data_storage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Different storage backends for the data of a simulation."""

from . import types
from .in_memory_storage import InMemoryStorage
from .on_disk_storage import OnDiskStorage

__all__ = ["InMemoryStorage", "OnDiskStorage", "types"]
__all__ = ["InMemoryStorage", "OnDiskStorage"]
21 changes: 11 additions & 10 deletions openfisca_core/data_storage/in_memory_storage.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from __future__ import annotations

from collections.abc import KeysView, MutableMapping
from typing import Generic, TypeVar

import numpy

from openfisca_core import periods
from openfisca_core.periods import DateUnit
from openfisca_core import periods, types as t

from . import types as t
#: Type var for numpy arrays (invariant).
_N = TypeVar("_N", bound=t.VarDType)


class InMemoryStorage:
class InMemoryStorage(Generic[_N]):
"""Storing and retrieving calculated vectors in memory.

Args:
Expand All @@ -22,13 +23,13 @@ class InMemoryStorage:
is_eternal: bool

#: A dictionary containing data that has been stored in memory.
_arrays: MutableMapping[t.Period, t.Array[t.DTypeGeneric]]
_arrays: MutableMapping[t.Period, t.Array[_N]]

def __init__(self, is_eternal: bool = False) -> None:
self._arrays = {}
self.is_eternal = is_eternal

def get(self, period: None | t.Period = None) -> None | t.Array[t.DTypeGeneric]:
def get(self, period: None | t.Period = None) -> None | t.Array[_N]:
"""Retrieve the data for the specified :obj:`.Period` from memory.

Args:
Expand Down Expand Up @@ -56,15 +57,15 @@ def get(self, period: None | t.Period = None) -> None | t.Array[t.DTypeGeneric]:

"""
if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.Period.eternity()
period = periods.period(period)

values = self._arrays.get(period)
if values is None:
return None
return values

def put(self, value: t.Array[t.DTypeGeneric], period: None | t.Period) -> None:
def put(self, value: t.Array[_N], period: None | t.Period) -> None:
"""Store the specified data in memory for the specified :obj:`.Period`.

Args:
Expand All @@ -88,7 +89,7 @@ def put(self, value: t.Array[t.DTypeGeneric], period: None | t.Period) -> None:

"""
if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.Period.eternity()
period = periods.period(period)

self._arrays[period] = value
Expand Down Expand Up @@ -133,7 +134,7 @@ def delete(self, period: None | t.Period = None) -> None:
return

if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.Period.eternity()
period = periods.period(period)

self._arrays = {
Expand Down
Loading
Loading