I have a dataclass whose instances I want to hash and order, using the id
member as a key.
from dataclasses import dataclass, field
@dataclass(eq=True, order=True)
class Category:
id: str = field(compare=True)
name: str = field(default="set this in post_init", compare=False)
I know that I can implement __hash__
myself. However, I would like dataclasses to do the work for me because they are intended to handle this.
Unfortunately, the above dataclass fails:
a = sorted(list(set([ Category(id='x'), Category(id='y')])))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'Category'