Skip to content

Commit b773b3f

Browse files
committed
fix: validate covid_hosp_facility date arg
1 parent b461c00 commit b773b3f

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

epidatpy/_endpoints.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
Union,
1010
)
1111

12+
from epiweeks import Week
13+
1214
from ._covidcast import GeoType, TimeType, define_covidcast_fields
1315
from ._model import (
1416
CALL_TYPE,
@@ -20,9 +22,8 @@
2022
InvalidArgumentException,
2123
ParamType,
2224
StringParam,
23-
format_epiweek,
2425
)
25-
from ._parse import parse_api_week
26+
from ._parse import parse_user_date_or_week
2627

2728

2829
def get_wildcard_equivalent_dates(time_value: EpiRangeParam, time_type: Literal["day", "week"]) -> EpiRangeParam:
@@ -34,22 +35,6 @@ def get_wildcard_equivalent_dates(time_value: EpiRangeParam, time_type: Literal[
3435
return time_value
3536

3637

37-
def reformat_epirange(epirange: EpiRange, to_type: Literal["day", "week"]) -> EpiRange:
38-
"""Reformat from week to day or vice versa or noop."""
39-
if to_type == "day" and isinstance(epirange.start, (str, int)) and len(str(epirange.start)) == 6:
40-
coercion_msg = (
41-
"`collection_weeks` is in week format but `pub_covid_hosp_facility`"
42-
"expects day format; dates will be converted to day format but may not"
43-
"correspond exactly to desired time range"
44-
)
45-
warnings.warn(coercion_msg, UserWarning)
46-
epirange = EpiRange(parse_api_week(epirange.start), parse_api_week(epirange.end))
47-
elif to_type == "week" and isinstance(epirange.start, (int, str)) and len(str(epirange.start)) == 8:
48-
epirange = EpiRange(format_epiweek(epirange.start), format_epiweek(epirange.end))
49-
50-
return epirange
51-
52-
5338
class AEpiDataEndpoints(ABC, Generic[CALL_TYPE]):
5439
"""
5540
epidata endpoint list and fetcher
@@ -141,11 +126,27 @@ def pub_covid_hosp_facility(
141126

142127
# Confusingly, the endpoint expects `collection_weeks` to be in day format,
143128
# but correspond to epiweeks. Allow `collection_weeks` to be provided in
144-
# either day or week format.
145-
if isinstance(collection_weeks, EpiRange):
146-
parsed_weeks = reformat_epirange(collection_weeks, to_type="day")
147-
elif isinstance(collection_weeks, (str, int)):
148-
parsed_weeks = parse_api_week(collection_weeks)
129+
# either day or week format and convert to day format.
130+
parsed_weeks = collection_weeks
131+
if isinstance(collection_weeks, EpiRange) and isinstance(collection_weeks.start, Week):
132+
warnings.warn(
133+
"`collection_weeks` is in week format but `pub_covid_hosp_facility`"
134+
"expects day format; dates will be converted to day format but may not"
135+
"correspond exactly to desired time range",
136+
UserWarning,
137+
)
138+
parsed_weeks = EpiRange(
139+
parse_user_date_or_week(collection_weeks.start, "day"),
140+
parse_user_date_or_week(collection_weeks.end, "day"),
141+
)
142+
elif isinstance(collection_weeks, (str, int)) and len(str(collection_weeks)) == 6:
143+
warnings.warn(
144+
"`collection_weeks` is in week format but `pub_covid_hosp_facility`"
145+
"expects day format; dates will be converted to day format but may not"
146+
"correspond exactly to desired time range",
147+
UserWarning,
148+
)
149+
parsed_weeks = parse_user_date_or_week(collection_weeks, "day")
149150

150151
fields_string = [
151152
"hospital_pk",

0 commit comments

Comments
 (0)