Skip to content

Commit

Permalink
Add supported categories validation (#1576)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkid15r authored Nov 28, 2023
1 parent ea38c85 commit 5c019de
Show file tree
Hide file tree
Showing 25 changed files with 39 additions and 47 deletions.
16 changes: 0 additions & 16 deletions holidays/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,3 @@
HEBREW = "hebrew"
HINDU = "hindu"
ISLAMIC = "islamic"

ALL_CATEGORIES = {
ARMED_FORCES,
BANK,
CHINESE,
CHRISTIAN,
GOVERNMENT,
HALF_DAY,
HEBREW,
HINDU,
ISLAMIC,
OPTIONAL,
PUBLIC,
SCHOOL,
WORKDAY,
}
2 changes: 1 addition & 1 deletion holidays/countries/austria.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class Austria(HolidayBase, ChristianHolidays, InternationalHolidays):
country = "AT"
default_language = "de"
supported_categories = {BANK, PUBLIC}
supported_categories = (BANK, PUBLIC)
supported_languages = ("de", "en_US", "uk")
subdivisions = ("1", "2", "3", "4", "5", "6", "7", "8", "9")

Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/belgium.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Belgium(HolidayBase, ChristianHolidays, InternationalHolidays):

country = "BE"
default_language = "nl"
supported_categories = {BANK, PUBLIC}
supported_categories = (BANK, PUBLIC)
supported_languages = ("de", "en_US", "fr", "nl", "uk")

def __init__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/brazil.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Brazil(HolidayBase, ChristianHolidays, InternationalHolidays):
"SP", # São Paulo
"TO", # Tocantins
)
supported_categories = {OPTIONAL, PUBLIC}
supported_categories = (OPTIONAL, PUBLIC)

def __init__(self, *args, **kwargs) -> None:
ChristianHolidays.__init__(self)
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/bulgaria.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Bulgaria(ObservedHolidayBase, ChristianHolidays, InternationalHolidays):
default_language = "bg"
# %s (Observed).
observed_label = tr("%s (почивен ден)")
supported_categories = {PUBLIC, SCHOOL}
supported_categories = (PUBLIC, SCHOOL)
supported_languages = ("bg", "en_US", "uk")

def __init__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/canada.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Canada(ObservedHolidayBase, ChristianHolidays, InternationalHolidays, Stat
default_language = "en"
# %s (Observed).
observed_label = tr("%s (Observed)")
supported_categories = {GOVERNMENT, OPTIONAL, PUBLIC}
supported_categories = (GOVERNMENT, OPTIONAL, PUBLIC)
subdivisions = (
"AB",
"BC",
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/china.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class China(HolidayBase, ChineseCalendarHolidays, InternationalHolidays, StaticH
"""

country = "CN"
supported_categories = {PUBLIC, HALF_DAY}
supported_categories = (PUBLIC, HALF_DAY)
default_language = "zh_CN"
supported_languages = ("en_US", "th", "zh_CN", "zh_TW")

Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/denmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Denmark(HolidayBase, ChristianHolidays, InternationalHolidays):

country = "DK"
default_language = "da"
supported_categories = {OPTIONAL, PUBLIC}
supported_categories = (OPTIONAL, PUBLIC)
supported_languages = ("da", "en_US", "uk")

def __init__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/greece.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Greece(ObservedHolidayBase, ChristianHolidays, InternationalHolidays):
default_language = "el"
# %s (Observed).
observed_label = tr("%s (παρατηρήθηκε)")
supported_categories = {HALF_DAY, PUBLIC}
supported_categories = (HALF_DAY, PUBLIC)
supported_languages = ("el", "en_US", "uk")

def __init__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/indonesia.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Indonesia(
default_language = "id"
estimated_label = tr("%s* (*perkiraan)")
supported_languages = ("en_US", "id", "uk")
supported_categories = {GOVERNMENT, PUBLIC}
supported_categories = (GOVERNMENT, PUBLIC)

def __init__(self, *args, **kwargs):
BuddhistCalendarHolidays.__init__(self, cls=IndonesiaBuddhistHolidays, show_estimated=True)
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/israel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class Israel(ObservedHolidayBase):
country = "IL"
observed_label = "%s (Observed)"
supported_categories = {OPTIONAL, PUBLIC, SCHOOL}
supported_categories = (OPTIONAL, PUBLIC, SCHOOL)

def __init__(self, *args, **kwargs):
kwargs.setdefault("observed_rule", FRI_TO_PREV_THU + SAT_TO_PREV_THU)
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/japan.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Japan(HolidayBase, InternationalHolidays, StaticHolidays):

country = "JP"
default_language = "ja"
supported_categories = {BANK, PUBLIC}
supported_categories = (BANK, PUBLIC)
supported_languages = ("en_US", "ja", "th")

def __init__(self, *args, **kwargs) -> None:
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/laos.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Laos(ObservedHolidayBase, InternationalHolidays, StaticHolidays, ThaiCalen
"""

country = "LA"
supported_categories = {BANK, PUBLIC, SCHOOL, WORKDAY}
supported_categories = (BANK, PUBLIC, SCHOOL, WORKDAY)
default_language = "lo"
# %s (in lieu).
observed_label = tr("ພັກຊົດເຊີຍ%s")
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/liechtenstein.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Liechtenstein(HolidayBase, ChristianHolidays, InternationalHolidays):

country = "LI"
default_language = "de"
supported_categories = {BANK, PUBLIC}
supported_categories = (BANK, PUBLIC)
supported_languages = ("de", "en_US", "uk")

def __init__(self, *args, **kwargs) -> None:
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/netherlands.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Netherlands(HolidayBase, ChristianHolidays, InternationalHolidays):

country = "NL"
default_language = "nl"
supported_categories = {OPTIONAL, PUBLIC}
supported_categories = (OPTIONAL, PUBLIC)
supported_languages = ("en_US", "nl", "uk")

def __init__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/portugal.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Portugal(HolidayBase, ChristianHolidays, InternationalHolidays):

country = "PT"
default_language = "pt_PT"
supported_categories = {OPTIONAL, PUBLIC}
supported_categories = (OPTIONAL, PUBLIC)

# https://en.wikipedia.org/wiki/ISO_3166-2:PT
subdivisions = (
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/slovakia.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Slovakia(HolidayBase, ChristianHolidays, InternationalHolidays, StaticHoli

country = "SK"
default_language = "sk"
supported_categories = {PUBLIC, WORKDAY}
supported_categories = (PUBLIC, WORKDAY)
supported_languages = ("en_US", "sk", "uk")

def __init__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/south_korea.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SouthKorea(
"""

country = "KR"
supported_categories = {BANK, PUBLIC}
supported_categories = (BANK, PUBLIC)
default_language = "ko"
# Alternative holiday for %s.
observed_label = tr("%s 대체 휴일")
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/tanzania.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Tanzania(
"""

country = "TZ"
supported_categories = {BANK, PUBLIC}
supported_categories = (BANK, PUBLIC)
default_language = "sw"
estimated_label = tr("%s* (*makisio)")
supported_languages = ("en_US", "sw")
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/thailand.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Thailand(ObservedHolidayBase, InternationalHolidays, StaticHolidays, ThaiC
"""

country = "TH"
supported_categories = {ARMED_FORCES, BANK, GOVERNMENT, PUBLIC, SCHOOL, WORKDAY}
supported_categories = (ARMED_FORCES, BANK, GOVERNMENT, PUBLIC, SCHOOL, WORKDAY)
default_language = "th"
# %s (in lieu).
observed_label = tr("ชดเชย%s")
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/timor_leste.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TimorLeste(
"""

country = "TL"
supported_categories = {GOVERNMENT, PUBLIC, WORKDAY}
supported_categories = (GOVERNMENT, PUBLIC, WORKDAY)
default_language = "pt_TL"
estimated_label = tr("%s* (*aproximada)")
supported_languages = ("en_US", "pt_TL", "tet")
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/turkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Turkey(HolidayBase, InternationalHolidays, IslamicHolidays, StaticHolidays
default_language = "tr"
# Estimated label.
estimated_label = tr("%s* (*tahmini)")
supported_categories = {HALF_DAY, PUBLIC}
supported_categories = (HALF_DAY, PUBLIC)
supported_languages = ("en_US", "tr", "uk")

def __init__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion holidays/countries/uruguay.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Uruguay(ObservedHolidayBase, ChristianHolidays, InternationalHolidays, Sta

country = "UY"
default_language = "es"
supported_categories = {BANK, PUBLIC}
supported_categories = (BANK, PUBLIC)
supported_languages = ("en_US", "es", "uk")

def __init__(self, *args, **kwargs):
Expand Down
13 changes: 7 additions & 6 deletions holidays/holiday_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
_get_nth_weekday_from,
_get_nth_weekday_of_month,
)
from holidays.constants import HOLIDAY_NAME_DELIMITER, ALL_CATEGORIES, PUBLIC
from holidays.constants import HOLIDAY_NAME_DELIMITER, PUBLIC
from holidays.helpers import _normalize_arguments, _normalize_tuple

CategoryArg = Union[str, Iterable[str]]
Expand Down Expand Up @@ -231,7 +231,7 @@ def _populate(self, year):
"""The entity language used by default."""
categories: Optional[Set[str]] = None
"""Requested holiday categories."""
supported_categories: Set[str] = set()
supported_categories: Tuple[str, ...] = ()
"""All holiday categories supported by this entity."""
supported_languages: Tuple[str, ...] = ()
"""All languages supported by this entity."""
Expand Down Expand Up @@ -319,10 +319,11 @@ def __init__(
DeprecationWarning,
)

unknown_categories = self.categories.difference( # type: ignore[union-attr]
ALL_CATEGORIES
)
if len(unknown_categories) > 0:
if len(self.supported_categories) > 0 and (
unknown_categories := self.categories.difference( # type: ignore[union-attr]
set(self.supported_categories)
)
):
raise NotImplementedError(
f"Category is not supported: {', '.join(unknown_categories)}."
)
Expand Down
13 changes: 10 additions & 3 deletions tests/test_holiday_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from datetime import timedelta as td

from holidays.calendars.gregorian import JAN, FEB, OCT, DEC, MON, TUE, SAT, SUN
from holidays.constants import HOLIDAY_NAME_DELIMITER, PUBLIC
from holidays.constants import HOLIDAY_NAME_DELIMITER, OPTIONAL, PUBLIC, SCHOOL
from holidays.holiday_base import HolidayBase


Expand All @@ -35,7 +35,6 @@ class EntityStub(HolidayBase):
}
substituted_date_format = "%d/%m/%Y"
substituted_label = "From %s"
supported_categories = {PUBLIC}

def _add_observed(self, dt: date, before: bool = True, after: bool = True) -> None:
if not self.observed:
Expand Down Expand Up @@ -63,6 +62,7 @@ def _populate(self, year: int) -> None:
class CountryStub1(EntityStub):
country = "CS1"
subdivisions = ("Subdiv1", "Subdiv2")
supported_categories = (PUBLIC, SCHOOL)

def _add_subdiv_subdiv1_holidays(self):
self._add_holiday_aug_10("Subdiv1 Custom Holiday")
Expand Down Expand Up @@ -99,7 +99,14 @@ class MarketStub2(EntityStub):

class TestArgs(unittest.TestCase):
def test_categories(self):
self.assertRaises(NotImplementedError, lambda: CountryStub1(categories="HOME"))
self.assertEqual({PUBLIC}, CountryStub1(categories=PUBLIC).categories)
self.assertEqual({PUBLIC, SCHOOL}, CountryStub1(categories=(PUBLIC, SCHOOL)).categories)

self.assertRaises(NotImplementedError, lambda: CountryStub1(categories=OPTIONAL))
self.assertRaises(NotImplementedError, lambda: CountryStub1(categories="UNSUPPORTED"))
self.assertRaises(
NotImplementedError, lambda: CountryStub1(categories=("HOME", "UNSUPPORTED"))
)

def test_country(self):
self.assertEqual(CountryStub1().country, "CS1")
Expand Down

0 comments on commit 5c019de

Please sign in to comment.