Skip to content

Commit

Permalink
fix(types): entities
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Sep 16, 2024
1 parent 629a467 commit f652724
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 28 deletions.
12 changes: 11 additions & 1 deletion openfisca_core/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@
from .helpers import build_entity, find_role
from .role import Role

__all__ = ["Entity", "GroupEntity", "Role", "build_entity", "find_role", "types"]
SingleEntity = Entity

__all__ = [
"Entity",
"SingleEntity",
"GroupEntity",
"Role",
"build_entity",
"find_role",
"types",
]
8 changes: 6 additions & 2 deletions openfisca_core/entities/_core_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from abc import abstractmethod

from .role import Role
from .types import Entity
from .types import CoreEntity


class _CoreEntity:
Expand Down Expand Up @@ -49,12 +49,16 @@ def get_variable(
check_existence: bool = False,
) -> Variable | None:
"""Get a ``variable_name`` from ``variables``."""
if self._tax_benefit_system is None:
raise ValueError(
"You must set 'tax_benefit_system' before calling this method."
)
return self._tax_benefit_system.get_variable(variable_name, check_existence)

def check_variable_defined_for_entity(self, variable_name: str) -> None:
"""Check if ``variable_name`` is defined for ``self``."""
variable: Variable | None
entity: Entity
entity: CoreEntity

variable = self.get_variable(variable_name, check_existence=True)

Expand Down
6 changes: 3 additions & 3 deletions openfisca_core/entities/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dataclasses
import textwrap

from .types import Entity
from .types import SingleEntity


class Role:
Expand Down Expand Up @@ -48,7 +48,7 @@ class Role:
"""

#: The Entity the Role belongs to.
entity: Entity
entity: SingleEntity

#: A description of the Role.
description: _Description
Expand Down Expand Up @@ -79,7 +79,7 @@ def doc(self) -> str | None:
"""A full description, non-indented."""
return self.description.doc

def __init__(self, description: Mapping[str, Any], entity: Entity) -> None:
def __init__(self, description: Mapping[str, Any], entity: SingleEntity) -> None:
self.description = _Description(
**{
key: value
Expand Down
15 changes: 12 additions & 3 deletions openfisca_core/entities/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
from collections.abc import Iterable
from typing import Protocol, TypedDict

from openfisca_core import types

class Entity(Protocol):
# Entities


class CoreEntity(types.CoreEntity, Protocol):
...


class GroupEntity(Protocol):
class SingleEntity(types.SingleEntity, Protocol):
key: str
plural: str | None


class GroupEntity(types.GroupEntity, Protocol):
...


class Role(Protocol):
class Role(types.Role, Protocol):
max: int | None
subroles: Iterable[Role] | None

Expand Down
6 changes: 3 additions & 3 deletions openfisca_core/populations/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict, NamedTuple, Optional, Sequence, Union
from typing_extensions import TypedDict

from openfisca_core.types import Array, Entity, Period, Role, Simulation
from openfisca_core.types import Array, Period, Role, Simulation, SingleEntity

import traceback

Expand All @@ -16,12 +16,12 @@

class Population:
simulation: Optional[Simulation]
entity: Entity
entity: SingleEntity
_holders: Dict[str, holders.Holder]
count: int
ids: Array[str]

def __init__(self, entity: Entity) -> None:
def __init__(self, entity: SingleEntity) -> None:
self.simulation = None
self.entity = entity
self._holders = {}
Expand Down
4 changes: 2 additions & 2 deletions openfisca_core/projectors/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections.abc import Mapping

from openfisca_core.entities.types import Entity, GroupEntity, Role
from openfisca_core.types import GroupEntity, Role, SingleEntity

from openfisca_core import entities, projectors

Expand Down Expand Up @@ -110,7 +110,7 @@ def get_projector_from_shortcut(
"""

entity: Entity | GroupEntity = population.entity
entity: SingleEntity | GroupEntity = population.entity

if isinstance(entity, entities.Entity):
populations: Mapping[
Expand Down
4 changes: 2 additions & 2 deletions openfisca_core/projectors/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from collections.abc import Mapping
from typing import Protocol

from openfisca_core.entities.types import Entity, GroupEntity
from openfisca_core.types import GroupEntity, SingleEntity


class Population(Protocol):
@property
def entity(self) -> Entity:
def entity(self) -> SingleEntity:
...

@property
Expand Down
10 changes: 9 additions & 1 deletion openfisca_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# Entities


class Entity(Protocol):
class CoreEntity(Protocol):
key: Any
plural: Any

Expand All @@ -51,6 +51,14 @@ def get_variable(
...


class SingleEntity(CoreEntity, Protocol):
...


class GroupEntity(CoreEntity, Protocol):
...


class Role(Protocol):
entity: Any
subroles: Any
Expand Down
1 change: 1 addition & 0 deletions openfisca_tasks/lint.mk
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ check-types:
@$(call print_help,$@:)
@mypy \
openfisca_core/commons \
openfisca_core/entities \
openfisca_core/types.py
@$(call print_pass,$@:)

Expand Down
13 changes: 2 additions & 11 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ skip_covered = true
skip_empty = true

[tool:pytest]
addopts = --doctest-modules --disable-pytest-warnings --showlocals
addopts = --disable-pytest-warnings --doctest-modules --showlocals
doctest_optionflags = ELLIPSIS IGNORE_EXCEPTION_DETAIL NUMBER NORMALIZE_WHITESPACE
python_files = **/*.py
testpaths = tests
Expand All @@ -72,14 +72,5 @@ non_interactive = true
plugins = numpy.typing.mypy_plugin
python_version = 3.9

[mypy-openfisca_core.commons.tests.*]
ignore_errors = True

[mypy-openfisca_core.holders.tests.*]
ignore_errors = True

[mypy-openfisca_core.periods.tests.*]
ignore_errors = True

[mypy-openfisca_core.scripts.*]
[mypy-openfisca_core.*.tests.*]
ignore_errors = True

0 comments on commit f652724

Please sign in to comment.