typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Relativedelta `__rsub__` doesn't respect subclasses of datetime

Open MaicoTimmerman opened this issue 2 years ago • 0 comments

Given this sample case:

from datetime import datetime
from dateutil.relativedelta import relativedelta

class MyDateTime(datetime):
    pass

d = MyDateTime.now()
x = d - relativedelta(days=1)
print(type(x))
# reveal_type(x)

results in Python:

<class '__main__.MyDateTime'>

and mypy reports:

test.py:10: note: Revealed type is "datetime.datetime"

However, I'd expect MyDateTime as type here. I'm guessing the generic isn't correct here? With understanding of TypeVar and it's effects on inheritance. Should it be adjusted to the following?

_DateT = TypeVar("_DateT", bound=date)

Given that datetime inherits from date already?

MaicoTimmerman avatar Feb 22 '24 16:02 MaicoTimmerman