Skip to content

[fix] Normalize all date_input session state values to date#14123

Merged
lukasmasuch merged 1 commit intodevelopfrom
lukasmasuch/fix-date-input-datetime
Feb 26, 2026
Merged

[fix] Normalize all date_input session state values to date#14123
lukasmasuch merged 1 commit intodevelopfrom
lukasmasuch/fix-date-input-datetime

Conversation

@lukasmasuch
Copy link
Copy Markdown
Collaborator

Describe your changes

Fixes TypeError when a datetime value from session_state is compared with date-type min_value/max_value bounds in st.date_input.

  • Added _to_date() helper to normalize datetime to date for safe comparison
  • Updated _validate_date_value() to handle datetime values from session_state

GitHub Issue Link

Fixes #14109

Testing Plan

  • Unit Tests (Python) - Added regression test in lib/tests/streamlit/elements/date_input_test.py

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
Copilot AI review requested due to automatic review settings February 25, 2026 09:27
@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Feb 25, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@lukasmasuch lukasmasuch added change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Feb 25, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 25, 2026

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-14123/streamlit-1.54.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-14123.streamlit.app (☁️ Deploy here if not accessible)

@lukasmasuch lukasmasuch changed the title [fix] date_input TypeError with datetime session_state values [fix] date_input TypeError with datetime session_state values Feb 25, 2026
@lukasmasuch lukasmasuch changed the title [fix] date_input TypeError with datetime session_state values [fix] Normalize all date_input session state values to date Feb 25, 2026
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 datetime to date for 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 + date bounds 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_date helper duplicates the existing _convert_datelike_to_date logic in this module (which already converts datetime to date). Reusing _convert_datelike_to_date would 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_value now normalizes datetime values only for bound comparisons, but it can still return a datetime unchanged when the current value comes from session_state. That’s inconsistent with st.date_input’s documented behavior of ignoring the time component and returning a datetime.date. Consider normalizing current_value (and range tuple entries) to date before 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 dt is redundant since datetime is 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-level datetime import instead.
    from datetime import datetime as dt

lib/tests/streamlit/elements/date_input_test.py:1069

  • The test asserts that st.date_input preserves a datetime value from st.session_state, but the public API docstring says date_input ignores any time component and returns datetime.date. This assertion also diverges from existing tests that expect a date when a datetime is provided as value. Consider asserting the returned value is the corresponding date (and/or that no exception is raised) rather than locking in a datetime return 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)

@lukasmasuch lukasmasuch merged commit 666c539 into develop Feb 26, 2026
81 of 84 checks passed
@lukasmasuch lukasmasuch deleted the lukasmasuch/fix-date-input-datetime branch February 26, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Date input fails date value validation

3 participants