Skip to content

Commit

Permalink
Merge branch 'ls-mypy'
Browse files Browse the repository at this point in the history
  • Loading branch information
lsbardel committed Sep 29, 2023
2 parents 6c54f14 + a7b63c5 commit a510f2b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
45 changes: 24 additions & 21 deletions ccy/dates/period.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from __future__ import annotations

from typing import Any


def period(pstr: str = "") -> Period:
"""Create a period object from a period string"""
return Period.make(pstr)


def find_first_of(st: str, possible: str) -> int:
possible = tuple(possible)
lowi = -1
for p in possible:
for p in tuple(possible):
i = st.find(p)
if i != -1 and (i < lowi or lowi == -1):
lowi = i
Expand All @@ -30,11 +31,13 @@ def __init__(self, months: int = 0, days: int = 0) -> None:
self._days = days

@classmethod
def make(cls, pstr: str = "") -> Period:
if isinstance(pstr, cls):
return pstr
def make(cls, data: Any) -> Period:
if isinstance(data, cls):
return data
elif isinstance(data, str):
return cls().add_tenure(data)
else:
return cls().add_tenure(pstr)
raise TypeError("Cannot convert %s to Period" % data)

def isempty(self) -> bool:
return self._months == 0 and self._days == 0
Expand Down Expand Up @@ -68,7 +71,7 @@ def days(self) -> int:
return safemod(self._days, 7)

@property
def totaldays(self):
def totaldays(self) -> int:
return 30 * self._months + self._days

def __repr__(self) -> str:
Expand Down Expand Up @@ -96,7 +99,7 @@ def components(self) -> str:
p = "%s%sD" % (p, abs(d))
return "-" + p if neg else p

def simple(self):
def simple(self) -> str:
"""A string representation with only one period delimiter."""
if self._days:
return "%sD" % self.totaldays
Expand Down Expand Up @@ -138,31 +141,31 @@ def add_tenure(self, pstr: str) -> Period:
st = st[ip:]
return self

def __add__(self, other):
other = self.make(other)
return self.__class__(self._months + other._months, self._days + other._days)
def __add__(self, other: Any) -> Period:
p = self.make(other)
return self.__class__(self._months + p._months, self._days + p._days)

def __radd__(self, other):
def __radd__(self, other: Any) -> Period:
return self + other

def __sub__(self, other):
other = self.make(other)
return self.__class__(self._months - other._months, self._days - other._days)
def __sub__(self, other: Any) -> Period:
p = self.make(other)
return self.__class__(self._months - p._months, self._days - p._days)

def __rsub__(self, other):
def __rsub__(self, other: Any) -> Period:
return self.make(other) - self

def __gt__(self, other):
def __gt__(self, other: Any) -> bool:
return self.totaldays > self.make(other).totaldays

def __lt__(self, other):
def __lt__(self, other: Any) -> bool:
return self.totaldays < self.make(other).totaldays

def __ge__(self, other):
def __ge__(self, other: Any) -> bool:
return self.totaldays >= self.make(other).totaldays

def __le__(self, other):
def __le__(self, other: Any) -> bool:
return self.totaldays <= self.make(other).totaldays

def __eq__(self, other):
def __eq__(self, other: Any) -> bool:
return self.totaldays == self.make(other).totaldays
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,3 @@ ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "ccy.tradingcentres.*"
ignore_errors = true

[[tool.mypy.overrides]]
module = "ccy.dates.period.*"
ignore_errors = true

0 comments on commit a510f2b

Please sign in to comment.