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.
This commit is contained in:
Luís Murta 2023-05-01 21:24:23 +01:00
parent 01df97ed46
commit ffc3d477e2
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94

View File

@ -3,7 +3,7 @@ import datetime as dt
import decimal import decimal
import enum import enum
import re import re
from typing import Annotated, Any, Optional from typing import Annotated, Any, Callable, Optional
from sqlalchemy import ( from sqlalchemy import (
BigInteger, BigInteger,
@ -270,7 +270,7 @@ class Tag(Base):
) )
class TransactionTag(Base, Export): class TransactionTag(Base, Export, unsafe_hash=True):
__tablename__ = "transactions_tagged" __tablename__ = "transactions_tagged"
id: Mapped[idfk] = mapped_column(primary_key=True, init=False) id: Mapped[idfk] = mapped_column(primary_key=True, init=False)
@ -280,9 +280,6 @@ class TransactionTag(Base, Export):
def format(self): def format(self):
return dict(tag=self.tag) return dict(tag=self.tag)
def __hash__(self):
return hash(self.id)
categoryselector = Annotated[ categoryselector = Annotated[
Selector_T, Selector_T,
@ -341,7 +338,7 @@ class Link(Base):
link: Mapped[idfk] = mapped_column(primary_key=True) 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" __tablename__ = "rules"
id: Mapped[idpk] = mapped_column(init=False) id: Mapped[idpk] = mapped_column(init=False)
@ -401,7 +398,7 @@ class Rule(Base, Export, init=False):
) )
@staticmethod @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 return op(r) if r is not None else True
@ -428,9 +425,6 @@ class CategoryRule(Rule):
super().__init__(**kwargs) super().__init__(**kwargs)
self.name = name self.name = name
def __hash__(self):
return hash(self.id)
class TagRule(Rule): class TagRule(Rule):
__tablename__ = "tag_rules" __tablename__ = "tag_rules"
@ -454,6 +448,3 @@ class TagRule(Rule):
def __init__(self, name: str, **kwargs: Any) -> None: def __init__(self, name: str, **kwargs: Any) -> None:
super().__init__(**kwargs) super().__init__(**kwargs)
self.tag = name self.tag = name
def __hash__(self):
return hash(self.id)