Skip to content

Commit

Permalink
Do not force ordered roles
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Sep 27, 2023
1 parent 29407ea commit 751ec4c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
19 changes: 9 additions & 10 deletions openfisca_core/entities/group_entity.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from collections.abc import Sequence
from collections.abc import Iterable, Mapping
from typing import Any

from itertools import chain

from .entity import Entity
from .role import Role
from .typing import HasKey


class GroupEntity(Entity):
Expand All @@ -21,10 +23,6 @@ class GroupEntity(Entity):
containing_entities: The list of keys of group entities whose members
are guaranteed to be a superset of this group's entities.
.. versionchanged:: 35.7.0
Added ``containing_entities``, that allows the defining of group
entities which entirely contain other group entities.
""" # noqa RST301

def __init__(
Expand All @@ -33,8 +31,8 @@ def __init__(
plural: str,
label: str,
doc: str,
roles: Sequence[HasKey],
containing_entities: Sequence[str] = (),
roles: Iterable[Mapping[str, Any]],
containing_entities: Iterable[str] = (),
) -> None:
super().__init__(key, plural, label, doc)
self.roles_description = roles
Expand All @@ -50,8 +48,9 @@ def __init__(
setattr(self, subrole.key.upper(), subrole)
role.subroles.append(subrole)
role.max = len(role.subroles)
self.flattened_roles = sum(
[role2.subroles or [role2] for role2 in self.roles], []
self.flattened_roles = tuple(
chain.from_iterable(role.subroles or [role] for role in self.roles)
)

self.is_person = False
self.containing_entities = containing_entities
7 changes: 4 additions & 3 deletions openfisca_core/entities/role.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from collections.abc import Iterable, Mapping
from typing import Any

import dataclasses
Expand Down Expand Up @@ -46,15 +47,15 @@ class Role:
"""

def __init__(self, description: dict[str, Any], entity: HasKey) -> None:
def __init__(self, description: Mapping[str, Any], entity: HasKey) -> None:
role_description: _RoleDescription = _RoleDescription(**description)
self.entity: HasKey = entity
self.key: str = role_description.key
self.plural: str | None = role_description.plural
self.label: str | None = role_description.label
self.doc: str = role_description.doc
self.max: int | None = role_description.max
self.subroles: list[Role] | None = None
self.subroles: Iterable[Role] | None = None

def __eq__(self, other: object) -> bool:
if not isinstance(other, Role):
Expand Down Expand Up @@ -113,7 +114,7 @@ class _RoleDescription:
max: int | None = None

#: A list of subroles.
subroles: list[str] | None = None
subroles: Iterable[str] | None = None

def __post_init__(self) -> None:
object.__setattr__(self, "doc", textwrap.dedent(self.doc))
11 changes: 5 additions & 6 deletions openfisca_core/entities/tests/test_group_entity.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from collections.abc import Mapping
from typing import Any

import pytest

from openfisca_core import entities

from ..typing import HasKey


@pytest.fixture
def parent() -> str:
Expand Down Expand Up @@ -46,22 +45,22 @@ def third_parent() -> str:
def role(parent: str, first_parent: str, third_parent: str) -> Any:
"""A role."""

return {"key": parent, "subroles": [first_parent, third_parent]}
return {"key": parent, "subroles": {first_parent, third_parent}}


@pytest.fixture
def group_entity(role: HasKey) -> entities.GroupEntity:
def group_entity(role: Mapping[str, Any]) -> entities.GroupEntity:
"""A group entity."""

return entities.GroupEntity("key", "label", "plural", "doc", [role])
return entities.GroupEntity("key", "label", "plural", "doc", (role,))


def test_init_when_doc_indented() -> None:
"""De-indent the ``doc`` attribute if it is passed at initialisation."""

key = "\tkey"
doc = "\tdoc"
group_entity = entities.GroupEntity(key, "label", "plural", doc, [])
group_entity = entities.GroupEntity(key, "label", "plural", doc, ())
assert group_entity.key == key
assert group_entity.doc != doc

Expand Down

0 comments on commit 751ec4c

Please sign in to comment.