From 555a7b612417eca2422cb2c881a9eac4d6d8b36b Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Fri, 16 Feb 2024 17:48:38 -0600 Subject: [PATCH 1/2] Don't error on malformed date input --- shiny/input_handler.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/shiny/input_handler.py b/shiny/input_handler.py index 5a45bba5c..dfb9b49b1 100644 --- a/shiny/input_handler.py +++ b/shiny/input_handler.py @@ -99,13 +99,24 @@ def _(value, name, session): @input_handlers.add("shiny.date") def _( value: str | list[str], name: ResolvedId, session: Session -) -> date | tuple[date, date]: +) -> date | None | tuple[date | None, date | None]: + if isinstance(value, str): + return _safe_strptime_date(value) + else: + return ( + _safe_strptime_date(value[0]), + _safe_strptime_date(value[1]), + ) + + +def _safe_strptime_date(value: str | None) -> date | None: + if value is None: + return None + try: return datetime.strptime(value, "%Y-%m-%d").date() - return ( - datetime.strptime(value[0], "%Y-%m-%d").date(), - datetime.strptime(value[1], "%Y-%m-%d").date(), - ) + except ValueError: + return None @input_handlers.add("shiny.datetime") From eff03b12a9bcdc096b34df7d94ed12cf5bd0f644 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Mon, 26 Feb 2024 17:09:18 -0600 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d1f4ca5e..4fa3f8b78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Fixed several issues with `page_navbar()` styling. (#1124) * Fixed `Renderer.output_id` to not contain the module namespace prefix, only the output id. (#1130) * Fixed gap-driven spacing between children in fillable `nav_panel()` containers. (#1152) +* Fixed #1138: An empty value in a date or date range input would cause an error; now it is treated as `None`. (#1139) ### Other changes