Skip to content

Commit b8f24dd

Browse files
committed
refactor: EpiRange validates input
1 parent 617b1ff commit b8f24dd

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

epidatpy/_model.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
Any,
66
Dict,
77
Final,
8-
Generic,
98
List,
109
Mapping,
1110
Optional,
@@ -26,6 +25,7 @@
2625
parse_api_date,
2726
parse_api_date_or_week,
2827
parse_api_week,
28+
parse_user_date_or_week,
2929
)
3030

3131
EpiDateLike = Union[int, str, date, Week]
@@ -34,9 +34,8 @@
3434
EpiRangeParam = Union[EpiRangeLike, Sequence[EpiRangeLike]]
3535
StringParam = Union[str, Sequence[str]]
3636
IntParam = Union[int, Sequence[int]]
37-
EpiDataResponse = TypedDict("EpiDataResponse", {"result": int, "message": str, "epidata": List})
3837
ParamType = Union[StringParam, IntParam, EpiRangeParam]
39-
EPI_RANGE_TYPE = TypeVar("EPI_RANGE_TYPE", int, date, str, Week)
38+
EpiDataResponse = TypedDict("EpiDataResponse", {"result": int, "message": str, "epidata": List})
4039
CALL_TYPE = TypeVar("CALL_TYPE")
4140

4241

@@ -70,20 +69,15 @@ def format_list(values: EpiRangeParam) -> str:
7069
return format_item(values)
7170

7271

73-
def format_epiweek(value: Union[str, int]) -> str:
74-
return Week.fromstring(str(value)).cdcformat()
75-
76-
77-
@dataclass(repr=False)
78-
class EpiRange(Generic[EPI_RANGE_TYPE]):
72+
class EpiRange:
7973
"""
8074
Range object for dates/epiweeks
8175
"""
8276

83-
start: EPI_RANGE_TYPE
84-
end: EPI_RANGE_TYPE
85-
86-
def __post_init__(self) -> None:
77+
def __init__(self, start: EpiDateLike, end: EpiDateLike) -> None:
78+
# check if types are correct
79+
self.start = parse_user_date_or_week(start)
80+
self.end = parse_user_date_or_week(end)
8781
# swap if wrong order
8882
# complicated construct for typing inference
8983
if self.end < self.start:

tests/test_model.py

+9-7
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)