Skip to content

Commit 9a77d68

Browse files
committed
refactor: EpiRange validates input
1 parent 4c54dbb commit 9a77d68

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

epidatpy/_model.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from enum import Enum
44
from typing import (
55
Final,
6-
Generic,
76
List,
87
Mapping,
98
Optional,
@@ -22,6 +21,7 @@
2221
parse_api_date,
2322
parse_api_date_or_week,
2423
parse_api_week,
24+
parse_user_date_or_week,
2525
)
2626

2727
EpiDateLike = Union[int, str, date, Week]
@@ -30,9 +30,8 @@
3030
EpiRangeParam = Union[EpiRangeLike, Sequence[EpiRangeLike]]
3131
StringParam = Union[str, Sequence[str]]
3232
IntParam = Union[int, Sequence[int]]
33-
EpiDataResponse = TypedDict("EpiDataResponse", {"result": int, "message": str, "epidata": List})
3433
ParamType = Union[StringParam, IntParam, EpiRangeParam]
35-
EPI_RANGE_TYPE = TypeVar("EPI_RANGE_TYPE", int, date, str, Week)
34+
EpiDataResponse = TypedDict("EpiDataResponse", {"result": int, "message": str, "epidata": List})
3635
CALL_TYPE = TypeVar("CALL_TYPE")
3736

3837

@@ -66,20 +65,15 @@ def format_list(values: EpiRangeParam) -> str:
6665
return format_item(values)
6766

6867

69-
def format_epiweek(value: Union[str, int]) -> str:
70-
return Week.fromstring(str(value)).cdcformat()
71-
72-
73-
@dataclass(repr=False)
74-
class EpiRange(Generic[EPI_RANGE_TYPE]):
68+
class EpiRange:
7569
"""
7670
Range object for dates/epiweeks
7771
"""
7872

79-
start: EPI_RANGE_TYPE
80-
end: EPI_RANGE_TYPE
81-
82-
def __post_init__(self) -> None:
73+
def __init__(self, start: EpiDateLike, end: EpiDateLike) -> None:
74+
# check if types are correct
75+
self.start = parse_user_date_or_week(start)
76+
self.end = parse_user_date_or_week(end)
8377
# swap if wrong order
8478
# complicated construct for typing inference
8579
if self.end < self.start:

tests/test_model.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1+
import datetime
2+
13
from epidatpy._model import EpiRange, format_item, format_list
24

35

46
def test_epirange() -> None:
5-
r = EpiRange(3, 4)
6-
assert r.start == 3 and r.end == 4
7-
assert str(r) == "3-4"
7+
r = EpiRange(20000101, 20000102)
8+
assert r.start == datetime.date(2000, 1, 1) and r.end == datetime.date(2000, 1, 2)
9+
assert str(r) == "20000101-20000102"
810

911

1012
def test_epirange_wrong_order() -> None:
11-
r = EpiRange(4, 3)
12-
assert r.start == 3 and r.end == 4
13+
r = EpiRange(20000101, 20000102)
14+
assert r.start == datetime.date(2000, 1, 1) and r.end == datetime.date(2000, 1, 2)
1315

1416

1517
def test_format_item() -> None:
1618
assert format_item("a") == "a"
1719
assert format_item(1) == "1"
1820
assert format_item({"from": 1, "to": 3}) == "1-3"
19-
assert format_item(EpiRange(3, 5)) == "3-5"
21+
assert format_item(EpiRange(20000101, 20000102)) == "20000101-20000102"
2022

2123

2224
def test_format_list() -> None:
2325
assert format_list("a") == "a"
2426
assert format_list(1) == "1"
2527
assert format_list({"from": 1, "to": 3}) == "1-3"
26-
assert format_list(EpiRange(3, 5)) == "3-5"
28+
assert format_list(EpiRange(20000101, 20000102)) == "20000101-20000102"
2729

2830
assert format_list(["a", "b"]) == "a,b"
2931
assert format_list(("a", "b")) == "a,b"

0 commit comments

Comments
 (0)