-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve commons module typing (#1219)
- Loading branch information
Showing
12 changed files
with
123 additions
and
224 deletions.
There are no files selected for viewing
This file contains 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 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 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
Empty file.
This file contains 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
130 changes: 76 additions & 54 deletions
130
openfisca_core/types/_domain.py → openfisca_core/types.py
This file contains 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 |
---|---|---|
@@ -1,146 +1,168 @@ | ||
from __future__ import annotations | ||
|
||
import typing_extensions | ||
from typing import Any, Optional | ||
from typing_extensions import Protocol | ||
from collections.abc import Sequence | ||
from numpy.typing import NDArray | ||
from typing import Any, TypeVar | ||
from typing_extensions import Protocol, TypeAlias | ||
|
||
import abc | ||
|
||
import numpy | ||
|
||
N = TypeVar("N", bound=numpy.generic, covariant=True) | ||
|
||
class Entity(Protocol): | ||
"""Entity protocol.""" | ||
#: Type representing an numpy array. | ||
Array: TypeAlias = NDArray[N] | ||
|
||
L = TypeVar("L") | ||
|
||
#: Type representing an array-like object. | ||
ArrayLike: TypeAlias = Sequence[L] | ||
|
||
#: Type variable representing an error. | ||
E = TypeVar("E", covariant=True) | ||
|
||
#: Type variable representing a value. | ||
A = TypeVar("A", covariant=True) | ||
|
||
|
||
# Entities | ||
|
||
|
||
class Entity(Protocol): | ||
key: Any | ||
plural: Any | ||
|
||
@abc.abstractmethod | ||
def check_role_validity(self, role: Any) -> None: | ||
"""Abstract method.""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def check_variable_defined_for_entity(self, variable_name: Any) -> None: | ||
"""Abstract method.""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def get_variable( | ||
self, | ||
variable_name: Any, | ||
check_existence: Any = ..., | ||
) -> Optional[Any]: | ||
"""Abstract method.""" | ||
) -> Any | None: | ||
... | ||
|
||
|
||
class Formula(Protocol): | ||
"""Formula protocol.""" | ||
class Role(Protocol): | ||
entity: Any | ||
subroles: Any | ||
|
||
@abc.abstractmethod | ||
def __call__( | ||
self, | ||
population: Population, | ||
instant: Instant, | ||
params: Params, | ||
) -> numpy.ndarray: | ||
"""Abstract method.""" | ||
|
||
# Holders | ||
|
||
class Holder(Protocol): | ||
"""Holder protocol.""" | ||
|
||
class Holder(Protocol): | ||
@abc.abstractmethod | ||
def clone(self, population: Any) -> Holder: | ||
"""Abstract method.""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def get_memory_usage(self) -> Any: | ||
"""Abstract method.""" | ||
... | ||
|
||
|
||
class Instant(Protocol): | ||
"""Instant protocol.""" | ||
# Parameters | ||
|
||
|
||
@typing_extensions.runtime_checkable | ||
class ParameterNodeAtInstant(Protocol): | ||
"""ParameterNodeAtInstant protocol.""" | ||
... | ||
|
||
|
||
class Params(Protocol): | ||
"""Params protocol.""" | ||
# Periods | ||
|
||
@abc.abstractmethod | ||
def __call__(self, instant: Instant) -> ParameterNodeAtInstant: | ||
"""Abstract method.""" | ||
|
||
class Instant(Protocol): | ||
... | ||
|
||
|
||
@typing_extensions.runtime_checkable | ||
class Period(Protocol): | ||
"""Period protocol.""" | ||
|
||
@property | ||
@abc.abstractmethod | ||
def start(self) -> Any: | ||
"""Abstract method.""" | ||
... | ||
|
||
@property | ||
@abc.abstractmethod | ||
def unit(self) -> Any: | ||
"""Abstract method.""" | ||
... | ||
|
||
|
||
class Population(Protocol): | ||
"""Population protocol.""" | ||
# Populations | ||
|
||
|
||
class Population(Protocol): | ||
entity: Any | ||
|
||
@abc.abstractmethod | ||
def get_holder(self, variable_name: Any) -> Any: | ||
"""Abstract method.""" | ||
|
||
... | ||
|
||
class Role(Protocol): | ||
"""Role protocol.""" | ||
|
||
entity: Any | ||
subroles: Any | ||
# Simulations | ||
|
||
|
||
class Simulation(Protocol): | ||
"""Simulation protocol.""" | ||
|
||
@abc.abstractmethod | ||
def calculate(self, variable_name: Any, period: Any) -> Any: | ||
"""Abstract method.""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def calculate_add(self, variable_name: Any, period: Any) -> Any: | ||
"""Abstract method.""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def calculate_divide(self, variable_name: Any, period: Any) -> Any: | ||
"""Abstract method.""" | ||
... | ||
|
||
@abc.abstractmethod | ||
def get_population(self, plural: Optional[Any]) -> Any: | ||
"""Abstract method.""" | ||
def get_population(self, plural: Any | None) -> Any: | ||
... | ||
|
||
|
||
class TaxBenefitSystem(Protocol): | ||
"""TaxBenefitSystem protocol.""" | ||
# Tax-Benefit systems | ||
|
||
|
||
class TaxBenefitSystem(Protocol): | ||
person_entity: Any | ||
|
||
@abc.abstractmethod | ||
def get_variable( | ||
self, | ||
variable_name: Any, | ||
check_existence: Any = ..., | ||
) -> Optional[Any]: | ||
) -> Any | None: | ||
"""Abstract method.""" | ||
|
||
|
||
class Variable(Protocol): | ||
"""Variable protocol.""" | ||
# Variables | ||
|
||
|
||
class Variable(Protocol): | ||
entity: Any | ||
|
||
|
||
class Formula(Protocol): | ||
@abc.abstractmethod | ||
def __call__( | ||
self, | ||
population: Population, | ||
instant: Instant, | ||
params: Params, | ||
) -> Array[Any]: | ||
... | ||
|
||
|
||
class Params(Protocol): | ||
@abc.abstractmethod | ||
def __call__(self, instant: Instant) -> ParameterNodeAtInstant: | ||
... |
Oops, something went wrong.