From 5d2c99f4b61cfc03493d2cf56d34029e91c57904 Mon Sep 17 00:00:00 2001 From: brassy endomorph Date: Sat, 28 Sep 2024 09:10:09 +0000 Subject: [PATCH] idiomatically use super() constructors in DB models and fix one bug from incorrect usage --- hushline/model.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/hushline/model.py b/hushline/model.py index d69108e4..2fe097e9 100644 --- a/hushline/model.py +++ b/hushline/model.py @@ -50,8 +50,7 @@ def fetch_or_default(cls) -> Self: return cls.fetch() or cls() def __init__(self, id: int | None = None) -> None: - super().__init__() - self.id = id if id is not None else self._DEFAULT_ID + super().__init__(id=id if id is not None else self._DEFAULT_ID) @dataclass(frozen=True, repr=False, eq=False) @@ -99,9 +98,11 @@ def __init__( is_primary: bool, **kwargs: Any, ) -> None: - super().__init__(**kwargs) - self._username = _username - self.is_primary = is_primary + super().__init__( + _username=_username, + is_primary=is_primary, + **kwargs, + ) @property def username(self) -> str: @@ -245,7 +246,7 @@ def __init__(self, **kwargs: Any) -> None: if key in kwargs: raise ValueError(f"Key {key!r} cannot be mannually set. Try 'password' instead.") pw = kwargs.pop("password", None) - super().__init__() + super().__init__(**kwargs) self.password_hash = pw @@ -281,11 +282,12 @@ def __init__( otp_code: str | None = None, timecode: int | None = None, ) -> None: - super().__init__() - self.user_id = user_id - self.successful = successful - self.otp_code = otp_code - self.timecode = timecode + super().__init__( + user_id=user_id, + successful=successful, + otp_code=otp_code, + timecode=timecode, + ) class Message(Model): @@ -323,9 +325,10 @@ class InviteCode(Model): expiration_date: Mapped[datetime] def __init__(self) -> None: - super().__init__() - self.code = secrets.token_urlsafe(16) - self.expiration_date = datetime.now(timezone.utc) + timedelta(days=365) + super().__init__( + code=secrets.token_urlsafe(16), + expiration_date=datetime.now(timezone.utc) + timedelta(days=365), + ) def __repr__(self) -> str: return f""