Skip to content

Date/DateTime + relativedelta() #11

@SonOfLilit

Description

@SonOfLilit

Hi,

You're my hero. Thanks.

Also, I want to actually use your library in my code, but I'm getting errors for my_date + relativedelta(days=1) (and same with datetimes). I wonder what it would take to fix it.

Can your pyi add additional overloads to [https://github.com/python/typeshed/blob/main/stubs/python-dateutil/dateutil/relativedelta.pyi](typeshed's relativedelta.pyi)?

class relativedelta:
    # ...
    @overload
    def __add__(self, other: relativedelta) -> Self: ...
    @overload
    def __add__(self, other: timedelta) -> Self: ...
    @overload
    def __add__(self, other: _DateT) -> _DateT: ...
    # and __radd__ etc'

I guess we can add a fake Date.__add__(self, other: relativedelta) instead and it would do the right thing? Do we need to entirely replace typeshed's relativedelta for this to work?

How can I approach getting this to work? Will you accept a PR? Is there a workaround I can use in the meantime?

You should be aware of this before you answer, I guess. One of the weirder corners of Python:

>>> from dateutil.relativedelta import relativedelta
>>> from datetime import date, datetime
>>> date(1970, 1, 1) + relativedelta(days=1)
datetime.date(1970, 1, 2)
>>> date(1970, 1, 1) + relativedelta(minutes=1)
datetime.datetime(1970, 1, 1, 0, 1)

I guess we could have something like:

    @overload
    def __add__(self, other: Date) -> Date | DateTime: ...

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