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:
This is a bug in the system overall but probably a feature request in mypy itself.
from datetime import date, datetime
if datetime.now() < date.today():
print("that's a surprise!")
No error!
A warning, since at runtime I get TypeError: can't compare datetime.datetime to datetime.date.
0.770, 3.8.2
Yes.
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
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].
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
datetimevsdate. 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 aboutstrused asIterable[str].