-
Notifications
You must be signed in to change notification settings - Fork 80
Refactor subroles #1196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
bonjourmauko
wants to merge
7
commits into
master
Choose a base branch
from
compose-entity-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Refactor subroles #1196
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8d8f4e2
Add test to group entity
bonjourmauko 4f56cb9
Fix syntax & style
bonjourmauko e3ad22c
Reuse description
bonjourmauko e821407
Refactor subroles
bonjourmauko 5899152
Fix failing tests
bonjourmauko 2106d15
Fix typing
bonjourmauko c78940a
Bump version
bonjourmauko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""Actions related to the entities context.""" | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Iterable, Mapping | ||
from typing import Any | ||
|
||
from .entity import Entity | ||
from .group_entity import GroupEntity | ||
|
||
|
||
def build_entity( | ||
key: str, | ||
plural: str, | ||
label: str, | ||
doc: str = "", | ||
roles: Iterable[Mapping[str, Any]] | None = None, | ||
is_person: bool = False, | ||
class_override: Any | None = None, | ||
containing_entities: Iterable[str] = (), | ||
) -> Entity | GroupEntity: | ||
"""Build an Entity` or GroupEntity. | ||
|
||
Args: | ||
key (str): Key to identify the Entity or GroupEntity. | ||
plural (str): ``key``, pluralised. | ||
label (str): A summary description. | ||
doc (str): A full description. | ||
roles (list) : A list of Role, if it's a GroupEntity. | ||
is_person (bool): If is an individual, or not. | ||
class_override: ? | ||
containing_entities (list): Keys of contained entities. | ||
|
||
Returns: | ||
Entity or GroupEntity: | ||
Entity: When ``is_person`` is True. | ||
GroupEntity: When ``is_person`` is False. | ||
|
||
Raises: | ||
ValueError: If ``roles`` is not an Iterable. | ||
|
||
Examples: | ||
>>> from openfisca_core import entities | ||
|
||
>>> build_entity( | ||
... "syndicate", | ||
... "syndicates", | ||
... "Banks loaning jointly.", | ||
... roles = [], | ||
... containing_entities = [], | ||
... ) | ||
GroupEntity(syndicate) | ||
|
||
>>> build_entity( | ||
... "company", | ||
... "companies", | ||
... "A small or medium company.", | ||
... is_person = True, | ||
... ) | ||
Entity(company) | ||
|
||
>>> role = entities.Role({"key": "key"}, object()) | ||
|
||
>>> build_entity( | ||
... "syndicate", | ||
... "syndicates", | ||
... "Banks loaning jointly.", | ||
... roles = role, | ||
... ) | ||
Traceback (most recent call last): | ||
ValueError: Invalid value 'Role(key)' for 'roles', must be an iterable. | ||
|
||
""" | ||
|
||
if is_person: | ||
return Entity(key, plural, label, doc) | ||
|
||
if isinstance(roles, (list, tuple)): | ||
return GroupEntity(key, plural, label, doc, roles, containing_entities) | ||
|
||
raise ValueError(f"Invalid value '{roles}' for 'roles', must be an iterable.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import annotations | ||
|
||
import dataclasses | ||
import textwrap | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class Description: | ||
"""A description. | ||
|
||
Examples: | ||
>>> data = { | ||
... "key": "parent", | ||
... "label": "Parents", | ||
... "plural": "parents", | ||
... "doc": "\t\t\tThe one/two adults in charge of the household.", | ||
... } | ||
|
||
>>> description = Description(**data) | ||
|
||
>>> repr(Description) | ||
"<class 'openfisca_core.entities._description.Description'>" | ||
|
||
>>> repr(description) | ||
"Description(key='parent', plural='parents', label='Parents', ...)" | ||
|
||
>>> str(description) | ||
"Description(key='parent', plural='parents', label='Parents', ...)" | ||
|
||
>>> {description} | ||
{Description(key='parent', plural='parents', label='Parents', doc=...} | ||
|
||
>>> description.key | ||
'parent' | ||
|
||
.. versionadded:: 41.0.1 | ||
|
||
""" | ||
|
||
#: A key to identify an entity. | ||
key: str | ||
|
||
#: The ``key``, pluralised. | ||
plural: str | None = None | ||
|
||
#: A summary description. | ||
label: str | None = None | ||
|
||
#: A full description, non-indented. | ||
doc: str | None = None | ||
|
||
def __post_init__(self) -> None: | ||
if self.doc is not None: | ||
object.__setattr__(self, "doc", textwrap.dedent(self.doc)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
import dataclasses | ||
|
||
from .typing import Entity, GroupEntity, Role | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class SubRole: | ||
"""The sub-role of a Role. | ||
|
||
Each Role can be composed of one or several SubRole. For example, if you | ||
have a Role "parent", its sub-roles could include "mother" and "father". | ||
|
||
Attributes: | ||
role (Role): The Role the SubRole belongs to. | ||
key (str): A key to identify the SubRole. | ||
max (int): Max number of members. | ||
|
||
Args: | ||
role (Role): The Role the SubRole belongs to. | ||
key (str): A key to identify the SubRole. | ||
|
||
Examples: | ||
>>> from openfisca_core import entities | ||
|
||
>>> entity = entities.GroupEntity("person", "", "", "", {}) | ||
>>> role = entities.Role({"key": "sorority"}, entity) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sorority example is not very clear to me. |
||
>>> subrole = SubRole(role, "sister") | ||
|
||
>>> repr(SubRole) | ||
"<class 'openfisca_core.entities._subrole.SubRole'>" | ||
|
||
>>> repr(subrole) | ||
"SubRole(role=Role(sorority), key='sister', max=1)" | ||
|
||
>>> str(subrole) | ||
"SubRole(role=Role(sorority), key='sister', max=1)" | ||
|
||
>>> {subrole} | ||
{SubRole(role=Role(sorority), key='sister', max=1)} | ||
|
||
>>> subrole.entity.key | ||
'person' | ||
|
||
>>> subrole.role.key | ||
'sorority' | ||
|
||
>>> subrole.key | ||
'sister' | ||
|
||
>>> subrole.max | ||
1 | ||
|
||
.. versionadded:: 41.2.0 | ||
|
||
""" | ||
|
||
#: An id to identify the Role the SubRole belongs to. | ||
role: Role | ||
|
||
#: A key to identify the SubRole. | ||
key: str | ||
|
||
#: Max number of members. | ||
max: int = 1 | ||
|
||
@property | ||
def entity(self) -> Entity | GroupEntity: | ||
"""The Entity the SubRole transitively belongs to.""" | ||
return self.role.entity |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The group entity is not clear here.