diff --git a/openfisca_core/periods/tests/test__parsers.py b/openfisca_core/periods/tests/test__parsers.py index a78a11942..6c88c9cd1 100644 --- a/openfisca_core/periods/tests/test__parsers.py +++ b/openfisca_core/periods/tests/test__parsers.py @@ -11,18 +11,9 @@ ["1001-01", Period((DateUnit.MONTH, Instant((1001, 1, 1)), 1))], ["1001-12", Period((DateUnit.MONTH, Instant((1001, 12, 1)), 1))], ["1001-01-01", Period((DateUnit.DAY, Instant((1001, 1, 1)), 1))], - [ - "1001-W01", - Period((DateUnit.WEEK, Instant((1000, 12, 29)), 1)), - ], - [ - "1001-W52", - Period((DateUnit.WEEK, Instant((1001, 12, 21)), 1)), - ], - [ - "1001-W01-1", - Period((DateUnit.WEEKDAY, Instant((1000, 12, 29)), 1)), - ], + ["1001-W01", Period((DateUnit.WEEK, Instant((1000, 12, 29)), 1))], + ["1001-W52", Period((DateUnit.WEEK, Instant((1001, 12, 21)), 1))], + ["1001-W01-1", Period((DateUnit.WEEKDAY, Instant((1000, 12, 29)), 1))], ], ) def test__parse_period(arg, expected): diff --git a/openfisca_core/periods/tests/test_instant.py b/openfisca_core/periods/tests/test_instant.py index 6c04f78f2..21549008f 100644 --- a/openfisca_core/periods/tests/test_instant.py +++ b/openfisca_core/periods/tests/test_instant.py @@ -6,101 +6,26 @@ @pytest.mark.parametrize( "instant, offset, unit, expected", [ - [ - Instant((2020, 2, 29)), - "first-of", - DateUnit.YEAR, - Instant((2020, 1, 1)), - ], - [ - Instant((2020, 2, 29)), - "first-of", - DateUnit.MONTH, - Instant((2020, 2, 1)), - ], - [ - Instant((2020, 2, 29)), - "first-of", - DateUnit.WEEK, - Instant((2020, 2, 24)), - ], + [Instant((2020, 2, 29)), "first-of", DateUnit.YEAR, Instant((2020, 1, 1))], + [Instant((2020, 2, 29)), "first-of", DateUnit.MONTH, Instant((2020, 2, 1))], + [Instant((2020, 2, 29)), "first-of", DateUnit.WEEK, Instant((2020, 2, 24))], [Instant((2020, 2, 29)), "first-of", DateUnit.DAY, None], [Instant((2020, 2, 29)), "first-of", DateUnit.WEEKDAY, None], - [ - Instant((2020, 2, 29)), - "last-of", - DateUnit.YEAR, - Instant((2020, 12, 31)), - ], - [ - Instant((2020, 2, 29)), - "last-of", - DateUnit.MONTH, - Instant((2020, 2, 29)), - ], - [ - Instant((2020, 2, 29)), - "last-of", - DateUnit.WEEK, - Instant((2020, 3, 1)), - ], + [Instant((2020, 2, 29)), "last-of", DateUnit.YEAR, Instant((2020, 12, 31))], + [Instant((2020, 2, 29)), "last-of", DateUnit.MONTH, Instant((2020, 2, 29))], + [Instant((2020, 2, 29)), "last-of", DateUnit.WEEK, Instant((2020, 3, 1))], [Instant((2020, 2, 29)), "last-of", DateUnit.DAY, None], [Instant((2020, 2, 29)), "last-of", DateUnit.WEEKDAY, None], - [ - Instant((2020, 2, 29)), - -3, - DateUnit.YEAR, - Instant((2017, 2, 28)), - ], - [ - Instant((2020, 2, 29)), - -3, - DateUnit.MONTH, - Instant((2019, 11, 29)), - ], - [ - Instant((2020, 2, 29)), - -3, - DateUnit.WEEK, - Instant((2020, 2, 8)), - ], - [ - Instant((2020, 2, 29)), - -3, - DateUnit.DAY, - Instant((2020, 2, 26)), - ], - [ - Instant((2020, 2, 29)), - -3, - DateUnit.WEEKDAY, - Instant((2020, 2, 26)), - ], - [ - Instant((2020, 2, 29)), - 3, - DateUnit.YEAR, - Instant((2023, 2, 28)), - ], - [ - Instant((2020, 2, 29)), - 3, - DateUnit.MONTH, - Instant((2020, 5, 29)), - ], - [ - Instant((2020, 2, 29)), - 3, - DateUnit.WEEK, - Instant((2020, 3, 21)), - ], + [Instant((2020, 2, 29)), -3, DateUnit.YEAR, Instant((2017, 2, 28))], + [Instant((2020, 2, 29)), -3, DateUnit.MONTH, Instant((2019, 11, 29))], + [Instant((2020, 2, 29)), -3, DateUnit.WEEK, Instant((2020, 2, 8))], + [Instant((2020, 2, 29)), -3, DateUnit.DAY, Instant((2020, 2, 26))], + [Instant((2020, 2, 29)), -3, DateUnit.WEEKDAY, Instant((2020, 2, 26))], + [Instant((2020, 2, 29)), 3, DateUnit.YEAR, Instant((2023, 2, 28))], + [Instant((2020, 2, 29)), 3, DateUnit.MONTH, Instant((2020, 5, 29))], + [Instant((2020, 2, 29)), 3, DateUnit.WEEK, Instant((2020, 3, 21))], [Instant((2020, 2, 29)), 3, DateUnit.DAY, Instant((2020, 3, 3))], - [ - Instant((2020, 2, 29)), - 3, - DateUnit.WEEKDAY, - Instant((2020, 3, 3)), - ], + [Instant((2020, 2, 29)), 3, DateUnit.WEEKDAY, Instant((2020, 3, 3))], ], ) def test_offset(instant, offset, unit, expected): diff --git a/openfisca_core/scripts/measure_performances.py b/openfisca_core/scripts/measure_performances.py index 65dd6afde..89fd47b44 100644 --- a/openfisca_core/scripts/measure_performances.py +++ b/openfisca_core/scripts/measure_performances.py @@ -14,6 +14,7 @@ from openfisca_core import periods, simulations from openfisca_core.entities import build_entity +from openfisca_core.periods import DateUnit from openfisca_core.taxbenefitsystems import TaxBenefitSystem from openfisca_core.tools import assert_near from openfisca_core.variables import Variable @@ -80,7 +81,7 @@ class city_code(Variable): value_type = "FixedStr" max_length = 5 entity = Famille - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY label = """Code INSEE "city_code" de la commune de résidence de la famille""" @@ -114,7 +115,7 @@ class dom_tom(Variable): label = "La famille habite-t-elle les DOM-TOM ?" def formula(self, simulation, period): - period = period.start.period(periods.YEAR).offset("first-of") + period = period.start.period(DateUnit.YEAR).offset("first-of") city_code = simulation.calculate("city_code", period) return np.logical_or(startswith(city_code, "97"), startswith(city_code, "98")) @@ -125,7 +126,7 @@ class revenu_disponible(Variable): label = "Revenu disponible de l'individu" def formula(self, simulation, period): - period = period.start.period(periods.YEAR).offset("first-of") + period = period.start.period(DateUnit.YEAR).offset("first-of") rsa = simulation.calculate("rsa", period) salaire_imposable = simulation.calculate("salaire_imposable", period) return rsa + salaire_imposable * 0.7 @@ -137,17 +138,17 @@ class rsa(Variable): label = "RSA" def formula_2010_01_01(self, simulation, period): - period = period.start.period(periods.MONTH).offset("first-of") + period = period.start.period(DateUnit.MONTH).offset("first-of") salaire_imposable = simulation.calculate("salaire_imposable", period) return (salaire_imposable < 500) * 100.0 def formula_2011_01_01(self, simulation, period): - period = period.start.period(periods.MONTH).offset("first-of") + period = period.start.period(DateUnit.MONTH).offset("first-of") salaire_imposable = simulation.calculate("salaire_imposable", period) return (salaire_imposable < 500) * 200.0 def formula_2013_01_01(self, simulation, period): - period = period.start.period(periods.MONTH).offset("first-of") + period = period.start.period(DateUnit.MONTH).offset("first-of") salaire_imposable = simulation.calculate("salaire_imposable", period) return (salaire_imposable < 500) * 300 @@ -158,7 +159,7 @@ class salaire_imposable(Variable): label = "Salaire imposable" def formula(individu, period): - period = period.start.period(periods.YEAR).offset("first-of") + period = period.start.period(DateUnit.YEAR).offset("first-of") dom_tom = individu.famille("dom_tom", period) salaire_net = individu("salaire_net", period) return salaire_net * 0.9 - 100 * dom_tom @@ -170,7 +171,7 @@ class salaire_net(Variable): label = "Salaire net" def formula(self, simulation, period): - period = period.start.period(periods.YEAR).offset("first-of") + period = period.start.period(DateUnit.YEAR).offset("first-of") salaire_brut = simulation.calculate("salaire_brut", period) return salaire_brut * 0.8 diff --git a/openfisca_core/simulations/tests/test_invariants.py b/openfisca_core/simulations/tests/test_invariants.py deleted file mode 100644 index ff9564f6e..000000000 --- a/openfisca_core/simulations/tests/test_invariants.py +++ /dev/null @@ -1,248 +0,0 @@ -from typing import TypedDict - -from openfisca_core.simulations.types import Variable - -import enum - -import pytest - -from openfisca_core import periods - - -class EitherTag(enum.Enum): - FAILURE = enum.auto() - SUCCESS = enum.auto() - - -class Either: - def __init__(self, value, tag=EitherTag.SUCCESS): - self._value = value - self._tag = tag - - @property - def isSuccess(self): - return self._tag == EitherTag.SUCCESS - - @property - def isFailure(self): - return self._tag == EitherTag.FAILURE - - def map(self, f): - if self.isSuccess: - return Either.succeed(f(**self.unwrap())) - - return self - - def join(self): - if self.isSuccess and isinstance(self.unwrap(), Either): - return self.unwrap() - - return self - - def then(self, f): - return self.map(f).join() - - def unwrap(self): - return self._value - - @staticmethod - def fail(value): - return Either(value, EitherTag.FAILURE) - - @staticmethod - def succeed(value): - return Either(value, EitherTag.SUCCESS) - - -class TestVariable(Variable): - def __init__(self, name, definition_period): - self.name = name - self.definition_period = definition_period - - -class ValidationParams(TypedDict): - variable: Variable - period: periods.Period - - -def are_periods_compatible_1(**params): - variable = params["variable"] - period = params["period"] - - if ( - variable.definition_period in (periods.MONTH, periods.DAY) - and period.unit == periods.WEEK - ): - return Either.fail( - f"Unable to compute variable '{variable.name}' for period " - f"{period}, as {period} and {variable.definition_period} are " - "incompatible periods. You can, however, change the requested " - "period to 'period.this_year'." - ) - - return Either.succeed(params) - - -def are_periods_compatible_2(**params): - variable = params["variable"] - period = params["period"] - - if ( - variable.definition_period in (periods.WEEK, periods.WEEKDAY) - and period.unit == periods.MONTH - ): - return Either.fail( - f"Unable to compute variable '{variable.name}' for period " - f"{period}, as {period} and {variable.definition_period} are " - "incompatible periods. You can, however, change the requested " - "period to 'period.this_year' or 'period.first_week'." - ) - - return Either.succeed(params) - - -def derive_calculate_add(**params): - either = ( - Either(params).then(are_periods_compatible_1).then(are_periods_compatible_2) - ) - - if either.isSuccess: - return either.unwrap() - - raise ValueError(either.unwrap()) - - -@pytest.mark.parametrize( - "period_unit, period_str, expected", - [ - (periods.YEAR, "2020", True), - (periods.YEAR, "2020-01", True), - (periods.YEAR, "2020-01-01", True), - (periods.YEAR, "2020-W01", True), - (periods.YEAR, "2020-W01-1", True), - (periods.MONTH, "2020", True), - (periods.MONTH, "2020-01", True), - (periods.MONTH, "2020-01-01", True), - (periods.MONTH, "2020-W01", False), - (periods.MONTH, "2020-W01-1", True), - (periods.DAY, "2020", True), - (periods.DAY, "2020-01", True), - (periods.DAY, "2020-01-01", True), - (periods.DAY, "2020-W01", False), - (periods.DAY, "2020-W01-1", True), - (periods.WEEK, "2020", True), - (periods.WEEK, "2020-01", True), - (periods.WEEK, "2020-01-01", True), - (periods.WEEK, "2020-W01", True), - (periods.WEEK, "2020-W01-1", True), - (periods.WEEKDAY, "2020", True), - (periods.WEEKDAY, "2020-01", True), - (periods.WEEKDAY, "2020-01-01", True), - (periods.WEEKDAY, "2020-W01", True), - (periods.WEEKDAY, "2020-W01-1", True), - ], -) -def test_are_periods_compatible_1(period_unit, period_str, expected): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - either = are_periods_compatible_1(variable=variable, period=period) - assert either.isSuccess is expected - - -@pytest.mark.parametrize( - "period_unit, period_str, expected", - [ - (periods.YEAR, "2020", True), - (periods.YEAR, "2020-01", True), - (periods.YEAR, "2020-01-01", True), - (periods.YEAR, "2020-W01", True), - (periods.YEAR, "2020-W01-1", True), - (periods.MONTH, "2020", True), - (periods.MONTH, "2020-01", True), - (periods.MONTH, "2020-01-01", True), - (periods.MONTH, "2020-W01", True), - (periods.MONTH, "2020-W01-1", True), - (periods.DAY, "2020", True), - (periods.DAY, "2020-01", True), - (periods.DAY, "2020-01-01", True), - (periods.DAY, "2020-W01", True), - (periods.DAY, "2020-W01-1", True), - (periods.WEEK, "2020", True), - (periods.WEEK, "2020-01", False), - (periods.WEEK, "2020-01-01", True), - (periods.WEEK, "2020-W01", True), - (periods.WEEK, "2020-W01-1", True), - (periods.WEEKDAY, "2020", True), - (periods.WEEKDAY, "2020-01", False), - (periods.WEEKDAY, "2020-01-01", True), - (periods.WEEKDAY, "2020-W01", True), - (periods.WEEKDAY, "2020-W01-1", True), - ], -) -def test_are_periods_compatible_2(period_unit, period_str, expected): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - either = are_periods_compatible_2(variable=variable, period=period) - assert either.isSuccess is expected - - -@pytest.mark.parametrize( - "period_unit, period_str", - [ - (periods.YEAR, "2020"), - (periods.YEAR, "2020-01"), - (periods.YEAR, "2020-01-01"), - (periods.YEAR, "2020-W01"), - (periods.YEAR, "2020-W01-1"), - (periods.MONTH, "2020"), - (periods.MONTH, "2020-01"), - (periods.MONTH, "2020-01-01"), - (periods.MONTH, "2020-W01-1"), - (periods.DAY, "2020"), - (periods.DAY, "2020-01"), - (periods.DAY, "2020-01-01"), - (periods.DAY, "2020-W01-1"), - (periods.WEEK, "2020"), - (periods.WEEK, "2020-01-01"), - (periods.WEEK, "2020-W01"), - (periods.WEEK, "2020-W01-1"), - (periods.WEEKDAY, "2020"), - (periods.WEEKDAY, "2020-01-01"), - (periods.WEEKDAY, "2020-W01"), - (periods.WEEKDAY, "2020-W01-1"), - ], -) -def test_derive_calculate_add(period_unit, period_str): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - assert derive_calculate_add(variable=variable, period=period) - - -@pytest.mark.parametrize( - "period_unit, period_str", - [ - (periods.MONTH, "2020-W01"), - (periods.DAY, "2020-W01"), - ], -) -def test_derive_calculate_add_with_invalid_period_1(period_unit, period_str): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - with pytest.raises(ValueError) as error: - derive_calculate_add(variable=variable, period=period) - assert "period.first_week" not in str(error.value) - - -@pytest.mark.parametrize( - "period_unit, period_str", - [ - (periods.WEEK, "2020-01"), - (periods.WEEKDAY, "2020-01"), - ], -) -def test_derive_calculate_add_with_invalid_period_2(period_unit, period_str): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - with pytest.raises(ValueError) as error: - derive_calculate_add(variable=variable, period=period) - assert "period.first_week" in str(error.value) diff --git a/openfisca_core/tools/simulation_dumper.py b/openfisca_core/tools/simulation_dumper.py index 497c7ca03..ab21bd79f 100644 --- a/openfisca_core/tools/simulation_dumper.py +++ b/openfisca_core/tools/simulation_dumper.py @@ -5,8 +5,8 @@ import numpy as np -from openfisca_core import periods from openfisca_core.data_storage import OnDiskStorage +from openfisca_core.periods import DateUnit from openfisca_core.simulations import Simulation @@ -122,7 +122,7 @@ def _restore_holder(simulation, variable, directory): storage_dir = os.path.join(directory, variable) is_variable_eternal = ( simulation.tax_benefit_system.get_variable(variable).definition_period - == periods.ETERNITY + == DateUnit.ETERNITY ) disk_storage = OnDiskStorage( storage_dir, is_eternal=is_variable_eternal, preserve_storage_dir=True diff --git a/openfisca_core/variables/variable.py b/openfisca_core/variables/variable.py index eeddc97ba..e70c0d05d 100644 --- a/openfisca_core/variables/variable.py +++ b/openfisca_core/variables/variable.py @@ -14,6 +14,7 @@ from openfisca_core import periods, tools from openfisca_core.entities import Entity, GroupEntity from openfisca_core.indexed_enums import Enum, EnumArray +from openfisca_core.periods import DateUnit, Period from . import config, helpers @@ -135,7 +136,7 @@ def __init__(self, baseline_variable=None): ) self.entity = self.set(attr, "entity", required=True, setter=self.set_entity) self.definition_period = self.set( - attr, "definition_period", required=True, allowed_values=periods.DateUnit + attr, "definition_period", required=True, allowed_values=DateUnit ) self.label = self.set(attr, "label", allowed_type=str, setter=self.set_label) self.end = self.set(attr, "end", allowed_type=str, setter=self.set_end) @@ -373,7 +374,7 @@ def get_introspection_data(cls): def get_formula( self, - period: Union[Instant, periods.Period, str, int] = None, + period: Union[Instant, Period, str, int] = None, ) -> Optional[Formula]: """Returns the formula to compute the variable at the given period. @@ -398,7 +399,7 @@ def get_formula( 1 ] # peekitem gets the 1st key-value tuple (the oldest start_date and formula). Return the formula. - if isinstance(period, periods.Period): + if isinstance(period, Period): instant = period.start else: try: diff --git a/tests/core/test_calculate_output.py b/tests/core/test_calculate_output.py index 926db5253..ecf59b5f7 100644 --- a/tests/core/test_calculate_output.py +++ b/tests/core/test_calculate_output.py @@ -2,27 +2,28 @@ from openfisca_country_template import entities, situation_examples -from openfisca_core import periods, simulations, tools +from openfisca_core import simulations, tools +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder from openfisca_core.variables import Variable class simple_variable(Variable): entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH value_type = int class variable_with_calculate_output_add(Variable): entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH value_type = int calculate_output = simulations.calculate_output_add class variable_with_calculate_output_divide(Variable): entity = entities.Person - definition_period = periods.YEAR + definition_period = DateUnit.YEAR value_type = int calculate_output = simulations.calculate_output_divide diff --git a/tests/core/test_countries.py b/tests/core/test_countries.py index bc0368246..8263ac3c4 100644 --- a/tests/core/test_countries.py +++ b/tests/core/test_countries.py @@ -2,6 +2,7 @@ from openfisca_core import periods, populations, tools from openfisca_core.errors import VariableNameConflictError, VariableNotFoundError +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder from openfisca_core.variables import Variable @@ -100,7 +101,7 @@ def test_variable_with_reference(make_simulation, isolated_tax_benefit_system): assert result > 0 class disposable_income(Variable): - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(household, period): return household.empty_array() @@ -116,7 +117,7 @@ def formula(household, period): def test_variable_name_conflict(tax_benefit_system): class disposable_income(Variable): reference = "disposable_income" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(household, period): return household.empty_array() diff --git a/tests/core/test_cycles.py b/tests/core/test_cycles.py index 0a9166d02..14886532c 100644 --- a/tests/core/test_cycles.py +++ b/tests/core/test_cycles.py @@ -4,6 +4,7 @@ from openfisca_core import periods, tools from openfisca_core.errors import CycleError +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder from openfisca_core.variables import Variable @@ -22,7 +23,7 @@ def simulation(tax_benefit_system): class variable1(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("variable2", period) @@ -31,7 +32,7 @@ def formula(person, period): class variable2(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("variable1", period) @@ -41,7 +42,7 @@ def formula(person, period): class variable3(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("variable4", period.last_month) @@ -50,7 +51,7 @@ def formula(person, period): class variable4(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("variable3", period) @@ -61,7 +62,7 @@ def formula(person, period): class variable5(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): variable6 = person("variable6", period.last_month) @@ -71,7 +72,7 @@ def formula(person, period): class variable6(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): variable5 = person("variable5", period) @@ -81,7 +82,7 @@ def formula(person, period): class variable7(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): variable5 = person("variable5", period) @@ -92,7 +93,7 @@ def formula(person, period): class cotisation(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): if period.start.month == 12: diff --git a/tests/core/test_formulas.py b/tests/core/test_formulas.py index b9b991f65..c8a537980 100644 --- a/tests/core/test_formulas.py +++ b/tests/core/test_formulas.py @@ -3,7 +3,8 @@ from openfisca_country_template import entities -from openfisca_core import commons, periods +from openfisca_core import commons +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder from openfisca_core.variables import Variable @@ -11,14 +12,14 @@ class choice(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH class uses_multiplication(Variable): value_type = int entity = entities.Person label = "Variable with formula that uses multiplication" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): choice = person("choice", period) @@ -30,7 +31,7 @@ class returns_scalar(Variable): value_type = int entity = entities.Person label = "Variable with formula that returns a scalar value" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return 666 @@ -40,7 +41,7 @@ class uses_switch(Variable): value_type = int entity = entities.Person label = "Variable with formula that uses switch" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): choice = person("choice", period) @@ -107,8 +108,8 @@ def test_group_encapsulation(): And calculations are projected to all the member families. """ - from openfisca_core import periods from openfisca_core.entities import build_entity + from openfisca_core.periods import DateUnit from openfisca_core.taxbenefitsystems import TaxBenefitSystem person_entity = build_entity( @@ -150,12 +151,12 @@ def test_group_encapsulation(): class household_level_variable(Variable): value_type = int entity = household_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY class projected_family_level_variable(Variable): value_type = int entity = family_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(family, period): return family.household("household_level_variable", period) diff --git a/tests/core/test_holders.py b/tests/core/test_holders.py index 2db121ece..088ca1593 100644 --- a/tests/core/test_holders.py +++ b/tests/core/test_holders.py @@ -8,6 +8,7 @@ from openfisca_core.errors import PeriodMismatchError from openfisca_core.holders import Holder from openfisca_core.memory_config import MemoryConfig +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder @@ -105,9 +106,9 @@ def test_permanent_variable_filled(single): simulation = single holder = simulation.person.get_holder("birth") value = numpy.asarray(["1980-01-01"], dtype=holder.variable.dtype) - holder.set_input(periods.period(periods.ETERNITY), value) + holder.set_input(periods.period(DateUnit.ETERNITY), value) assert holder.get_array(None) == value - assert holder.get_array(periods.ETERNITY) == value + assert holder.get_array(DateUnit.ETERNITY) == value assert holder.get_array("2016-01") == value diff --git a/tests/core/test_opt_out_cache.py b/tests/core/test_opt_out_cache.py index 55ad202db..e9fe3a246 100644 --- a/tests/core/test_opt_out_cache.py +++ b/tests/core/test_opt_out_cache.py @@ -3,6 +3,7 @@ from openfisca_country_template.entities import Person from openfisca_core import periods +from openfisca_core.periods import DateUnit from openfisca_core.variables import Variable PERIOD = periods.period("2016-01") @@ -12,14 +13,14 @@ class input(Variable): value_type = int entity = Person label = "Input variable" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH class intermediate(Variable): value_type = int entity = Person label = "Intermediate result that don't need to be cached" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("input", period) @@ -29,7 +30,7 @@ class output(Variable): value_type = int entity = Person label = "Output variable" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("intermediate", period) diff --git a/tests/core/test_projectors.py b/tests/core/test_projectors.py index 878b258bf..27391711c 100644 --- a/tests/core/test_projectors.py +++ b/tests/core/test_projectors.py @@ -1,8 +1,8 @@ import numpy as np -from openfisca_core import periods from openfisca_core.entities import build_entity from openfisca_core.indexed_enums import Enum +from openfisca_core.periods import DateUnit from openfisca_core.simulations.simulation_builder import SimulationBuilder from openfisca_core.taxbenefitsystems import TaxBenefitSystem from openfisca_core.variables import Variable @@ -138,14 +138,14 @@ class household_enum_variable(Variable): possible_values = enum default_value = enum.FIRST_OPTION entity = household - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY class projected_enum_variable(Variable): value_type = Enum possible_values = enum default_value = enum.FIRST_OPTION entity = person - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(person, period): return person.household("household_enum_variable", period) @@ -209,7 +209,7 @@ class household_projected_variable(Variable): possible_values = enum default_value = enum.FIRST_OPTION entity = household - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(household, period): return household.value_from_first_person( @@ -221,7 +221,7 @@ class person_enum_variable(Variable): possible_values = enum default_value = enum.FIRST_OPTION entity = person - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY system.add_variables(household_projected_variable, person_enum_variable) @@ -300,14 +300,14 @@ class household_level_variable(Variable): possible_values = enum default_value = enum.FIRST_OPTION entity = household_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY class projected_family_level_variable(Variable): value_type = Enum possible_values = enum default_value = enum.FIRST_OPTION entity = family_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(family, period): return family.household("household_level_variable", period) @@ -315,7 +315,7 @@ def formula(family, period): class decoded_projected_family_level_variable(Variable): value_type = str entity = family_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(family, period): return family.household("household_level_variable", period).decode_to_str() diff --git a/tests/core/test_reforms.py b/tests/core/test_reforms.py index 1b3a1f291..0c17bb116 100644 --- a/tests/core/test_reforms.py +++ b/tests/core/test_reforms.py @@ -6,6 +6,7 @@ from openfisca_core import holders, periods, simulations from openfisca_core.parameters import ParameterNode, ValuesHistory +from openfisca_core.periods import DateUnit, Instant from openfisca_core.reforms import Reform from openfisca_core.tools import assert_near from openfisca_core.variables import Variable @@ -16,7 +17,7 @@ class goes_to_school(Variable): default_value = True entity = Person label = "The person goes to school (only relevant for children)" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH class WithBasicIncomeNeutralized(Reform): @@ -318,7 +319,7 @@ class new_variable(Variable): value_type = int label = "Nouvelle variable introduite par la réforme" entity = Household - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(household, period): return household.empty_array() + 10 @@ -341,7 +342,7 @@ class new_dated_variable(Variable): value_type = int label = "Nouvelle variable introduite par la réforme" entity = Household - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula_2010_01_01(household, period): return household.empty_array() + 10 @@ -365,7 +366,7 @@ def apply(self): def test_update_variable(make_simulation, tax_benefit_system): class disposable_income(Variable): - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula_2018(household, period): return household.empty_array() + 10 @@ -402,7 +403,7 @@ def apply(self): def test_replace_variable(tax_benefit_system): class disposable_income(Variable): - definition_period = periods.MONTH + definition_period = DateUnit.MONTH entity = Person label = "Disposable income" value_type = float @@ -454,7 +455,7 @@ def apply(self): parameters_new_node = reform.parameters.children["new_node"] assert parameters_new_node is not None - instant = periods.Instant((2013, 1, 1)) + instant = Instant((2013, 1, 1)) parameters_at_instant = reform.get_parameters_at_instant(instant) assert parameters_at_instant.new_node.new_param is True @@ -464,7 +465,7 @@ class some_variable(Variable): value_type = int entity = Person label = "Variable with many attributes" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH set_input = holders.set_input_divide_by_period calculate_output = simulations.calculate_output_add diff --git a/tests/core/test_simulation_builder.py b/tests/core/test_simulation_builder.py index ecfccdcfd..d1dc0cde7 100644 --- a/tests/core/test_simulation_builder.py +++ b/tests/core/test_simulation_builder.py @@ -6,9 +6,10 @@ from openfisca_country_template import entities, situation_examples -from openfisca_core import periods, tools +from openfisca_core import tools from openfisca_core.errors import SituationParsingError from openfisca_core.indexed_enums import Enum +from openfisca_core.periods import DateUnit from openfisca_core.populations import Population from openfisca_core.simulations import Simulation, SimulationBuilder from openfisca_core.tools import test_runner @@ -18,7 +19,7 @@ @pytest.fixture def int_variable(persons): class intvar(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = int entity = persons @@ -31,7 +32,7 @@ def __init__(self): @pytest.fixture def date_variable(persons): class datevar(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = datetime.date entity = persons @@ -44,7 +45,7 @@ def __init__(self): @pytest.fixture def enum_variable(): class TestEnum(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = Enum dtype = "O" default_value = "0" diff --git a/tests/core/tools/test_runner/test_yaml_runner.py b/tests/core/tools/test_runner/test_yaml_runner.py index ac4ec0085..bf04ade9b 100644 --- a/tests/core/tools/test_runner/test_yaml_runner.py +++ b/tests/core/tools/test_runner/test_yaml_runner.py @@ -5,8 +5,9 @@ import numpy import pytest -from openfisca_core import errors, periods +from openfisca_core import errors from openfisca_core.entities import Entity +from openfisca_core.periods import DateUnit from openfisca_core.populations import Population from openfisca_core.tools.test_runner import YamlFile, YamlItem, _get_tax_benefit_system from openfisca_core.variables import Variable @@ -72,7 +73,7 @@ def __init__(self, test): class TestVariable(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = float def __init__(self): diff --git a/tests/core/variables/test_annualize.py b/tests/core/variables/test_annualize.py index 39a6316bf..7bf85d9a4 100644 --- a/tests/core/variables/test_annualize.py +++ b/tests/core/variables/test_annualize.py @@ -4,6 +4,7 @@ from openfisca_country_template.entities import Person from openfisca_core import periods +from openfisca_core.periods import DateUnit from openfisca_core.variables import Variable, get_annualized_variable @@ -14,7 +15,7 @@ def monthly_variable(): class monthly_variable(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period, parameters): variable.calculation_count += 1 @@ -46,7 +47,7 @@ def test_without_annualize(monthly_variable): yearly_sum = sum( person("monthly_variable", month) - for month in period.get_subperiods(periods.MONTH) + for month in period.get_subperiods(DateUnit.MONTH) ) assert monthly_variable.calculation_count == 11 @@ -61,7 +62,7 @@ def test_with_annualize(monthly_variable): yearly_sum = sum( person("monthly_variable", month) - for month in period.get_subperiods(periods.MONTH) + for month in period.get_subperiods(DateUnit.MONTH) ) assert monthly_variable.calculation_count == 0 @@ -78,7 +79,7 @@ def test_with_partial_annualize(monthly_variable): yearly_sum = sum( person("monthly_variable", month) - for month in period.get_subperiods(periods.MONTH) + for month in period.get_subperiods(DateUnit.MONTH) ) assert monthly_variable.calculation_count == 11 diff --git a/tests/core/variables/test_definition_period.py b/tests/core/variables/test_definition_period.py deleted file mode 100644 index 7938aaeae..000000000 --- a/tests/core/variables/test_definition_period.py +++ /dev/null @@ -1,43 +0,0 @@ -import pytest - -from openfisca_core import periods -from openfisca_core.variables import Variable - - -@pytest.fixture -def variable(persons): - class TestVariable(Variable): - value_type = float - entity = persons - - return TestVariable - - -def test_weekday_variable(variable): - variable.definition_period = periods.WEEKDAY - assert variable() - - -def test_week_variable(variable): - variable.definition_period = periods.WEEK - assert variable() - - -def test_day_variable(variable): - variable.definition_period = periods.DAY - assert variable() - - -def test_month_variable(variable): - variable.definition_period = periods.MONTH - assert variable() - - -def test_year_variable(variable): - variable.definition_period = periods.YEAR - assert variable() - - -def test_eternity_variable(variable): - variable.definition_period = periods.ETERNITY - assert variable() diff --git a/tests/core/variables/test_variables.py b/tests/core/variables/test_variables.py index 08a44a0ce..3b2790bae 100644 --- a/tests/core/variables/test_variables.py +++ b/tests/core/variables/test_variables.py @@ -8,7 +8,7 @@ import openfisca_country_template.situation_examples from openfisca_country_template.entities import Person -from openfisca_core import periods +from openfisca_core.periods import DateUnit from openfisca_core.simulation_builder import SimulationBuilder from openfisca_core.tools import assert_near from openfisca_core.variables import Variable @@ -67,7 +67,7 @@ def get_message(error): class variable__no_date(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable without date." @@ -88,7 +88,7 @@ def test_variable__no_date(): class variable__strange_end_attribute(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with dubious end attribute, no formula." end = "1989-00-00" @@ -113,7 +113,7 @@ def test_variable__strange_end_attribute(): class variable__end_attribute(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with end attribute, no formula." end = "1989-12-31" @@ -141,7 +141,7 @@ def test_variable__end_attribute_set_input(simulation): class end_attribute__one_simple_formula(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with end attribute, one formula without date." end = "1989-12-31" @@ -187,7 +187,7 @@ def test_dates__end_attribute__one_simple_formula(): class no_end_attribute__one_formula__strange_name(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable without end attribute, one stangely named formula." def formula_2015_toto(individu, period): @@ -208,7 +208,7 @@ def test_add__no_end_attribute__one_formula__strange_name(): class no_end_attribute__one_formula__start(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable without end attribute, one dated formula." def formula_2000_01_01(individu, period): @@ -241,7 +241,7 @@ class no_end_attribute__one_formula__eternity(Variable): value_type = int entity = Person definition_period = ( - periods.ETERNITY + DateUnit.ETERNITY ) # For this entity, this variable shouldn't evolve through time label = "Variable without end attribute, one dated formula." @@ -278,7 +278,7 @@ def test_call__no_end_attribute__one_formula__eternity_after(simulation): class no_end_attribute__formulas__start_formats(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable without end attribute, multiple dated formulas." def formula_2000(individu, period): @@ -339,7 +339,7 @@ def test_call__no_end_attribute__formulas__start_formats(simulation): class no_attribute__formulas__different_names__dates_overlap(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable, no end attribute, multiple dated formulas with different names but same dates." def formula_2000(individu, period): @@ -364,7 +364,7 @@ def test_add__no_attribute__formulas__different_names__dates_overlap(): class no_attribute__formulas__different_names__no_overlap(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable, no end attribute, multiple dated formulas with different names and no date overlap." def formula_2000_01_01(individu, period): @@ -404,7 +404,7 @@ def test_call__no_attribute__formulas__different_names__no_overlap(simulation): class end_attribute__one_formula__start(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with end attribute, one dated formula." end = "2001-12-31" @@ -432,7 +432,7 @@ def test_call__end_attribute__one_formula__start(simulation): class stop_attribute_before__one_formula__start(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with stop attribute only coming before formula start." end = "1990-01-01" @@ -454,7 +454,7 @@ def test_add__stop_attribute_before__one_formula__start(): class end_attribute_restrictive__one_formula(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = ( "Variable with end attribute, one dated formula and dates intervals overlap." ) @@ -484,7 +484,7 @@ def test_call__end_attribute_restrictive__one_formula(simulation): class end_attribute__formulas__different_names(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with end attribute, multiple dated formulas with different names." end = "2010-12-31" @@ -535,7 +535,7 @@ def test_unexpected_attr(): class variable_with_strange_attr(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH unexpected = "???" with raises(ValueError): diff --git a/tests/fixtures/appclient.py b/tests/fixtures/appclient.py index 035bd6f25..5edcfc2c9 100644 --- a/tests/fixtures/appclient.py +++ b/tests/fixtures/appclient.py @@ -20,7 +20,7 @@ def test_client(tax_benefit_system): class new_variable(Variable): value_type = float entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "New variable" reference = "https://law.gov.example/new_variable" # Always use the most official source diff --git a/tests/fixtures/variables.py b/tests/fixtures/variables.py index cd0d9b70c..aab7cda58 100644 --- a/tests/fixtures/variables.py +++ b/tests/fixtures/variables.py @@ -1,9 +1,9 @@ -from openfisca_core import periods +from openfisca_core.periods import DateUnit from openfisca_core.variables import Variable class TestVariable(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = float def __init__(self, entity):