This is an automated email from the ASF dual-hosted git repository. villebro pushed a commit to branch 7.0 in repository https://gitbox.apache.org/repos/asf/superset.git
commit 6199472df19798de0c68f266f3afb63262cf2d9b Author: Sepuri Sai Krishna <[email protected]> AuthorDate: Wed Nov 4 17:10:18 2026 +0530 fix(date-parser): stop HOLIDAY returning 500 for its country argument (#45260) Co-authored-by: Enzo Martellucci <[email protected]> Co-authored-by: Evan Rusackas <[email protected]> (cherry picked from commit 6f9138802f4ad2a6d41c36f25915c6fb0fa46b42) --- .../components/DateFunctionTooltip.tsx | 4 ++- superset/translations/messages.pot | 4 +++ superset/utils/date_parser.py | 31 +++++++++++++---- tests/unit_tests/utils/date_parser_tests.py | 39 ++++++++++++++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/superset-frontend/src/explore/components/controls/DateFilterControl/components/DateFunctionTooltip.tsx b/superset-frontend/src/explore/components/controls/DateFilterControl/components/DateFunctionTooltip.tsx index e8a77b1004e..cfbbcf6b7eb 100644 --- a/superset-frontend/src/explore/components/controls/DateFilterControl/components/DateFunctionTooltip.tsx +++ b/superset-frontend/src/explore/components/controls/DateFilterControl/components/DateFunctionTooltip.tsx @@ -90,7 +90,8 @@ dateunit = (year | month | week)`}</code> <pre> <code>{`holiday([string]) holiday([holiday string], [datetime]) -holiday([holiday string], [datetime], [country name])`}</code> +holiday([holiday string], [country code]) +holiday([holiday string], [datetime], [country code])`}</code> </pre> <h4>{t('Example')}</h4> <pre> @@ -98,6 +99,7 @@ holiday([holiday string], [datetime], [country name])`}</code> holiday("christmas", datetime("2019")) holiday("christmas", dateadd(datetime("2019"), 1, year)) holiday("christmas", datetime("2 years ago")) +holiday("Boxing Day", "UK") holiday("Easter Monday", datetime("2019"), "UK")`}</code> </pre> </div> diff --git a/superset/translations/messages.pot b/superset/translations/messages.pot index 294f9459745..571e89fdcbe 100644 --- a/superset/translations/messages.pot +++ b/superset/translations/messages.pot @@ -16021,6 +16021,10 @@ msgstr "" msgid "Unable to fetch semantic views. Check the layer configuration." msgstr "" +#, python-format +msgid "Unable to find such a country: [%(country)s]" +msgstr "" + #, python-format msgid "Unable to find such a holiday: [%(holiday)s]" msgstr "" diff --git a/superset/utils/date_parser.py b/superset/utils/date_parser.py index bca761295eb..02efa19fffd 100644 --- a/superset/utils/date_parser.py +++ b/superset/utils/date_parser.py @@ -905,15 +905,32 @@ class EvalHolidayFunc: # pylint: disable=too-few-public-methods def eval(self) -> datetime: holiday = self.value[0].eval() - dttm, country = [None, None] - if len(self.value) >= 2: - dttm = self.value[1].eval() - if len(self.value) == 3: - country = self.value[2] + dttm: datetime | None = None + country: str | None = None + # Both arguments after the holiday name are optional, so the second one + # is the datetime or the country depending on which were supplied. What + # tells them apart is the kind of argument: a datetime is always a + # function call (`datetime(...)`, `dateadd(...)`, ...) and a country is + # always quoted text. + for argument in self.value[1:]: + if isinstance(argument, EvalText): + country = argument.eval() + else: + dttm = argument.eval() holiday_year = dttm.year if dttm else parse_human_datetime("today").year - country = country.eval() if country else "US" - holiday_lookup = country_holidays(country, years=[holiday_year], observed=False) + try: + holiday_lookup = country_holidays( + country or "US", years=[holiday_year], observed=False + ) + except NotImplementedError as ex: + # `country_holidays` has no calendar for the country, which the + # caller can correct: the accepted spellings are the ISO code and + # the registered name, so a lowercase code or a country written out + # in full both land here. + raise ValueError( + _("Unable to find such a country: [%(country)s]", country=country) + ) from ex searched_result = holiday_lookup.get_named(holiday, lookup="istartswith") if len(searched_result) > 0: return dttm_from_timetuple(searched_result[0].timetuple()) diff --git a/tests/unit_tests/utils/date_parser_tests.py b/tests/unit_tests/utils/date_parser_tests.py index 10c6c2bf850..3bb2b3e07f9 100644 --- a/tests/unit_tests/utils/date_parser_tests.py +++ b/tests/unit_tests/utils/date_parser_tests.py @@ -639,6 +639,45 @@ def test_datetime_eval() -> None: assert result == expected +def test_holiday_reads_a_country_given_without_a_datetime() -> None: + """ + Both arguments after the holiday name are optional, so the country can be + the second one. Boxing Day tells the calendars apart: the UK has it and the + default US calendar does not. + """ + with freezegun.freeze_time("2026-06-15"): + assert datetime_eval("holiday('Boxing Day', 'UK')") == datetime(2026, 12, 26) + + with pytest.raises(ValueError, match="Unable to find such a holiday"): + datetime_eval("holiday('Boxing Day')") + + +def test_holiday_reads_a_country_past_an_empty_datetime_slot() -> None: + with freezegun.freeze_time("2026-06-15"): + assert datetime_eval("holiday('Boxing Day', , 'UK')") == datetime(2026, 12, 26) + + [email protected]( + "country", + [ + "ZZ", + "United Kingdom", # written out in full rather than as the ISO code + "us", # the right code in the wrong case + ], +) +def test_holiday_rejects_a_country_without_a_calendar(country: str) -> None: + """ + A country the holidays package has no calendar for is the caller's to + correct, so it has to arrive as a ValueError rather than as whatever the + package raises. + """ + expression = ( + f"holiday('Christmas Day', datetime('2019-01-01T00:00:00'), '{country}')" + ) + with pytest.raises(ValueError, match=f"Unable to find such a country: .{country}."): + datetime_eval(expression) + + @patch("superset.utils.date_parser.datetime") def test_parse_human_timedelta(mock_datetime: Mock) -> None: mock_datetime.now.return_value = datetime(2019, 4, 1)
