Repository: streamlit/streamlit
File: lib/streamlit/elements/widgets/time_widgets.py (Python)

Code before:
    current_value: datetime | None,
    parsed_values: _DateTimeInputValues,
    has_explicit_bounds: bool,
) -> tuple[datetime | None, bool]:
    """Validate current datetime value against min/max bounds and determine if reset is needed.

    Only validates when has_explicit_bounds is True (user provided min_value or max_value).
    This avoids incorrectly determining if reset is needed against computed default bounds.

    Parameters
    ----------
    current_value : datetime | None
        The current value of the datetime input widget.
    parsed_values : _DateTimeInputValues
        Parsed configuration containing min, max, and default value.
    has_explicit_bounds : bool
        Whether the user explicitly provided min_value or max_value. If False, validation
        is skipped to avoid resetting against computed default bounds.

    Returns
    -------
    tuple[datetime | None, bool]
        A tuple of (validated_value, was_reset) where validated_value is either the
        original value (if valid) or the default value (if reset was needed), and
        was_reset indicates whether a reset occurred.
    """
    value_needs_reset = False

    if current_value is None or not has_explicit_bounds:
        return current_value, value_needs_reset

    # Check if current value is outside bounds
    if current_value < parsed_values.min or current_value > parsed_values.max:
        value_needs_reset = True

    if not value_needs_reset:
        return current_value, value_needs_reset

    # Needs reset to the default value from parsed_values
    return parsed_values.value, True


@dataclass
class DateInputSerde:
    value: _DateInputValues

    def deserialize(self, ui_value: Any) -> DateWidgetReturn:
        return_value: Sequence[date] | None
        if ui_value is not None:
            return_value = tuple(
                datetime.strptime(v, "%Y/%m/%d").date() for v in ui_value

Review comment (suggestion):
I think this logic can be simplified

```
if current_value is None or not has_explicit_bounds:
    return current_value, False

if current_value < parsed_values.min or current_value > parsed_values.max:
    return parsed_values.value, True

return current_value, False
```




Code after:
    current_value: datetime | None,
    parsed_values: _DateTimeInputValues,
    has_explicit_bounds: bool,
) -> tuple[datetime | None, bool]:
    """Validate current datetime value against min/max bounds and determine if reset is needed.

    Only validates when has_explicit_bounds is True (user provided min_value or max_value).
    This avoids incorrectly determining if reset is needed against computed default bounds.

    Parameters
    ----------
    current_value : datetime | None
        The current value of the datetime input widget.
    parsed_values : _DateTimeInputValues
        Parsed configuration containing min, max, and default value.
    has_explicit_bounds : bool
        Whether the user explicitly provided min_value or max_value. If False, validation
        is skipped to avoid resetting against computed default bounds.

    Returns
    -------
    tuple[datetime | None, bool]
        A tuple of (validated_value, was_reset) where validated_value is either the
        original value (if valid) or the default value (if reset was needed), and
        was_reset indicates whether a reset occurred.
    """
    if current_value is None or not has_explicit_bounds:
        return current_value, False

    if current_value < parsed_values.min or current_value > parsed_values.max:
        return parsed_values.value, True

    return current_value, False


@dataclass
class DateInputSerde:
    value: _DateInputValues

    def deserialize(self, ui_value: Any) -> DateWidgetReturn:
        return_value: Sequence[date] | None
        if ui_value is not None:
            return_value = tuple(
                datetime.strptime(v, "%Y/%m/%d").date() for v in ui_value
            )
        else:
            return_value = self.value.value

        if return_value is None or len(return_value) == 0:
            return () if self.value.is_range else None