Skip to content

Commit

Permalink
fix(periods): dunder method types
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Sep 19, 2024
1 parent 1392bb8 commit e4326f6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
5 changes: 5 additions & 0 deletions openfisca_core/periods/date_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ class DateUnit(StrEnum, metaclass=DateUnitMeta):
"""

def __contains__(self, other: object) -> bool:
if isinstance(other, str):
return super().__contains__(other)
return NotImplemented

WEEKDAY = "weekday"
WEEK = "week"
DAY = "day"
Expand Down
10 changes: 10 additions & 0 deletions openfisca_core/periods/instant_.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ def __str__(self) -> t.InstantStr:

return instant_str

def __lt__(self, other: object) -> bool:
if isinstance(other, Instant):
return super().__lt__(other)
return NotImplemented

def __le__(self, other: object) -> bool:
if isinstance(other, Instant):
return super().__le__(other)
return NotImplemented

@property
def date(self) -> Date:
instant_date = config.date_by_instant_cache.get(self)
Expand Down
11 changes: 9 additions & 2 deletions openfisca_core/periods/period_.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def size_in_weekdays(self) -> int:
if self.unit == DateUnit.YEAR:
return self.size_in_weeks * 7

if self.unit in DateUnit.MONTH:
if DateUnit.MONTH in self.unit:
last = self.start.offset(self.size, self.unit)
if last is None:
raise NotImplementedError
Expand Down Expand Up @@ -685,10 +685,17 @@ def offset(self, offset: str | int, unit: t.DateUnit | None = None) -> t.Period:
"""

start: None | t.Instant = self[1].offset(
offset, self[0] if unit is None else unit
)

if start is None:
raise NotImplementedError

return self.__class__(
(
self[0],
self[1].offset(offset, self[0] if unit is None else unit),
start,
self[2],
)
)
Expand Down
6 changes: 6 additions & 0 deletions openfisca_core/periods/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def day(self) -> int:
def date(self) -> Date:
...

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

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

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

Expand Down
10 changes: 3 additions & 7 deletions openfisca_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,14 @@ class ParameterNodeAtInstant(Protocol):
# Periods


class Container(Protocol[T_con]):
def __contains__(self, item: T_con, /) -> bool:
...


class Indexable(Protocol[T_cov]):
def __getitem__(self, index: int, /) -> T_cov:
...


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


class Instant(Indexable[int], Iterable[int], Sized, Protocol):
Expand Down

0 comments on commit e4326f6

Please sign in to comment.