Message 310653 - Python tracker (original) (raw)

Raising for order=True if one of the ordering dunders exists sounds fine.

I am confused by the corner case for hash. Your table:

""" eq=? frozen=? hash False False do not generate hash False True do not generate hash True False set hash to None unless it already exists True True generate hash unless it already exists and is None """

Then you write at the end of that message:

""" One special case to recognize is if the class defines a eq. In this case, Python will assign hash=None before the dataclass decorator is called. The decorator cannot distinguish between these two cases (except possibly by using the order of dict keys, but that seems overly fragile):

@dataclass class A: def eq(self, other): pass

@dataclass class B: def eq(self, other): pass hash = None

This is the source of the last line in the above table: for a dataclass where eq=True, frozen=True, and hash=None, if hash is None it will still be overwritten. The assumption is that this is what the user wants, but it's a tricky corner case. It also occurs if setting hash=True and defining eq. Again, it's not expected to come up in normal usage. """

I think I understand what you are saying there -- the two cases are treated the same, and a hash is created (assuming the decorator is really "@dataclass(eq=True, frozen=True)"), overwriting the "hash = None" for class B.

However the table's last line says "generate hash unless it already exists and is None". Perhaps that was a typo and you meant to write "and is not None"?