[fix] Normalize all date_input session state values to date#14123
[fix] Normalize all date_input session state values to date#14123lukasmasuch merged 1 commit intodevelopfrom
date_input session state values to date#14123Conversation
When a datetime value is stored in session_state and used with st.date_input that has date-type min/max bounds, comparing datetime with date directly raises TypeError. This adds a helper to normalize datetime to date for safe comparison. Fixes #14109
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
date_input TypeError with datetime session_state values
date_input TypeError with datetime session_state valuesdate_input session state values to date
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
There was a problem hiding this comment.
Pull request overview
Fixes a regression in st.date_input where datetime values coming from session_state could raise a TypeError when validated against date-typed min_value/max_value bounds.
Changes:
- Added a helper to normalize
datetimetodatefor safe min/max comparisons during validation. - Updated date-input validation to use normalized values in both single-date and range modes.
- Added a regression test covering the
datetime-in-session_state+datebounds scenario.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/streamlit/elements/widgets/time_widgets.py |
Normalizes datetime values to date for min/max comparisons in st.date_input validation. |
lib/tests/streamlit/elements/date_input_test.py |
Adds regression coverage for the datetime session_state value vs date bounds validation case. |
Comments suppressed due to low confidence (4)
lib/streamlit/elements/widgets/time_widgets.py:498
- This new
_to_datehelper duplicates the existing_convert_datelike_to_datelogic in this module (which already convertsdatetimetodate). Reusing_convert_datelike_to_datewould reduce duplication and keep date normalization behavior centralized.
def _to_date(v: date) -> date:
"""Convert datetime to date for comparison.
st.date_input can receive datetime values from session_state. Since datetime
is a subclass of date, isinstance(v, date) returns True, but datetime and date
objects cannot be directly compared with < or >. This helper normalizes the
value for safe comparison with date bounds.
"""
return v.date() if isinstance(v, datetime) else v
lib/streamlit/elements/widgets/time_widgets.py:557
_validate_date_valuenow normalizesdatetimevalues only for bound comparisons, but it can still return adatetimeunchanged when the current value comes fromsession_state. That’s inconsistent withst.date_input’s documented behavior of ignoring the time component and returning adatetime.date. Consider normalizingcurrent_value(and range tuple entries) todatebefore returning/updating session_state so the widget’s return type matches its API contract.
elif not parsed_values.is_range and isinstance(current_value, date):
# For single date mode. Use _to_date to handle datetime values from session_state.
if (
_to_date(current_value) < parsed_values.min
or _to_date(current_value) > parsed_values.max
):
value_needs_reset = True
lib/tests/streamlit/elements/date_input_test.py:1046
- The in-test import
from datetime import datetime as dtis redundant sincedatetimeis already imported at module scope, and it goes against the test-file pattern of keeping imports at the top. Consider removing this local import and using the existing module-leveldatetimeimport instead.
from datetime import datetime as dt
lib/tests/streamlit/elements/date_input_test.py:1069
- The test asserts that
st.date_inputpreserves adatetimevalue fromst.session_state, but the public API docstring says date_input ignores any time component and returnsdatetime.date. This assertion also diverges from existing tests that expect adatewhen adatetimeis provided asvalue. Consider asserting the returned value is the correspondingdate(and/or that no exception is raised) rather than locking in adatetimereturn type here.
assert not at.exception
# The widget preserves the datetime type from session_state
assert at.date_input[0].value == dt(2025, 6, 15, 12, 30, 0)
Describe your changes
Fixes TypeError when a
datetimevalue fromsession_stateis compared withdate-typemin_value/max_valuebounds inst.date_input._to_date()helper to normalize datetime to date for safe comparison_validate_date_value()to handle datetime values from session_stateGitHub Issue Link
Fixes #14109
Testing Plan
lib/tests/streamlit/elements/date_input_test.py