From effa0e4df75b1c2ba24608297e362cc776d66236 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Tue, 31 Mar 2020 19:53:47 +0200 Subject: Deseasonify: validate season setup Add a static check confirming that no seasons overlap. This avoids having to deal with ambiguities in `get_current_season` at runtime, as we can now rely on `len(active_seasons)` always being 0 or 1. Co-authored-by: MarkKoz --- bot/seasons.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'bot/seasons.py') diff --git a/bot/seasons.py b/bot/seasons.py index 6ec5f652..55cfef3c 100644 --- a/bot/seasons.py +++ b/bot/seasons.py @@ -3,6 +3,7 @@ import typing as t from bot.constants import Colours, Month from bot.utils import resolve_current_month +from bot.utils.exceptions import BrandingError log = logging.getLogger(__name__) @@ -141,9 +142,6 @@ def get_current_season() -> t.Type[SeasonBase]: if not active_seasons: return SeasonBase - if len(active_seasons) > 1: - log.warning(f"Multiple active season in month {current_month.name}") - return active_seasons[0] @@ -160,3 +158,24 @@ def get_season(name: str) -> t.Optional[t.Type[SeasonBase]]: if name in matches: return season + + +def _validate_season_overlap() -> None: + """ + Raise BrandingError if there are any colliding seasons. + + This serves as a local test to ensure that seasons haven't been misconfigured. + """ + month_to_season = {} + + for season in SeasonBase.__subclasses__(): + for month in season.months: + colliding_season = month_to_season.get(month) + + if colliding_season: + raise BrandingError(f"Season {season} collides with {colliding_season} in {month.name}") + else: + month_to_season[month] = season + + +_validate_season_overlap() -- cgit v1.2.3