fix(aurora): fixed an issue with json encoding
All checks were successful
Actions / Build Documentation (MkDocs) (pull_request) Successful in 47s
Actions / Lint Code (Ruff & Pylint) (pull_request) Successful in 1m10s

This commit is contained in:
Seaswimmer 2024-06-03 00:07:52 -04:00
parent 1990b97518
commit db477c4744
Signed by: cswimr
GPG key ID: 5D671B5D03D65A7F
2 changed files with 9 additions and 3 deletions

View file

@ -9,14 +9,20 @@ class AuroraBaseModel(BaseModel):
model_config = ConfigDict(ignored_types=(Red,), arbitrary_types_allowed=True) model_config = ConfigDict(ignored_types=(Red,), arbitrary_types_allowed=True)
bot: Red bot: Red
def dump(self) -> dict:
return self.model_dump(exclude={"bot"})
def to_json(self, indent: int | None = None, file: Any | None = None, **kwargs): def to_json(self, indent: int | None = None, file: Any | None = None, **kwargs):
from ..utilities.json import dump, dumps # pylint: disable=cyclic-import from ..utilities.json import dump, dumps # pylint: disable=cyclic-import
return dump(self.model_dump(exclude={"bot"}), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot"}), indent=indent, **kwargs) return dump(self.dump(), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot"}), indent=indent, **kwargs)
class AuroraGuildModel(AuroraBaseModel): class AuroraGuildModel(AuroraBaseModel):
"""Subclass of AuroraBaseModel that includes a guild_id attribute, and a modified to_json() method to match.""" """Subclass of AuroraBaseModel that includes a guild_id attribute, and a modified to_json() method to match."""
guild_id: int guild_id: int
def dump(self) -> dict:
return self.model_dump(exclude={"bot", "guild_id"})
def to_json(self, indent: int | None = None, file: Any | None = None, **kwargs): def to_json(self, indent: int | None = None, file: Any | None = None, **kwargs):
from ..utilities.json import dump, dumps # pylint: disable=cyclic-import from ..utilities.json import dump, dumps # pylint: disable=cyclic-import
return dump(self.model_dump(exclude={"bot", "guild_id"}), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot", "guild_id"}), indent=indent, **kwargs) return dump(self.dump(), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot", "guild_id"}), indent=indent, **kwargs)

View file

@ -13,7 +13,7 @@ class JSONEncoder(json.JSONEncoder):
if isinstance(o, timedelta): if isinstance(o, timedelta):
return str(o) return str(o)
if isinstance(o, AuroraBaseModel): if isinstance(o, AuroraBaseModel):
return o.to_json() return o.dump()
if isinstance(o, Red): if isinstance(o, Red):
return None return None
return super().default(o) return super().default(o)