Skip to content

Commit 0810400

Browse files
author
Mauko Quiroga
committed
Use literals as unit values
1 parent 6bd499c commit 0810400

File tree

15 files changed

+81
-81
lines changed

15 files changed

+81
-81
lines changed

openfisca_core/periods/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from .helpers import unit_weight, unit_weights # noqa: F401
4242

4343
for item in DateUnit:
44-
globals()[item.name] = item.value
44+
globals()[item.name.upper()] = item.value
4545

4646
str_by_instant_cache: Dict[Any, Any] = {}
4747
"""Cache to store :obj:`str` reprentations of :obj:`.Instant`.

tests/core/test_calculate_output.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010

1111
class simple_variable(Variable):
1212
entity = entities.Person
13-
definition_period = DateUnit.MONTH
13+
definition_period = DateUnit.MONTH.value
1414
value_type = int
1515

1616

1717
class variable_with_calculate_output_add(Variable):
1818
entity = entities.Person
19-
definition_period = DateUnit.MONTH
19+
definition_period = DateUnit.MONTH.value
2020
value_type = int
2121
calculate_output = simulations.calculate_output_add
2222

2323

2424
class variable_with_calculate_output_divide(Variable):
2525
entity = entities.Person
26-
definition_period = DateUnit.YEAR
26+
definition_period = DateUnit.YEAR.value
2727
value_type = int
2828
calculate_output = simulations.calculate_output_divide
2929

tests/core/test_countries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test_variable_with_reference(make_simulation, isolated_tax_benefit_system):
103103
assert result > 0
104104

105105
class disposable_income(Variable):
106-
definition_period = DateUnit.MONTH
106+
definition_period = DateUnit.MONTH.value
107107

108108
def formula(household, period):
109109
return household.empty_array()
@@ -120,7 +120,7 @@ def test_variable_name_conflict(tax_benefit_system):
120120

121121
class disposable_income(Variable):
122122
reference = "disposable_income"
123-
definition_period = DateUnit.MONTH
123+
definition_period = DateUnit.MONTH.value
124124

125125
def formula(household, period):
126126
return household.empty_array()

tests/core/test_cycles.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def simulation(tax_benefit_system):
2323
class variable1(Variable):
2424
value_type = int
2525
entity = entities.Person
26-
definition_period = DateUnit.MONTH
26+
definition_period = DateUnit.MONTH.value
2727

2828
def formula(person, period):
2929
return person('variable2', period)
@@ -32,7 +32,7 @@ def formula(person, period):
3232
class variable2(Variable):
3333
value_type = int
3434
entity = entities.Person
35-
definition_period = DateUnit.MONTH
35+
definition_period = DateUnit.MONTH.value
3636

3737
def formula(person, period):
3838
return person('variable1', period)
@@ -42,7 +42,7 @@ def formula(person, period):
4242
class variable3(Variable):
4343
value_type = int
4444
entity = entities.Person
45-
definition_period = DateUnit.MONTH
45+
definition_period = DateUnit.MONTH.value
4646

4747
def formula(person, period):
4848
return person('variable4', period.last_month)
@@ -51,7 +51,7 @@ def formula(person, period):
5151
class variable4(Variable):
5252
value_type = int
5353
entity = entities.Person
54-
definition_period = DateUnit.MONTH
54+
definition_period = DateUnit.MONTH.value
5555

5656
def formula(person, period):
5757
return person('variable3', period)
@@ -62,7 +62,7 @@ def formula(person, period):
6262
class variable5(Variable):
6363
value_type = int
6464
entity = entities.Person
65-
definition_period = DateUnit.MONTH
65+
definition_period = DateUnit.MONTH.value
6666

6767
def formula(person, period):
6868
variable6 = person('variable6', period.last_month)
@@ -72,7 +72,7 @@ def formula(person, period):
7272
class variable6(Variable):
7373
value_type = int
7474
entity = entities.Person
75-
definition_period = DateUnit.MONTH
75+
definition_period = DateUnit.MONTH.value
7676

7777
def formula(person, period):
7878
variable5 = person('variable5', period)
@@ -82,7 +82,7 @@ def formula(person, period):
8282
class variable7(Variable):
8383
value_type = int
8484
entity = entities.Person
85-
definition_period = DateUnit.MONTH
85+
definition_period = DateUnit.MONTH.value
8686

8787
def formula(person, period):
8888
variable5 = person('variable5', period)
@@ -93,7 +93,7 @@ def formula(person, period):
9393
class cotisation(Variable):
9494
value_type = int
9595
entity = entities.Person
96-
definition_period = DateUnit.MONTH
96+
definition_period = DateUnit.MONTH.value
9797

9898
def formula(person, period):
9999
if period.start.month == 12:

tests/core/test_formulas.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
class choice(Variable):
1414
value_type = int
1515
entity = entities.Person
16-
definition_period = DateUnit.MONTH
16+
definition_period = DateUnit.MONTH.value
1717

1818

1919
class uses_multiplication(Variable):
2020
value_type = int
2121
entity = entities.Person
2222
label = 'Variable with formula that uses multiplication'
23-
definition_period = DateUnit.MONTH
23+
definition_period = DateUnit.MONTH.value
2424

2525
def formula(person, period):
2626
choice = person('choice', period)
@@ -32,7 +32,7 @@ class returns_scalar(Variable):
3232
value_type = int
3333
entity = entities.Person
3434
label = 'Variable with formula that returns a scalar value'
35-
definition_period = DateUnit.MONTH
35+
definition_period = DateUnit.MONTH.value
3636

3737
def formula(person, period):
3838
return 666
@@ -42,7 +42,7 @@ class uses_switch(Variable):
4242
value_type = int
4343
entity = entities.Person
4444
label = 'Variable with formula that uses switch'
45-
definition_period = DateUnit.MONTH
45+
definition_period = DateUnit.MONTH.value
4646

4747
def formula(person, period):
4848
choice = person('choice', period)

tests/core/test_holders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ def test_permanent_variable_filled(single):
9090
simulation = single
9191
holder = simulation.person.get_holder('birth')
9292
value = numpy.asarray(['1980-01-01'], dtype = holder.variable.dtype)
93-
holder.set_input(periods.period(DateUnit.ETERNITY), value)
93+
holder.set_input(periods.period(DateUnit.ETERNITY.value), value)
9494
assert holder.get_array(None) == value
95-
assert holder.get_array(DateUnit.ETERNITY) == value
95+
assert holder.get_array(DateUnit.ETERNITY.value) == value
9696
assert holder.get_array('2016-01') == value
9797

9898

tests/core/test_opt_out_cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ class input(Variable):
1414
value_type = int
1515
entity = Person
1616
label = "Input variable"
17-
definition_period = DateUnit.MONTH
17+
definition_period = DateUnit.MONTH.value
1818

1919

2020
class intermediate(Variable):
2121
value_type = int
2222
entity = Person
2323
label = "Intermediate result that don't need to be cached"
24-
definition_period = DateUnit.MONTH
24+
definition_period = DateUnit.MONTH.value
2525

2626
def formula(person, period):
2727
return person('input', period)
@@ -31,7 +31,7 @@ class output(Variable):
3131
value_type = int
3232
entity = Person
3333
label = 'Output variable'
34-
definition_period = DateUnit.MONTH
34+
definition_period = DateUnit.MONTH.value
3535

3636
def formula(person, period):
3737
return person('intermediate', period)

tests/core/test_periods.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,43 @@
1717
# Years
1818

1919
def test_year():
20-
assert str(Period((DateUnit.YEAR, first_jan, 1))) == '2014'
20+
assert str(Period((DateUnit.YEAR.value, first_jan, 1))) == '2014'
2121

2222

2323
def test_12_months_is_a_year():
24-
assert str(Period((DateUnit.MONTH, first_jan, 12))) == '2014'
24+
assert str(Period((DateUnit.MONTH.value, first_jan, 12))) == '2014'
2525

2626

2727
def test_rolling_year():
28-
assert str(Period((DateUnit.MONTH, first_march, 12))) == 'year:2014-03'
29-
assert str(Period((DateUnit.YEAR, first_march, 1))) == 'year:2014-03'
28+
assert str(Period((DateUnit.MONTH.value, first_march, 12))) == 'year:2014-03'
29+
assert str(Period((DateUnit.YEAR.value, first_march, 1))) == 'year:2014-03'
3030

3131

3232
def test_several_years():
33-
assert str(Period((DateUnit.YEAR, first_jan, 3))) == 'year:2014:3'
34-
assert str(Period((DateUnit.YEAR, first_march, 3))) == 'year:2014-03:3'
33+
assert str(Period((DateUnit.YEAR.value, first_jan, 3))) == 'year:2014:3'
34+
assert str(Period((DateUnit.YEAR.value, first_march, 3))) == 'year:2014-03:3'
3535

3636

3737
# Months
3838

3939
def test_month():
40-
assert str(Period((DateUnit.MONTH, first_jan, 1))) == '2014-01'
40+
assert str(Period((DateUnit.MONTH.value, first_jan, 1))) == '2014-01'
4141

4242

4343
def test_several_months():
44-
assert str(Period((DateUnit.MONTH, first_jan, 3))) == 'month:2014-01:3'
45-
assert str(Period((DateUnit.MONTH, first_march, 3))) == 'month:2014-03:3'
44+
assert str(Period((DateUnit.MONTH.value, first_jan, 3))) == 'month:2014-01:3'
45+
assert str(Period((DateUnit.MONTH.value, first_march, 3))) == 'month:2014-03:3'
4646

4747

4848
# Days
4949

5050
def test_day():
51-
assert str(Period((DateUnit.DAY, first_jan, 1))) == '2014-01-01'
51+
assert str(Period((DateUnit.DAY.value, first_jan, 1))) == '2014-01-01'
5252

5353

5454
def test_several_days():
55-
assert str(Period((DateUnit.DAY, first_jan, 3))) == 'day:2014-01-01:3'
56-
assert str(Period((DateUnit.DAY, first_march, 3))) == 'day:2014-03-01:3'
55+
assert str(Period((DateUnit.DAY.value, first_jan, 3))) == 'day:2014-01-01:3'
56+
assert str(Period((DateUnit.DAY.value, first_march, 3))) == 'day:2014-03-01:3'
5757

5858

5959
'''
@@ -64,15 +64,15 @@ def test_several_days():
6464
# Years
6565

6666
def test_parsing_year():
67-
assert period('2014') == Period((DateUnit.YEAR, first_jan, 1))
67+
assert period('2014') == Period((DateUnit.YEAR.value, first_jan, 1))
6868

6969

7070
def test_parsing_rolling_year():
71-
assert period('year:2014-03') == Period((DateUnit.YEAR, first_march, 1))
71+
assert period('year:2014-03') == Period((DateUnit.YEAR.value, first_march, 1))
7272

7373

7474
def test_parsing_several_years():
75-
assert period('year:2014:2') == Period((DateUnit.YEAR, first_jan, 2))
75+
assert period('year:2014:2') == Period((DateUnit.YEAR.value, first_jan, 2))
7676

7777

7878
def test_wrong_syntax_several_years():
@@ -83,11 +83,11 @@ def test_wrong_syntax_several_years():
8383
# Months
8484

8585
def test_parsing_month():
86-
assert period('2014-01') == Period((DateUnit.MONTH, first_jan, 1))
86+
assert period('2014-01') == Period((DateUnit.MONTH.value, first_jan, 1))
8787

8888

8989
def test_parsing_several_months():
90-
assert period('month:2014-03:3') == Period((DateUnit.MONTH, first_march, 3))
90+
assert period('month:2014-03:3') == Period((DateUnit.MONTH.value, first_march, 3))
9191

9292

9393
def test_wrong_syntax_several_months():
@@ -98,11 +98,11 @@ def test_wrong_syntax_several_months():
9898
# Days
9999

100100
def test_parsing_day():
101-
assert period('2014-01-01') == Period((DateUnit.DAY, first_jan, 1))
101+
assert period('2014-01-01') == Period((DateUnit.DAY.value, first_jan, 1))
102102

103103

104104
def test_parsing_several_days():
105-
assert period('day:2014-03-01:3') == Period((DateUnit.DAY, first_march, 3))
105+
assert period('day:2014-03-01:3') == Period((DateUnit.DAY.value, first_march, 3))
106106

107107

108108
def test_wrong_syntax_several_days():
@@ -160,7 +160,7 @@ def test_ambiguous_period():
160160

161161
def test_deprecated_signature():
162162
with pytest.raises(TypeError):
163-
period(DateUnit.MONTH, 2014)
163+
period(DateUnit.MONTH.value, 2014)
164164

165165

166166
def test_wrong_argument():
@@ -184,13 +184,13 @@ def test_empty_string():
184184

185185

186186
@pytest.mark.parametrize("test", [
187-
(period('year:2014:2'), DateUnit.YEAR, 2, period('2014'), period('2015')),
188-
(period(2017), DateUnit.MONTH, 12, period('2017-01'), period('2017-12')),
189-
(period('year:2014:2'), DateUnit.MONTH, 24, period('2014-01'), period('2015-12')),
190-
(period('month:2014-03:3'), DateUnit.MONTH, 3, period('2014-03'), period('2014-05')),
191-
(period(2017), DateUnit.DAY, 365, period('2017-01-01'), period('2017-12-31')),
192-
(period('year:2014:2'), DateUnit.DAY, 730, period('2014-01-01'), period('2015-12-31')),
193-
(period('month:2014-03:3'), DateUnit.DAY, 92, period('2014-03-01'), period('2014-05-31')),
187+
(period('year:2014:2'), DateUnit.YEAR.value, 2, period('2014'), period('2015')),
188+
(period(2017), DateUnit.MONTH.value, 12, period('2017-01'), period('2017-12')),
189+
(period('year:2014:2'), DateUnit.MONTH.value, 24, period('2014-01'), period('2015-12')),
190+
(period('month:2014-03:3'), DateUnit.MONTH.value, 3, period('2014-03'), period('2014-05')),
191+
(period(2017), DateUnit.DAY.value, 365, period('2017-01-01'), period('2017-12-31')),
192+
(period('year:2014:2'), DateUnit.DAY.value, 730, period('2014-01-01'), period('2015-12-31')),
193+
(period('month:2014-03:3'), DateUnit.DAY.value, 92, period('2014-03-01'), period('2014-05-31')),
194194
])
195195
def test_subperiods(test):
196196

tests/core/test_reforms.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class goes_to_school(Variable):
1515
default_value = True
1616
entity = Person
1717
label = "The person goes to school (only relevant for children)"
18-
definition_period = DateUnit.MONTH
18+
definition_period = DateUnit.MONTH.value
1919

2020

2121
class WithBasicIncomeNeutralized(Reform):
@@ -216,7 +216,7 @@ class new_variable(Variable):
216216
value_type = int
217217
label = "Nouvelle variable introduite par la réforme"
218218
entity = Household
219-
definition_period = DateUnit.MONTH
219+
definition_period = DateUnit.MONTH.value
220220

221221
def formula(household, period):
222222
return household.empty_array() + 10
@@ -240,7 +240,7 @@ class new_dated_variable(Variable):
240240
value_type = int
241241
label = "Nouvelle variable introduite par la réforme"
242242
entity = Household
243-
definition_period = DateUnit.MONTH
243+
definition_period = DateUnit.MONTH.value
244244

245245
def formula_2010_01_01(household, period):
246246
return household.empty_array() + 10
@@ -263,7 +263,7 @@ def apply(self):
263263
def test_update_variable(make_simulation, tax_benefit_system):
264264

265265
class disposable_income(Variable):
266-
definition_period = DateUnit.MONTH
266+
definition_period = DateUnit.MONTH.value
267267

268268
def formula_2018(household, period):
269269
return household.empty_array() + 10
@@ -294,7 +294,7 @@ def apply(self):
294294
def test_replace_variable(tax_benefit_system):
295295

296296
class disposable_income(Variable):
297-
definition_period = DateUnit.MONTH
297+
definition_period = DateUnit.MONTH.value
298298
entity = Person
299299
label = "Disposable income"
300300
value_type = float
@@ -355,7 +355,7 @@ class some_variable(Variable):
355355
value_type = int
356356
entity = Person
357357
label = "Variable with many attributes"
358-
definition_period = DateUnit.MONTH
358+
definition_period = DateUnit.MONTH.value
359359
set_input = set_input_divide_by_period
360360
calculate_output = calculate_output_add
361361

0 commit comments

Comments
 (0)