Skip to content

Commit

Permalink
Make pydantic custom base model hash method recursive (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcamise-gpsw authored Dec 6, 2023
1 parent af0ad92 commit fceba44
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

"""Base classes shared throughout models"""

import json

from pydantic import BaseModel

from open_gopro.util import pretty_print, scrub
Expand All @@ -12,7 +14,13 @@ class CustomBaseModel(BaseModel):
"""Additional functionality added to Pydantic BaseModel"""

def __hash__(self) -> int:
return hash((type(self),) + tuple(getattr(self, f) for f in self.__fields__.keys()))
h = hash((type(self),))
for field in self.__fields__.keys():
if isinstance(field, (dict, list)):
h += json.loads(field)
# Base case
h += hash(field)
return h

def __str__(self) -> str:
d = dict(self)
Expand Down

0 comments on commit fceba44

Please sign in to comment.