Thanks to visit codestin.com
Credit goes to github.com

Skip to content

date_input should be able to specify return type #9477

@BobVitorBob

Description

@BobVitorBob

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

date_input currently accepts a single date or a tuple of dates for value. Then, it returns the same number of values. However, as currently defined, type checkers think its possible to receive one date and return two, and vice-versa.

Why?

It requires an unnecessary cast or assert every time that date_input is used. This is the same issue that was fixed for other inputs in v1.38.

How?

current definition (with DateValue and DateWidgetReturn being explicitly defined):

def date_input(
    self,
    label: str,
    value: Union[SingleDateValue, Sequence[SingleDateValue]]
    | Literal["today", "default_value_today"]
    | None = "default_value_today",
    min_value: SingleDateValue = None,
    max_value: SingleDateValue = None,
    key: Key | None = None,
    help: str | None = None,
    on_change: WidgetCallback | None = None,
    args: WidgetArgs | None = None,
    kwargs: WidgetKwargs | None = None,
    *,  # keyword-only arguments:
    format: str = "YYYY/MM/DD",
    disabled: bool = False,
    label_visibility: LabelVisibility = "visible",
) -> Union[date, Tuple[()], Tuple[date], Tuple[date, date], None]:

possible solution:

@overload
def date_input(
    self,
    label: str,
    value: SingleDateValue
    | Literal["today", "default_value_today"]
    | None = "default_value_today",
    min_value: SingleDateValue = None,
    max_value: SingleDateValue = None,
    key: Key | None = None,
    help: str | None = None,
    on_change: WidgetCallback | None = None,
    args: WidgetArgs | None = None,
    kwargs: WidgetKwargs | None = None,
    *,  # keyword-only arguments:
    format: str = "YYYY/MM/DD",
    disabled: bool = False,
    label_visibility: LabelVisibility = "visible",
) -> date | None:

@overload
def date_input(
    self,
    label: str,
    value: Tuple[SingleDateValue, SingleDateValue]
    | Literal["today", "default_value_today"]
    | None = "default_value_today",
    min_value: SingleDateValue = None,
    max_value: SingleDateValue = None,
    key: Key | None = None,
    help: str | None = None,
    on_change: WidgetCallback | None = None,
    args: WidgetArgs | None = None,
    kwargs: WidgetKwargs | None = None,
    *,  # keyword-only arguments:
    format: str = "YYYY/MM/DD",
    disabled: bool = False,
    label_visibility: LabelVisibility = "visible",
) -> Union[Tuple[()], Tuple[date], Tuple[date, date], None]:

def date_input(
    self,
    label: str,
    value: Union[SingleDateValue, Tuple[SingleDateValue, SingleDateValue]]
    | Literal["today", "default_value_today"]
    | None = "default_value_today",
    min_value: SingleDateValue = None,
    max_value: SingleDateValue = None,
    key: Key | None = None,
    help: str | None = None,
    on_change: WidgetCallback | None = None,
    args: WidgetArgs | None = None,
    kwargs: WidgetKwargs | None = None,
    *,  # keyword-only arguments:
    format: str = "YYYY/MM/DD",
    disabled: bool = False,
    label_visibility: LabelVisibility = "visible",
) -> Union[date, Tuple[()], Tuple[date], Tuple[date, date], None]:

Additional Context

No response

Metadata

Metadata

Assignees

Labels

feature:st.date_inputRelated to the `st.date_input` widgetgood first issueGood for newcomers to the codebasetype:enhancementRequests for feature enhancements or new features

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions