fix: Add UUID to Person class to make it hashable

This commit is contained in:
Andreas Koepf (aider) 2025-01-24 16:55:56 +01:00
parent e2e4e633be
commit 6a81b7a8f7

View file

@ -1,5 +1,6 @@
import random
from dataclasses import dataclass
import uuid
from dataclasses import dataclass, field
from typing import Optional, Dict, List, Set, Tuple
from enum import Enum
@ -31,11 +32,20 @@ class Person:
spouse: Optional['Person'] = None
parents: List['Person'] = None
children: List['Person'] = None
_id: uuid.UUID = field(default_factory=uuid.uuid4, compare=False)
def __post_init__(self):
self.parents = self.parents or []
self.children = self.children or []
def __hash__(self):
return hash(self._id)
def __eq__(self, other):
if not isinstance(other, Person):
return False
return self._id == other._id
def add_child(self, child: 'Person'):
if child not in self.children:
self.children.append(child)