From 8ac735dafebfd2fd84e9383478953e3194b7c4fe Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Tue, 4 Jun 2024 14:20:01 -0400 Subject: [PATCH] misc(aurora): change the JSONEncoder subclass to use match/case instead of if statements --- aurora/utilities/json.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/aurora/utilities/json.py b/aurora/utilities/json.py index 80f1259..5dbd4eb 100644 --- a/aurora/utilities/json.py +++ b/aurora/utilities/json.py @@ -1,5 +1,6 @@ import json from datetime import datetime, timedelta +from typing import Any from redbot.core.bot import Red @@ -7,16 +8,18 @@ from ..models.base import AuroraBaseModel class JSONEncoder(json.JSONEncoder): - def default(self, o): - if isinstance(o, datetime): - return int(o.timestamp()) - if isinstance(o, timedelta): - return str(o) - if isinstance(o, AuroraBaseModel): - return o.dump() - if isinstance(o, Red): - return None - return super().default(o) + def default(self, o) -> Any: + match o: + case datetime(): + return int(o.timestamp()) + case timedelta(): + return str(o) + case AuroraBaseModel(): + return o.dump() + case Red(): + return None + case _: + return super().default(o) # This is a wrapper around the json module's dumps function that uses our custom JSONEncoder class