Skip to content

Make 'datetime' a subclass of 'date'. - #2488

Merged
JelleZijlstra merged 1 commit into
python:masterfrom
mvaled:datetime-base-date
Oct 2, 2018
Merged

JelleZijlstra merged 1 commit into
python:masterfrom
mvaled:datetime-base-date

Conversation

@mvaled

@mvaled mvaled commented Sep 26, 2018

Copy link
Copy Markdown
Contributor

Fixes #2487.

@mvaled

mvaled commented Sep 26, 2018

Copy link
Copy Markdown
Contributor Author

The incompatibilities I've found have to do with the date's comparison dunders.

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the code makes sense. Hopefully CI is happy with it too.

@mvaled

mvaled commented Sep 26, 2018

Copy link
Copy Markdown
Contributor Author

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 >= now4

With mypy 0.630 I get:

bug.py:6: error: Incompatible types in assignment (expression has type "datetime", variable has type "date")
bug.py:16: error: Incompatible types in assignment (expression has type "datetime", variable has type "date")
bug.py:17: error: Incompatible types in assignment (expression has type "date", variable has type "datetime")
bug.py:20: error: Unsupported operand types for < ("date" and "datetime")
bug.py:20: error: Unsupported operand types for <= ("date" and "datetime")
bug.py:20: error: Unsupported operand types for > ("date" and "datetime")
bug.py:20: error: Unsupported operand types for >= ("date" and "datetime")
bug.py:21: error: Unsupported operand types for < ("datetime" and "date")
bug.py:21: error: Unsupported operand types for <= ("datetime" and "date")
bug.py:21: error: Unsupported operand types for > ("datetime" and "date")
bug.py:21: error: Unsupported operand types for >= ("datetime" and "date")

With this PR I only get the error in line 17.

@JelleZijlstra
JelleZijlstra merged commit 1297caa into python:master Oct 2, 2018
@lincolnq

Copy link
Copy Markdown
Contributor

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?

@srittau

srittau commented Dec 15, 2018

Copy link
Copy Markdown
Collaborator

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.

yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this pull request Jan 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants