From 57d74a0bb33eaafd1024fe1f43eea2f90d39b373 Mon Sep 17 00:00:00 2001 From: Mauko Quiroga Date: Wed, 18 Sep 2024 13:43:24 +0200 Subject: [PATCH] doc(types): add projector types --- .../projectors/entity_to_person_projector.py | 2 +- .../first_person_to_entity_projector.py | 2 +- openfisca_core/projectors/helpers.py | 18 ++++++------- openfisca_core/projectors/types.py | 25 +++++++------------ .../unique_role_to_entity_projector.py | 2 +- 5 files changed, 20 insertions(+), 29 deletions(-) diff --git a/openfisca_core/projectors/entity_to_person_projector.py b/openfisca_core/projectors/entity_to_person_projector.py index ca6245a1f..392fda08a 100644 --- a/openfisca_core/projectors/entity_to_person_projector.py +++ b/openfisca_core/projectors/entity_to_person_projector.py @@ -4,7 +4,7 @@ class EntityToPersonProjector(Projector): """For instance person.family.""" - def __init__(self, entity, parent=None): + def __init__(self, entity, parent=None) -> None: self.reference_entity = entity self.parent = parent diff --git a/openfisca_core/projectors/first_person_to_entity_projector.py b/openfisca_core/projectors/first_person_to_entity_projector.py index 4b4e7b799..d986460cd 100644 --- a/openfisca_core/projectors/first_person_to_entity_projector.py +++ b/openfisca_core/projectors/first_person_to_entity_projector.py @@ -4,7 +4,7 @@ class FirstPersonToEntityProjector(Projector): """For instance famille.first_person.""" - def __init__(self, entity, parent=None): + def __init__(self, entity, parent=None) -> None: self.target_entity = entity self.reference_entity = entity.members self.parent = parent diff --git a/openfisca_core/projectors/helpers.py b/openfisca_core/projectors/helpers.py index 9c666eb6b..d3d8a21c7 100644 --- a/openfisca_core/projectors/helpers.py +++ b/openfisca_core/projectors/helpers.py @@ -4,7 +4,7 @@ from openfisca_core import entities, projectors -from .types import GroupEntity, GroupPopulation, Role, SingleEntity, SinglePopulation +from . import types as t def projectable(function): @@ -17,10 +17,10 @@ def projectable(function): def get_projector_from_shortcut( - population: SinglePopulation | GroupPopulation, + population: t.CorePopulation, shortcut: str, - parent: projectors.Projector | None = None, -) -> projectors.Projector | None: + parent: t.Projector | None = None, +) -> t.Projector | None: """Get a projector from a shortcut. Projectors are used to project an invidividual Population's or a @@ -108,12 +108,10 @@ def get_projector_from_shortcut( """ - entity: SingleEntity | GroupEntity = population.entity + entity: t.CoreEntity = population.entity if isinstance(entity, entities.Entity): - populations: Mapping[ - str, SinglePopulation | GroupPopulation - ] = population.simulation.populations + populations: Mapping[str, t.CorePopulation] = population.simulation.populations if shortcut not in populations.keys(): return None @@ -124,8 +122,8 @@ def get_projector_from_shortcut( return projectors.FirstPersonToEntityProjector(population, parent) if isinstance(entity, entities.GroupEntity): - roles: Iterable[Role] = entity.roles - role: Role | None = entities.find_role(roles, shortcut, total=1) + roles: Iterable[t.Role] = entity.roles + role: t.Role | None = entities.find_role(roles, shortcut, total=1) if role is not None: return projectors.UniqueRoleToEntityProjector(population, role, parent) diff --git a/openfisca_core/projectors/types.py b/openfisca_core/projectors/types.py index a090c4813..e145cc9a1 100644 --- a/openfisca_core/projectors/types.py +++ b/openfisca_core/projectors/types.py @@ -8,11 +8,7 @@ # Entities -class SingleEntity(t.SingleEntity, Protocol): - ... - - -class GroupEntity(t.GroupEntity, Protocol): +class CoreEntity(t.CoreEntity, Protocol): ... @@ -20,22 +16,19 @@ class Role(t.Role, Protocol): ... -# Populations +# Projectors -class SinglePopulation(t.SinglePopulation, Protocol): - @property - def entity(self) -> t.SingleEntity: - ... +class Projector(Protocol): + ... - @property - def simulation(self) -> Simulation: - ... + +# Populations -class GroupPopulation(t.GroupPopulation, Protocol): +class CorePopulation(t.CorePopulation, Protocol): @property - def entity(self) -> t.GroupEntity: + def entity(self) -> CoreEntity: ... @property @@ -48,5 +41,5 @@ def simulation(self) -> Simulation: class Simulation(t.Simulation, Protocol): @property - def populations(self) -> Mapping[str, SinglePopulation | GroupPopulation]: + def populations(self) -> Mapping[str, CorePopulation]: ... diff --git a/openfisca_core/projectors/unique_role_to_entity_projector.py b/openfisca_core/projectors/unique_role_to_entity_projector.py index fed2f249c..c56548433 100644 --- a/openfisca_core/projectors/unique_role_to_entity_projector.py +++ b/openfisca_core/projectors/unique_role_to_entity_projector.py @@ -4,7 +4,7 @@ class UniqueRoleToEntityProjector(Projector): """For instance famille.declarant_principal.""" - def __init__(self, entity, role, parent=None): + def __init__(self, entity, role, parent=None) -> None: self.target_entity = entity self.reference_entity = entity.members self.parent = parent