Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference in equality check between C and Python implementation for datetime module's timedelta and time #81760

Closed
tirkarthi opened this issue Jul 13, 2019 · 6 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tirkarthi
Copy link
Member

BPO 37579
Nosy @abalkin, @serhiy-storchaka, @pganssle, @miss-islington, @tirkarthi
PRs
  • bpo-37579: Return NotImplemented in Python implementation of __eq__ to match C implementation of datetime for timedelta and time #14726
  • [3.8] bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726) #14743
  • [3.7] bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726) #14745
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-07-14.10:04:38.808>
    created_at = <Date 2019-07-13.05:25:41.676>
    labels = ['3.7', '3.8', 'type-bug', 'library', '3.9']
    title = "Difference in equality check between C and Python implementation for datetime module's timedelta and time"
    updated_at = <Date 2019-07-25.21:16:37.867>
    user = 'https://github.com/tirkarthi'

    bugs.python.org fields:

    activity = <Date 2019-07-25.21:16:37.867>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-07-14.10:04:38.808>
    closer = 'p-ganssle'
    components = ['Library (Lib)']
    creation = <Date 2019-07-13.05:25:41.676>
    creator = 'xtreak'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37579
    keywords = ['patch']
    message_count = 6.0
    messages = ['347773', '347813', '347815', '347821', '347903', '348454']
    nosy_count = 5.0
    nosy_names = ['belopolsky', 'serhiy.storchaka', 'p-ganssle', 'miss-islington', 'xtreak']
    pr_nums = ['14726', '14743', '14745']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37579'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @tirkarthi
    Copy link
    Member Author

    As reported by Serhiy on https://bugs.python.org/issue37555#msg347733 there is a difference in __eq__ definition in datetime module's C and Python implementation for timedelta and time. When the other in __eq__ is not of the same type NotImplemented is not returned in Python implementation like C causing equality to behave differently.

    Difference in C implementation and Python implementation for datetime.timedelta.__eq__

    static PyObject *

    static PyObject *
    delta_richcompare(PyObject *self, PyObject *other, int op)
    {
        if (PyDelta_Check(other)) {
            int diff = delta_cmp(self, other);
            return diff_to_bool(diff, op);
        }
        else {
            Py_RETURN_NOTIMPLEMENTED;
        }
    }

    def __eq__(self, other):

    def __eq__(self, other):
        if isinstance(other, timedelta):
            return self._cmp(other) == 0
        else:
            return False

    I will add a PR for this with test.

    @tirkarthi tirkarthi added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 13, 2019
    @serhiy-storchaka
    Copy link
    Member

    Do you mind to fix also other similar cases if they are? __lt__ and others should have the same property.

    @pganssle
    Copy link
    Member

    New changeset e6b46aa by Paul Ganssle (Xtreak) in branch 'master':
    bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726)
    e6b46aa

    @miss-islington
    Copy link
    Contributor

    New changeset 143672c by Miss Islington (bot) in branch '3.8':
    bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726)
    143672c

    @miss-islington
    Copy link
    Contributor

    New changeset c6b3106 by Miss Islington (bot) (Xtreak) in branch '3.7':
    [3.7] bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726) (GH-14745)
    c6b3106

    @serhiy-storchaka
    Copy link
    Member

    See bpo-37685.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants