-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0e5fdb
commit d2625f4
Showing
10 changed files
with
176 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._run_formula import RunFormula | ||
|
||
__all__ = ["RunFormula"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
from numpy.typing import NDArray | ||
from typing import Any, cast | ||
|
||
import dataclasses | ||
import inspect | ||
|
||
from ..typing import Formula2, Formula3, Instant, Params, Population | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class RunFormula: | ||
"""Run a Variable's given Formula.""" | ||
|
||
#: The formula we want to run. | ||
formula: Formula3 | Formula2 | None = None | ||
|
||
def __call__( | ||
self, population: Population, instant: Instant, params: Params | ||
) -> NDArray[Any] | None: | ||
if self.formula is None: | ||
return None | ||
|
||
if self.__arity() == 3: | ||
return cast(Formula3, self.formula)(population, instant, params) | ||
|
||
else: | ||
return cast(Formula2, self.formula)(population, instant) | ||
|
||
def __arity(self) -> int: | ||
return len(inspect.getfullargspec(self.formula).args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from __future__ import annotations | ||
|
||
from numpy.typing import NDArray | ||
from openfisca_core.simulations.typing import ( | ||
Formula2, | ||
Formula3, | ||
Instant, | ||
ParameterNodeAtInstant, | ||
Params, | ||
Population, | ||
) | ||
from typing import Any | ||
|
||
import numpy | ||
import pytest | ||
|
||
from openfisca_core import simulations | ||
|
||
|
||
class TestPopulation: | ||
... | ||
|
||
|
||
class TestInstant: | ||
... | ||
|
||
|
||
class TestParams: | ||
def __call__(self, instant: Instant) -> ParameterNodeAtInstant: | ||
... | ||
|
||
|
||
@pytest.fixture | ||
def population() -> Population: | ||
return TestPopulation() | ||
|
||
|
||
@pytest.fixture | ||
def instant() -> Instant: | ||
return TestInstant() | ||
|
||
|
||
@pytest.fixture | ||
def params() -> Params: | ||
return TestParams() | ||
|
||
|
||
def test_run_formula_without_formula( | ||
population: Population, instant: Instant, params: Params | ||
) -> None: | ||
"""Test that RunFormula runs without a formula.""" | ||
|
||
run_formula = simulations.RunFormula(None) | ||
|
||
assert not run_formula(population, instant, params) | ||
|
||
|
||
def test_run_formula_with_two_arguments( | ||
population: Population, instant: Instant, params: Params | ||
) -> None: | ||
"""Test that RunFormula runs a formula with two arguments.""" | ||
|
||
def formula(a: Population, b: Instant) -> NDArray[Any]: | ||
return numpy.array([1, 2, 3]) | ||
|
||
run_formula = simulations.RunFormula(formula) | ||
|
||
assert run_formula(population, instant, params) | ||
|
||
|
||
def test_run_formula_with_three_arguments( | ||
population: Population, instant: Instant, params: Params | ||
) -> None: | ||
"""Test that RunFormula runs a formula with three arguments.""" | ||
|
||
def formula(a: Population, b: Instant, c: Params) -> NDArray[Any]: | ||
return numpy.array([1, 2, 3]) | ||
|
||
run_formula = simulations.RunFormula(formula) | ||
|
||
assert run_formula(population, instant, params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from __future__ import annotations | ||
|
||
import abc | ||
from numpy.typing import NDArray | ||
from typing import Any, Protocol | ||
|
||
|
||
class Formula3(Protocol): | ||
@abc.abstractmethod | ||
def __call__( | ||
self, __population: Population, __instant: Instant, __params: Params | ||
) -> NDArray[Any]: | ||
... | ||
|
||
|
||
class Formula2(Protocol): | ||
@abc.abstractmethod | ||
def __call__(self, __population: Population, __instant: Instant) -> NDArray[Any]: | ||
... | ||
|
||
|
||
class Instant(Protocol): | ||
... | ||
|
||
|
||
class ParameterNodeAtInstant(Protocol): | ||
... | ||
|
||
|
||
class Params(Protocol): | ||
@abc.abstractmethod | ||
def __call__(self, __instant: Instant) -> ParameterNodeAtInstant: | ||
... | ||
|
||
|
||
class Population(Protocol): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters