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: improve testing in simulation builder #1222

Draft
wants to merge 11 commits into
base: fix-mypy-chekcs-proj
Choose a base branch
from
14 changes: 6 additions & 8 deletions openfisca_core/simulations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@
#
# See: https://www.python.org/dev/peps/pep-0008/#imports

from openfisca_core.errors import ( # noqa: F401
CycleError,
NaNCreationError,
SpiralError,
)
from openfisca_core.errors import CycleError, NaNCreationError, SpiralError

from .helpers import ( # noqa: F401
from . import types
from .helpers import (
calculate_output_add,
calculate_output_divide,
check_type,
transform_to_strict_syntax,
)
from .simulation import Simulation # noqa: F401
from .simulation_builder import SimulationBuilder # noqa: F401
from .simulation import Simulation
from .simulation_builder import SimulationBuilder

__all__ = [
"CycleError",
Expand All @@ -46,4 +43,5 @@
"calculate_output_divide",
"check_type",
"transform_to_strict_syntax",
"types",
]
16 changes: 10 additions & 6 deletions openfisca_core/simulations/_build_default_simulation.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
"""This module contains the _BuildDefaultSimulation class."""

from typing import Union
from typing_extensions import Self

import numpy

from .simulation import Simulation
from .typing import Entity, Population, TaxBenefitSystem
from .types import Populations, TaxBenefitSystem


class _BuildDefaultSimulation:
"""Build a default simulation.

Attributes:
count(int): The number of periods.
populations(Populations): The built populations.
simulation(Simulation): The built simulation.

Args:
tax_benefit_system(TaxBenefitSystem): The tax-benefit system.
count(int): The number of periods.
Expand Down Expand Up @@ -47,7 +51,7 @@ class _BuildDefaultSimulation:
count: int

#: The built populations.
populations: dict[str, Union[Population[Entity]]]
populations: Populations

#: The built simulation.
simulation: Simulation
Expand All @@ -61,7 +65,7 @@ def add_count(self) -> Self:
"""Add the number of Population to the simulation.

Returns:
_BuildDefaultSimulation: The builder.
Self: The builder.

Examples:
>>> from openfisca_core import entities, taxbenefitsystems
Expand Down Expand Up @@ -94,7 +98,7 @@ def add_ids(self) -> Self:
"""Add the populations ids to the simulation.

Returns:
_BuildDefaultSimulation: The builder.
Self: The builder.

Examples:
>>> from openfisca_core import entities, taxbenefitsystems
Expand Down Expand Up @@ -129,7 +133,7 @@ def add_members_entity_id(self) -> Self:
Each SingleEntity has its own GroupEntity.

Returns:
_BuildDefaultSimulation: The builder.
Self: The builder.

Examples:
>>> from openfisca_core import entities, taxbenefitsystems
Expand Down
24 changes: 14 additions & 10 deletions openfisca_core/simulations/_build_from_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from __future__ import annotations

from collections.abc import Sized
from typing_extensions import Self

from openfisca_core import errors

from ._build_default_simulation import _BuildDefaultSimulation
from ._type_guards import is_variable_dated
from ._guards import is_a_dated_value, is_a_pure_value
from .simulation import Simulation
from .typing import Entity, Population, TaxBenefitSystem, Variables
from .types import Populations, TaxBenefitSystem, Variables


class _BuildFromVariables:
Expand Down Expand Up @@ -67,7 +68,7 @@ class _BuildFromVariables:
default_period: str | None

#: The built populations.
populations: dict[str, Population[Entity]]
populations: Populations

#: The built simulation.
simulation: Simulation
Expand Down Expand Up @@ -99,7 +100,7 @@ def add_dated_values(self) -> Self:
"""Add the dated input values to the Simulation.

Returns:
_BuildFromVariables: The builder.
Self: The builder.

Examples:
>>> from openfisca_core import entities, periods, taxbenefitsystems, variables
Expand Down Expand Up @@ -141,7 +142,7 @@ def add_dated_values(self) -> Self:
"""

for variable, value in self.variables.items():
if is_variable_dated(dated_variable := value):
if is_a_dated_value(dated_variable := value):
for period, dated_value in dated_variable.items():
self.simulation.set_input(variable, period, dated_value)

Expand All @@ -151,7 +152,7 @@ def add_undated_values(self) -> Self:
"""Add the undated input values to the Simulation.

Returns:
_BuildFromVariables: The builder.
Self: The builder.

Raises:
SituationParsingError: If there is not a default period set.
Expand Down Expand Up @@ -184,7 +185,7 @@ def add_undated_values(self) -> Self:
>>> builder = _BuildFromVariables(tax_benefit_system, variables)
>>> builder.add_undated_values()
Traceback (most recent call last):
openfisca_core.errors.situation_parsing_error.SituationParsingError
openfisca_core.errors.situation_parsing_error.SituationParsingEr...
>>> builder.default_period = period
>>> builder.add_undated_values()
<..._BuildFromVariables object at ...>
Expand All @@ -199,7 +200,7 @@ def add_undated_values(self) -> Self:
"""

for variable, value in self.variables.items():
if not is_variable_dated(undated_value := value):
if is_a_pure_value(undated_value := value):
if (period := self.default_period) is None:
message = (
"Can't deal with type: expected object. Input "
Expand All @@ -218,15 +219,18 @@ def add_undated_values(self) -> Self:

def _person_count(params: Variables) -> int:
try:
first_value = next(iter(params.values()))
first_value: object = next(iter(params.values()))

if isinstance(first_value, dict):
first_value = next(iter(first_value.values()))

if isinstance(first_value, str):
return 1

return len(first_value)
if isinstance(first_value, Sized):
return len(first_value)

raise NotImplementedError

except Exception:
return 1
Loading
Loading