Skip to content

datetime.datetime should not inherit datetime.date #4802

@tomasaschan

Description

@tomasaschan

datetime.datetime and datetime.date types cannot be used interchangeably, but the inheritance specified by the types in this project imply that they can. Consider the following code:

import datetime

def foo(d: datetime.date):
    return d < datetime.date.today()

foo(datetime.datetime.now()) # raises TypeError: can't compare datetime.datetime to datetime.date

Typechecking should indicate a problem - but it doesn't, resulting in a runtime exception instead.

I raised this issue with the Pyright project, but was redirected here.

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