New GitHub issue #116035 from cjw296:<br>
<hr>
<pre>
# Bug report
### Bug description:
I'm sure folks will pull out reasoning behind this from the [inscrutable section in PEP 495](https://peps.python.org/pep-0495/#aware-datetime-equality-comparison) but this behaviour is honestly extremely surprising at best and just flat out wrong on first glance:
```python
>>> from datetime import time, datetime, date
>>> time(fold=1) == time(fold=0)
True
>>> hash(time(fold=1)) == hash(time(fold=0))
True
>>> datetime(2024, 10, 27, fold=1) == datetime(2024, 10, 27, fold=0)
True
>>> hash(datetime(2024, 10, 27, fold=1)) == hash(datetime(2024, 10, 27, fold=0))
True
```
Even when deliberately specifying as explicitly as possible two points in time which are absolutely not the same (these are two points during the UK DST backwards transition later this year):
```python
>>> from zoneinfo import ZoneInfo
>>> tz = ZoneInfo('Europe/London')
>>> dt1 = datetime(2024, 10, 27, 1, 30, fold=0, tzinfo=tz)
>>> dt2 = datetime(2024, 10, 27, 1, 30, fold=1, tzinfo=tz)
>>> dt1 == dt2
True
>>> hash(dt1) == hash(dt2)
True
```
This has really unpleasant implications for things like storing points in time in a dictionary:
```python
>>> d = {}
>>> d[dt1] = 1
>>> d[dt2] = 2
>>> d[dt1]
2
>>> d[dt2]
2
```
The inverse of this issue is reported in https://github.com/python/cpython/issues/115845.
Yes, I know the timestamp can be used:
```python
>>> dt1.timestamp() == dt2.timestamp()
False
```
...but not on `time` objects, and timestamp also brings the local machine's timezone into play:
```python
True
>>> fold_equal(2024, 10, 27, 0, 30)
True
>>> fold_equal(2024, 10, 27, 1, 30)
False
>>> fold_equal(2024, 10, 27, 2, 30)
True
>>> fold_equal(2024, 10, 27, 0, 30, tzinfo=ZoneInfo('America/Chicago'))
True
>>> fold_equal(2024, 10, 27, 1, 30, tzinfo=ZoneInfo('America/Chicago'))
True
>>> fold_equal(2024, 10, 27, 2, 30, tzinfo=ZoneInfo('America/Chicago'))
True
```
Concretely, it would be a lot less confusing if:
- `time` objects, and `datetime` objects where `tzinfo` is `None` are equal and hashed the same only if all of their attributes _including `fold`_
- `datetime` objects where `tzinfo` is not `None` are equal and hashed the same only if they represent the exact point in time
### CPython versions tested on:
3.12
### Operating systems tested on:
_No response_
</pre>
<hr>
<a href="https://github.com/python/cpython/issues/116035">View on GitHub</a>
<p>Labels: type-bug</p>
<p>Assignee: </p>