Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

datetime.__sub__ overload order #10924

Copy link
Copy link
Closed
Closed
Copy link
@randolf-scholz

Description

@randolf-scholz
Issue body actions

Currently, the overload order is:

if sys.version_info >= (3, 8):
@overload # type: ignore[override]
def __sub__(self, __value: timedelta) -> Self: ...
@overload
def __sub__(self: _D, __value: _D) -> timedelta: ...

However, both the c-module and the fallback pydatetime first check if other is a datetime:

    def __sub__(self, other):
        "Subtract two datetimes, or a datetime and a timedelta."
        if not isinstance(other, datetime):
            if isinstance(other, timedelta):
                return self + -other
            return NotImplemented

https://github.com/python/cpython/blob/c7d68f907ad3e3aa17546df92a32bddb145a69bf/Lib/_pydatetime.py#L2235-L2257

https://github.com/python/cpython/blob/1198076447f35b19a9173866ccb9839f3bcf3f17/Modules/_datetimemodule.c#L5608

So shouldn't the overload order be vice versa?

Also, I noticed that at runtime, timedelta has __rdivmod__, __rmod__ and datetime has __rsub__, which are missing in the stub. But I guess this is because the pure python fallback doesn't have them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.