From ffc3d477e2f3367818ba52e053971ba085892313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Murta?= Date: Mon, 1 May 2023 21:24:23 +0100 Subject: [PATCH] Fix typing for model.py and hash functions Set unsafe_hash parameter of dataclasses to true on types that need to be hashed. The __hash__ method won't be automatically generated since the types can't be declared as frozen. --- pfbudget/db/model.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/pfbudget/db/model.py b/pfbudget/db/model.py index 6337362..de4cee4 100644 --- a/pfbudget/db/model.py +++ b/pfbudget/db/model.py @@ -3,7 +3,7 @@ import datetime as dt import decimal import enum import re -from typing import Annotated, Any, Optional +from typing import Annotated, Any, Callable, Optional from sqlalchemy import ( BigInteger, @@ -270,7 +270,7 @@ class Tag(Base): ) -class TransactionTag(Base, Export): +class TransactionTag(Base, Export, unsafe_hash=True): __tablename__ = "transactions_tagged" id: Mapped[idfk] = mapped_column(primary_key=True, init=False) @@ -280,9 +280,6 @@ class TransactionTag(Base, Export): def format(self): return dict(tag=self.tag) - def __hash__(self): - return hash(self.id) - categoryselector = Annotated[ Selector_T, @@ -341,7 +338,7 @@ class Link(Base): link: Mapped[idfk] = mapped_column(primary_key=True) -class Rule(Base, Export, init=False): +class Rule(Base, Export, init=False, unsafe_hash=True): __tablename__ = "rules" id: Mapped[idpk] = mapped_column(init=False) @@ -401,7 +398,7 @@ class Rule(Base, Export, init=False): ) @staticmethod - def exists(r, op) -> bool: + def exists(r: Optional[Any], op: Callable[[Any], bool]) -> bool: return op(r) if r is not None else True @@ -428,9 +425,6 @@ class CategoryRule(Rule): super().__init__(**kwargs) self.name = name - def __hash__(self): - return hash(self.id) - class TagRule(Rule): __tablename__ = "tag_rules" @@ -454,6 +448,3 @@ class TagRule(Rule): def __init__(self, name: str, **kwargs: Any) -> None: super().__init__(**kwargs) self.tag = name - - def __hash__(self): - return hash(self.id)