Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Sep 24, 2024
1 parent 4f81fb7 commit b27db7c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 144 deletions.
2 changes: 1 addition & 1 deletion openfisca_core/periods/date_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DateUnit(StrEnum, metaclass=DateUnitMeta):
{<DateUnit.DAY: 'day'>: 'day'}
>>> list(DateUnit)
[<DateUnit.WEEKDAY: 'weekday'>, <DateUnit.WEEK: 'week'>, <DateUnit.DAY: 'day'>, ...]
[<DateUnit.WEEKDAY: 'weekday'>, <DateUnit.WEEK: 'week'>, <DateUnit.D...
>>> len(DateUnit)
6
Expand Down
16 changes: 6 additions & 10 deletions openfisca_core/periods/helpers.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
from __future__ import annotations

from typing import NoReturn, Optional, overload
from typing import NoReturn, overload

import datetime
import os

import pendulum

from . import _parsers, config
from . import types as t
from . import _parsers, config, types as t
from ._errors import ParserError
from .date_unit import DateUnit
from .instant_ import Instant
from .period_ import Period


@overload
def instant(instant: None) -> None:
...
def instant(instant: None) -> None: ...


@overload
def instant(instant: int | t.Period | t.Instant | datetime.date) -> t.Instant:
...
def instant(instant: int | t.Period | t.Instant | datetime.date) -> t.Instant: ...


@overload
def instant(instant: str | list[int] | tuple[int, ...]) -> NoReturn | t.Instant:
...
def instant(instant: str | list[int] | tuple[int, ...]) -> t.Instant: ...


def instant(instant: object) -> None | t.Instant:
Expand Down Expand Up @@ -100,7 +96,7 @@ def instant(instant: object) -> None | t.Instant:
return Instant(result)


def instant_date(instant: Optional[t.Instant]) -> Optional[datetime.date]:
def instant_date(instant: None | t.Instant) -> None | datetime.date:
"""Returns the date representation of an :class:`.Instant`.
Args:
Expand Down
3 changes: 1 addition & 2 deletions openfisca_core/periods/instant_.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import pendulum

from . import config
from . import types as t
from . import config, types as t
from .date_unit import DateUnit


Expand Down
9 changes: 4 additions & 5 deletions openfisca_core/periods/period_.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import pendulum

from . import helpers
from . import types as t
from . import helpers, types as t
from .date_unit import DateUnit
from .instant_ import Instant

Expand Down Expand Up @@ -138,8 +137,8 @@ def __str__(self) -> t.PeriodStr:
if month == 1:
# civil year starting from january
return t.PeriodStr(str(f_year))
# rolling year
return t.PeriodStr(f"{DateUnit.YEAR}:{f_year}-{month:02d}")
# rolling year
return t.PeriodStr(f"{DateUnit.YEAR}:{f_year}-{month:02d}")

# simple month
if unit == DateUnit.MONTH and size == 1:
Expand All @@ -152,7 +151,7 @@ def __str__(self) -> t.PeriodStr:
if unit == DateUnit.DAY:
if size == 1:
return t.PeriodStr(f"{f_year}-{month:02d}-{day:02d}")
return t.PeriodStr(f"{unit}:{f_year}-{month:02d}-{day:02d}:{size}")
return t.PeriodStr(f"{unit}:{f_year}-{month:02d}-{day:02d}:{size}")

# 1 week
if unit == DateUnit.WEEK and size == 1:
Expand Down
14 changes: 7 additions & 7 deletions openfisca_core/periods/tests/helpers/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
@pytest.mark.parametrize(
("arg", "expected"),
[
["eternity", Period((DateUnit.ETERNITY, Instant((1, 1, 1)), 0))],
["ETERNITY", Period((DateUnit.ETERNITY, Instant((1, 1, 1)), 0))],
[
("eternity", Period((DateUnit.ETERNITY, Instant((1, 1, 1)), 0))),
("ETERNITY", Period((DateUnit.ETERNITY, Instant((1, 1, 1)), 0))),
(
DateUnit.ETERNITY,
Period((DateUnit.ETERNITY, Instant((1, 1, 1)), 0)),
],
[datetime.date(1, 1, 1), Period((DateUnit.DAY, Instant((1, 1, 1)), 1))],
[Instant((1, 1, 1)), Period((DateUnit.DAY, Instant((1, 1, 1)), 1))],
[
),
(datetime.date(1, 1, 1), Period((DateUnit.DAY, Instant((1, 1, 1)), 1))),
(Instant((1, 1, 1)), Period((DateUnit.DAY, Instant((1, 1, 1)), 1))),
(
Period((DateUnit.DAY, Instant((1, 1, 1)), 365)),
Period((DateUnit.DAY, Instant((1, 1, 1)), 365)),
),
Expand Down
68 changes: 1 addition & 67 deletions openfisca_core/periods/types.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,3 @@
from __future__ import annotations

from typing import NewType, Protocol

import pendulum

from openfisca_core import types as t

# New types.

#: For example "2000-01".
InstantStr = NewType("InstantStr", str)

#: For example "1:2000-01-01:day".
PeriodStr = NewType("PeriodStr", str)


# Periods


class DateUnit(t.DateUnit, Protocol):
...


class Instant(t.Instant, Protocol):
@property
def year(self) -> int:
...

@property
def month(self) -> int:
...

@property
def day(self) -> int:
...

@property
def date(self) -> pendulum.Date:
...

def __lt__(self, other: object, /) -> bool:
...

def __le__(self, other: object, /) -> bool:
...

def offset(self, offset: str | int, unit: DateUnit) -> Instant | None:
...


class Period(t.Period, Protocol):
@property
def size(self) -> int:
...

@property
def start(self) -> Instant:
...

@property
def stop(self) -> Instant:
...

def offset(self, offset: str | int, unit: DateUnit | None = None) -> Period:
...

from openfisca_core.types import DateUnit, Instant, InstantStr, Period, PeriodStr

__all__ = ["DateUnit", "Instant", "Period", "InstantStr", "PeriodStr"]
93 changes: 47 additions & 46 deletions openfisca_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@
from collections.abc import Iterable, Sequence, Sized
from numpy.typing import NDArray
from typing import Any, TypeVar, Union
from typing_extensions import Protocol, TypeAlias
from typing_extensions import NewType, Protocol, TypeAlias

import numpy
import pendulum

N = TypeVar("N", bound=numpy.generic, covariant=True)
_N = TypeVar("_N", bound=numpy.generic, covariant=True)

#: Type representing an numpy array.
Array: TypeAlias = NDArray[N]
Array: TypeAlias = NDArray[_N]

L = TypeVar("L")
_L = TypeVar("_L")

#: Type representing an array-like object.
ArrayLike: TypeAlias = Sequence[L]
ArrayLike: TypeAlias = Sequence[_L]

#: Type variable representing an error.
E = TypeVar("E", covariant=True)
_E = TypeVar("_E", covariant=True)

#: Type variable representing a value.
A = TypeVar("A", covariant=True)
_A = TypeVar("_A", covariant=True)

#: Generic type vars.
T_cov = TypeVar("T_cov", covariant=True)
T_con = TypeVar("T_con", contravariant=True)
_T_cov = TypeVar("_T_cov", covariant=True)
_T_con = TypeVar("_T_con", contravariant=True)


# Entities
Expand All @@ -35,12 +36,8 @@ class CoreEntity(Protocol):
key: Any
plural: Any

@abc.abstractmethod
def check_role_validity(self, role: Any) -> None: ...

@abc.abstractmethod
def check_variable_defined_for_entity(self, variable_name: Any) -> None: ...

def get_variable(
self,
variable_name: Any,
Expand All @@ -67,44 +64,58 @@ def key(self) -> str: ...


class Holder(Protocol):
@abc.abstractmethod
def clone(self, population: Any) -> Holder: ...

@abc.abstractmethod
def get_memory_usage(self) -> Any: ...


# Parameters


class ParameterNodeAtInstant(Protocol):
...
class ParameterNodeAtInstant(Protocol): ...


# Periods

#: For example "2000-01".
InstantStr = NewType("InstantStr", str)

class Indexable(Protocol[T_cov]):
def __getitem__(self, index: int, /) -> T_cov:
...
#: For example "1:2000-01-01:day".
PeriodStr = NewType("PeriodStr", str)


class DateUnit(Protocol):
def __contains__(self, other: object, /) -> bool:
...
class Indexable(Protocol[_T_cov]):
def __getitem__(self, index: int, /) -> _T_cov: ...

def upper(self) -> str:
...

class DateUnit(Protocol):
def __contains__(self, other: object, /) -> bool: ...
def upper(self) -> str: ...


class Instant(Indexable[int], Iterable[int], Sized, Protocol):
...
@property
def year(self, /) -> int: ...
@property
def month(self, /) -> int: ...
@property
def day(self, /) -> int: ...
@property
def date(self, /) -> pendulum.Date: ...
def __lt__(self, other: object, /) -> bool: ...
def __le__(self, other: object, /) -> bool: ...
def offset(self, offset: str | int, unit: DateUnit, /) -> None | Instant: ...


class Period(Indexable[Union[DateUnit, Instant, int]], Protocol):
@property
def unit(self) -> DateUnit:
...
def unit(self) -> DateUnit: ...
@property
def start(self) -> Instant: ...
@property
def size(self) -> int: ...
@property
def stop(self) -> Instant: ...
def offset(self, offset: str | int, unit: None | DateUnit = None) -> Period: ...


# Populations
Expand All @@ -113,25 +124,17 @@ def unit(self) -> DateUnit:
class Population(Protocol):
entity: Any

def get_holder(self, variable_name: Any) -> Any:
...
def get_holder(self, variable_name: Any) -> Any: ...


# Simulations


class Simulation(Protocol):
def calculate(self, variable_name: Any, period: Any) -> Any:
...

def calculate_add(self, variable_name: Any, period: Any) -> Any:
...

def calculate_divide(self, variable_name: Any, period: Any) -> Any:
...

def get_population(self, plural: Any | None) -> Any:
...
def calculate(self, variable_name: Any, period: Any) -> Any: ...
def calculate_add(self, variable_name: Any, period: Any) -> Any: ...
def calculate_divide(self, variable_name: Any, period: Any) -> Any: ...
def get_population(self, plural: Any | None) -> Any: ...


# Tax-Benefit systems
Expand All @@ -144,8 +147,7 @@ def get_variable(
self,
variable_name: Any,
check_existence: Any = ...,
) -> Any | None:
"""Abstract method."""
) -> Any | None: ...


# Variables
Expand All @@ -165,5 +167,4 @@ def __call__(


class Params(Protocol):
def __call__(self, instant: Instant) -> ParameterNodeAtInstant:
...
def __call__(self, instant: Instant) -> ParameterNodeAtInstant: ...
9 changes: 4 additions & 5 deletions openfisca_core/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import numpy
import sortedcontainers

from openfisca_core import periods, tools
from openfisca_core import types as t
from openfisca_core import periods, tools, types as t
from openfisca_core.entities import Entity, GroupEntity
from openfisca_core.indexed_enums import Enum, EnumArray
from openfisca_core.periods import DateUnit, Period
Expand Down Expand Up @@ -384,8 +383,8 @@ def get_introspection_data(cls):

def get_formula(
self,
period: Union[t.Instant, t.Period, str, int] = None,
) -> Optional[t.Formula]:
period: None | t.Instant | t.Period | str | int = None,
) -> None | t.Formula:
"""Returns the formula to compute the variable at the given period.
If no period is given and the variable has several formulas, the method
Expand All @@ -398,7 +397,7 @@ def get_formula(
Formula used to compute the variable.
"""
instant: Optional[t.Instant]
instant: None | t.Instant

if not self.formulas:
return None
Expand Down
Loading

0 comments on commit b27db7c

Please sign in to comment.