[Datetime-SIG] Another round on error-checking

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Aug 31 21:54:15 CEST 2015

On Mon, Aug 31, 2015 at 3:33 PM, Alexander Belopolsky <
alexander.belopolsky at gmail.com> wrote:

> On Mon, Aug 31, 2015 at 3:20 PM, Tim Peters <tim.peters at gmail.com> wrote:
>>     def __hash__(self):
>>         if self._hashcode == -1:
>>             tzoff = self.utcoffset()
>>             if tzoff is None:
>>                 self._hashcode =
>> hash(self.replace(first=True)._getstate()[0])
>>             else:
>>                 days = _ymd2ord(self.year, self.month, self.day)
>>                 seconds = self.hour * 3600 + self.minute * 60 +
>> self.second
>>                 self._hashcode = hash(timedelta(days, seconds,
>> self.microsecond) - tzoff)
>>         return self._hashcode
>> So it's the case that two datetimes that compare true may have
>> different hashes, when they represent the earlier and later times in a
>> fold.  I didn't say "it's a puzzle" lightly ;-)
> Yes, it looks like I have a bug there, but isn't fixing it just a matter
> of moving self.replace(first=True) up two lines?  Is there a bigger
> puzzle?  Certainly x == y ⇒ hash(x) == hash(y) is the implication that I
> intend to preserve in all cases.

I think I admitted defeat too soon.  Can you present a specific case where
"two datetimes that compare true have different hashes"?  There may be some
subtlety due to the fact that we ignore tzinfo in == if it is the same for
both sides, but when we compute hash(), we don't know what's on the other
side.  It is hard to tell without a specific example.  I thought I got it
right when I wrote the code above, but it is possible I missed some case.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/datetime-sig/attachments/20150831/92d093b9/attachment.html>

More information about the Datetime-SIG mailing list