Mypy: datetimes and dates are not substitutable but mypy thinks they are

Created on 17 Jun 2020  路  1Comment  路  Source: python/mypy

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

I tried that first ;-) https://github.com/python/typeshed/issues/3805

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?

This is a bug in the system overall but probably a feature request in mypy itself.

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from datetime import date, datetime

if datetime.now() < date.today():
    print("that's a surprise!")
  • What is the actual behavior/output?

No error!

  • What is the behavior/output you expect?

A warning, since at runtime I get TypeError: can't compare datetime.datetime to datetime.date.

  • What are the versions of mypy and Python you are using?

0.770, 3.8.2

  • Do you see the same issue after installing mypy from Git master?

Yes.

  • What are the mypy flags you are using? (For example --strict-optional)
python_version=3.8
incremental=True
follow_imports=normal
warn_redundant_casts=True
warn_unused_ignores=True
strict_optional=True
strict_equality=True
no_implicit_optional=True
disallow_untyped_defs=True
disallow_any_generics=True
needs discussion

Most helpful comment

LSP violations are generally tricky. Some of them are fairly benign and rarely cause issues, but others can be painful.

As typeshed includes the subclass relationship present in the implementation, current mypy behavior is arguably "correct". However, I can imagine a way to deal with this in mypy: introduce optional subtype checks that would complain about code that relies on subtyping relationships that are not safe, such as datetime vs date. Mypy would maintain a set of known unsafe subtyping relationships, including cases where a class is a subclass of another class but shouldn't be treated as a subtype. This could also be used for things like warning about str used as Iterable[str].

>All comments

LSP violations are generally tricky. Some of them are fairly benign and rarely cause issues, but others can be painful.

As typeshed includes the subclass relationship present in the implementation, current mypy behavior is arguably "correct". However, I can imagine a way to deal with this in mypy: introduce optional subtype checks that would complain about code that relies on subtyping relationships that are not safe, such as datetime vs date. Mypy would maintain a set of known unsafe subtyping relationships, including cases where a class is a subclass of another class but shouldn't be treated as a subtype. This could also be used for things like warning about str used as Iterable[str].

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kissge picture kissge  路  3Comments

edwardcwang picture edwardcwang  路  3Comments

PeterJCLaw picture PeterJCLaw  路  3Comments

jstasiak picture jstasiak  路  3Comments

Stiivi picture Stiivi  路  3Comments