Repository navigation
Make 'datetime' a subclass of 'date'. - #2488
Conversation
6bacc45 to
4723360
Compare
|
The incompatibilities I've found have to do with the date's comparison dunders. |
JelleZijlstra
left a comment
There was a problem hiding this comment.
Thanks, the code makes sense. Hopefully CI is happy with it too.
|
I'd like to include some tests; but I don't find the relevant files in mypy. I've manually tested with: from datetime import date, datetime, timedelta
class Date: ...
class Datetime(Date): ...
now: date = datetime.now()
rightnow: Date = Datetime()
# Check __sub__
now3: datetime = datetime.now()
maybe_zero: timedelta = now3 - datetime.now()
now2: datetime = now3 + maybe_zero
now4: date = now3 + maybe_zero
now5: datetime = now4 + maybe_zero # Incompatible types in assignment
# Check __lt__ and friends
now4 < now5, now4 <= now5, now4 > now5, now4 >= now5
now5 < now4, now5 <= now4, now5 > now4, now5 >= now4With mypy 0.630 I get: With this PR I only get the error in line 17. |
|
I'd like to note that I was surprised when I updated typeshed and mypy stopped enforcing the distinction between Date and DateTime (specifically, I have functions meant to operate on Date where DateTime arguments should be rejected). They do indeed have a subtyping relationship in Python, but I can't think of a time when I wanted to be able to pass a DateTime where only Date was expected. The two classes do seem to be largely compatible (barring differences in result of .isoformat() and edge cases with, e.g., adding 24 hours across a timezone boundary and then normalizing). I'm not really sure whether or not it should be changed back, but it seems worth at least having the discussion? |
|
Since that relationship exists in Python, I believe it should be represented in typeshed. Otherwise this can lead to false positives for people who rely on this behaviour. |
Fixes #2487.