3
3
from __future__ import annotations
4
4
5
5
from collections .abc import Callable , Iterable , Sequence
6
- from typing import Protocol , TypeVar , TypedDict , Union
6
+ from typing import NewType , Protocol , TypeVar , TypedDict , Union
7
7
from typing_extensions import NotRequired , Required , TypeAlias
8
8
9
9
import datetime
10
- from abc import abstractmethod
11
10
12
11
from numpy import bool_ as Bool
13
12
from numpy import datetime64 as Date
19
18
from openfisca_core import types as t
20
19
21
20
# Generic type variables.
22
- D = TypeVar ("D" )
23
- E = TypeVar ("E" , covariant = True )
24
21
G = TypeVar ("G" , covariant = True )
25
22
T = TypeVar ("T" , Bool , Date , Enum , Float , Int , String , covariant = True )
26
23
U = TypeVar ("U" , bool , datetime .date , float , str )
27
24
V = TypeVar ("V" , covariant = True )
28
25
26
+ # New types.
27
+ PeriodStr = NewType ("PeriodStr" , str )
28
+ EntityKey = NewType ("EntityKey" , str )
29
+ EntityPlural = NewType ("EntityPlural" , str )
30
+ VariableName = NewType ("VariableName" , str )
31
+
32
+ # Type aliases.
33
+
29
34
#: Type alias for numpy arrays values.
30
35
Item : TypeAlias = Union [Bool , Date , Enum , Float , Int , String ]
31
36
37
+ #: Type Alias for a numpy Array.
38
+ Array : TypeAlias = t .Array
32
39
33
40
# Entities
34
41
35
42
36
- #: Type alias for a simulation dictionary defining the roles.
37
- Roles : TypeAlias = dict [str , Union [str , Iterable [str ]]]
38
-
39
-
40
43
class CoreEntity (t .CoreEntity , Protocol ):
41
- key : str
42
- plural : str | None
44
+ key : EntityKey
45
+ plural : EntityPlural | None
43
46
44
47
def get_variable (
45
48
self ,
46
- __variable_name : str ,
47
- __check_existence : bool = ...,
49
+ __variable_name : VariableName ,
50
+ check_existence : bool = ...,
48
51
) -> Variable [T ] | None :
49
52
...
50
53
@@ -55,7 +58,6 @@ class SingleEntity(t.SingleEntity, Protocol):
55
58
56
59
class GroupEntity (t .GroupEntity , Protocol ):
57
60
@property
58
- @abstractmethod
59
61
def flattened_roles (self ) -> Iterable [Role [G ]]:
60
62
...
61
63
@@ -69,11 +71,10 @@ class Role(t.Role, Protocol[G]):
69
71
70
72
class Holder (t .Holder , Protocol [V ]):
71
73
@property
72
- @abstractmethod
73
74
def variable (self ) -> Variable [T ]:
74
75
...
75
76
76
- def get_array (self , __period : str ) -> t .Array [T ] | None :
77
+ def get_array (self , __period : PeriodStr ) -> t .Array [T ] | None :
77
78
...
78
79
79
80
def set_input (
@@ -94,18 +95,19 @@ class Period(t.Period, Protocol):
94
95
# Populations
95
96
96
97
97
- class CorePopulation (t .CorePopulation , Protocol [ D ] ):
98
- entity : D
98
+ class CorePopulation (t .CorePopulation , Protocol ):
99
+ entity : CoreEntity
99
100
100
- def get_holder (self , __variable_name : str ) -> Holder [V ]:
101
+ def get_holder (self , __variable_name : VariableName ) -> Holder [V ]:
101
102
...
102
103
103
104
104
- class SinglePopulation (t .SinglePopulation , Protocol [ E ] ):
105
- ...
105
+ class SinglePopulation (t .SinglePopulation , Protocol ):
106
+ entity : SingleEntity
106
107
107
108
108
- class GroupPopulation (t .GroupPopulation , Protocol [E ]):
109
+ class GroupPopulation (t .GroupPopulation , Protocol ):
110
+ entity : GroupEntity
109
111
members_entity_id : t .Array [String ]
110
112
111
113
def nb_persons (self , __role : Role [G ] | None = ...) -> int :
@@ -114,6 +116,29 @@ def nb_persons(self, __role: Role[G] | None = ...) -> int:
114
116
115
117
# Simulations
116
118
119
+ #: Dictionary with axes parameters per variable.
120
+ InputBuffer : TypeAlias = dict [VariableName , dict [PeriodStr , Array ]]
121
+
122
+ #: Dictionary with entity/population key/pais.
123
+ Populations : TypeAlias = dict [EntityKey , GroupPopulation ]
124
+
125
+ #: Dictionary with single entity count per group entity.
126
+ EntityCounts : TypeAlias = dict [EntityPlural , int ]
127
+
128
+ #: Dictionary with a list of single entities per group entity.
129
+ EntityIds : TypeAlias = dict [EntityPlural , Iterable [int ]]
130
+
131
+ #: Dictionary with a list of members per group entity.
132
+ Memberships : TypeAlias = dict [EntityPlural , Iterable [int ]]
133
+
134
+ #: Dictionary with a list of roles per group entity.
135
+ EntityRoles : TypeAlias = dict [EntityPlural , Iterable [int ]]
136
+
137
+ #: Dictionary with a map between variables and entities.
138
+ VariableEntity : TypeAlias = dict [VariableName , CoreEntity ]
139
+
140
+ #: Type alias for a simulation dictionary defining the roles.
141
+ Roles : TypeAlias = dict [str , Union [str , Iterable [str ]]]
117
142
118
143
#: Type alias for a simulation dictionary with undated variables.
119
144
UndatedVariable : TypeAlias = dict [str , object ]
@@ -169,21 +194,18 @@ class Simulation(t.Simulation, Protocol):
169
194
170
195
class TaxBenefitSystem (t .TaxBenefitSystem , Protocol ):
171
196
@property
172
- @abstractmethod
173
197
def person_entity (self ) -> SingleEntity :
174
198
...
175
199
176
200
@person_entity .setter
177
- @abstractmethod
178
201
def person_entity (self , person_entity : SingleEntity ) -> None :
179
202
...
180
203
181
204
@property
182
- @abstractmethod
183
205
def variables (self ) -> dict [str , V ]:
184
206
...
185
207
186
- def entities_by_singular (self ) -> dict [str , E ]:
208
+ def entities_by_singular (self ) -> dict [str , CoreEntity ]:
187
209
...
188
210
189
211
def entities_plural (self ) -> Iterable [str ]:
@@ -198,7 +220,7 @@ def get_variable(
198
220
199
221
def instantiate_entities (
200
222
self ,
201
- ) -> dict [ str , GroupPopulation [ E ]] :
223
+ ) -> Populations :
202
224
...
203
225
204
226
@@ -209,7 +231,7 @@ class Variable(t.Variable, Protocol[T]):
209
231
calculate_output : Callable [[Simulation , str , str ], t .Array [T ]] | None
210
232
definition_period : str
211
233
end : str
212
- name : str
234
+ name : VariableName
213
235
214
236
def default_array (self , __array_size : int ) -> t .Array [T ]:
215
237
...
0 commit comments