mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
gh-49766: Make date-datetime comparison more symmetric and flexible (GH-114760)
Now the special comparison methods like `__eq__` and `__lt__` return NotImplemented if one of comparands is date and other is datetime instead of ignoring the time part and the time zone or forcefully return "not equal" or raise TypeError. It makes comparison of date and datetime subclasses more symmetric and allows to change the default behavior by overriding the special comparison methods in subclasses. It is now the same as if date and datetime was independent classes.
This commit is contained in:
parent
d9d6909697
commit
b104360788
5 changed files with 91 additions and 84 deletions
|
|
@ -619,11 +619,27 @@ Notes:
|
|||
(4)
|
||||
:class:`date` objects are equal if they represent the same date.
|
||||
|
||||
:class:`!date` objects that are not also :class:`.datetime` instances
|
||||
are never equal to :class:`!datetime` objects, even if they represent
|
||||
the same date.
|
||||
|
||||
(5)
|
||||
*date1* is considered less than *date2* when *date1* precedes *date2* in time.
|
||||
In other words, ``date1 < date2`` if and only if ``date1.toordinal() <
|
||||
date2.toordinal()``.
|
||||
|
||||
Order comparison between a :class:`!date` object that is not also a
|
||||
:class:`.datetime` instance and a :class:`!datetime` object raises
|
||||
:exc:`TypeError`.
|
||||
|
||||
.. versionchanged:: 3.13
|
||||
Comparison between :class:`.datetime` object and an instance of
|
||||
the :class:`date` subclass that is not a :class:`!datetime` subclass
|
||||
no longer coverts the latter to :class:`!date`, ignoring the time part
|
||||
and the time zone.
|
||||
The default behavior can be changed by overriding the special comparison
|
||||
methods in subclasses.
|
||||
|
||||
In Boolean contexts, all :class:`date` objects are considered to be true.
|
||||
|
||||
Instance methods:
|
||||
|
|
@ -1192,9 +1208,6 @@ Supported operations:
|
|||
and time, taking into account the time zone.
|
||||
|
||||
Naive and aware :class:`!datetime` objects are never equal.
|
||||
:class:`!datetime` objects are never equal to :class:`date` objects
|
||||
that are not also :class:`!datetime` instances, even if they represent
|
||||
the same date.
|
||||
|
||||
If both comparands are aware and have different :attr:`~.datetime.tzinfo`
|
||||
attributes, the comparison acts as comparands were first converted to UTC
|
||||
|
|
@ -1206,9 +1219,8 @@ Supported operations:
|
|||
*datetime1* is considered less than *datetime2* when *datetime1* precedes
|
||||
*datetime2* in time, taking into account the time zone.
|
||||
|
||||
Order comparison between naive and aware :class:`.datetime` objects,
|
||||
as well as a :class:`!datetime` object and a :class:`!date` object
|
||||
that is not also a :class:`!datetime` instance, raises :exc:`TypeError`.
|
||||
Order comparison between naive and aware :class:`.datetime` objects
|
||||
raises :exc:`TypeError`.
|
||||
|
||||
If both comparands are aware and have different :attr:`~.datetime.tzinfo`
|
||||
attributes, the comparison acts as comparands were first converted to UTC
|
||||
|
|
@ -1218,6 +1230,14 @@ Supported operations:
|
|||
Equality comparisons between aware and naive :class:`.datetime`
|
||||
instances don't raise :exc:`TypeError`.
|
||||
|
||||
.. versionchanged:: 3.13
|
||||
Comparison between :class:`.datetime` object and an instance of
|
||||
the :class:`date` subclass that is not a :class:`!datetime` subclass
|
||||
no longer coverts the latter to :class:`!date`, ignoring the time part
|
||||
and the time zone.
|
||||
The default behavior can be changed by overriding the special comparison
|
||||
methods in subclasses.
|
||||
|
||||
Instance methods:
|
||||
|
||||
.. method:: datetime.date()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue