How do I deal with an API that returns Fuzzy date with missing values? My initial plan was to check year, month, and day and then pass them into datetime.date but I just learnt that date requires all 3.
# API response
"startDate": {
"year": 2023,
"month": 9,
"day": 29
},
"endDate": {
"year": 2024,
"month": 3,
"day": null
},
Any of these can be None or all of these can be None.
In my head I planned to achieve something like this:
# Doesn't actually work, because date requires all 3
def _fuzzy_date_handler(year, month, day):
if year and month and day:
return date(year=year, month=month, day=day)
elif year and month:
return date(year=year, month=month)
elif year:
return date(year=year)
else:
return None # all 3 are null
Easiest solution I can think of is to use a very simple dataclass instead of a proper date library:
@dataclass
class FuzzyDate:
year: int | None
month: int | None
day: int | None
Is there a library that handles such cases already? If not, how should I go about dealing with this?