dash icon indicating copy to clipboard operation
dash copied to clipboard

component typing id as dict and datetime for Date pickers in Dash 3.0rc1

Open tlauli opened this issue 11 months ago • 5 comments

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 as typing.Optional[str], omitting dict ids
  • dates in datepickers are typed as typing.Optional[str], but we use datetime.dates without problem, not sure if dates working is intended or just a coincidence though There are possibly others, this is what I found in our codebase.

tlauli avatar Jan 29 '25 15:01 tlauli

@T4rk1n Hi, I see the the types were expanded to allow datetime.datetime for dates in datepicker, but it should be datetime.date.

tlauli avatar Mar 18 '25 11:03 tlauli

@T4rk1n Hi, I see the the types were expanded to allow datetime.datetime for dates in datepicker, but it should be datetime.date.

My bad, I tested with datetime.datetime.now() and assumed it was the same, it should be union of both.

T4rk1n avatar Mar 18 '25 12:03 T4rk1n

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

tlauli avatar Mar 18 '25 13:03 tlauli

The base class is datetime.date, it handle both.

T4rk1n avatar Mar 18 '25 13:03 T4rk1n

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.

tlauli avatar Mar 18 '25 13:03 tlauli