component typing id as dict and datetime for Date pickers in Dash 3.0rc1
Hi, I tried to update to dash 3.0rc1, and what I noticed is that some typehints are too strict. What I found:
-
ids are typed astyping.Optional[str], omitting dict ids - dates in datepickers are typed as
typing.Optional[str], but we usedatetime.dates without problem, not sure ifdates working is intended or just a coincidence though There are possibly others, this is what I found in our codebase.
@T4rk1n Hi, I see the the types were expanded to allow datetime.datetime for dates in datepicker, but it should be datetime.date.
@T4rk1n Hi, I see the the types were expanded to allow
datetime.datetimefor dates in datepicker, but it should bedatetime.date.
My bad, I tested with datetime.datetime.now() and assumed it was the same, it should be union of both.
I think it would be better to allow datetime.date only. Otherwise it is possible to init the datepicker with something that has time different from midnight (this still correctly sets the date in ui), and if the user does not change the date, then the time is read back into callbacks, which can be surprising in my opinion. In the attached example, 2025-03-18T14:17:32.244956 is shown, instead of just the date.
layout = html.Div(
[
dcc.DatePickerSingle(id="datepicker", date=datetime.datetime.now()),
html.Div(id="output"),
],
)
@callback(
Output("output", "children"),
Input("datepicker", "date"),
)
def xxx(date: str | None) -> str:
return date
The base class is datetime.date, it handle both.
Oh, of course. Then both date and datetime is fine. It could maybe be nice to throw away the time part in the component if present, but that is out of the scope of this issue.