Skip to content

datetimes and dates are not substitutable but mypy thinks they are #9015

Open

Description

@glyph

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 ;-) python/typeshed#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

Activity

  1. JukkaL commented on Jun 19, 2020

    @JukkaL
    Collaborator

    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].

  2. m-aciek commented on Jul 5, 2023

    @m-aciek

    Sharing a workaround typing library created by @glyph: https://pypi.org/project/datetype/. Found on StackOverflow.

  3. glyph commented on Jul 7, 2023

    @glyph
    Author

    Thanks @m-aciek, I forgot to link it directly from here :)

  4. m-aciek commented on Sep 17, 2025

    @m-aciek

    Another workaround is to use datetime exclusively and use its instances to mark dates (with 00:00 time). This way we prevent TypeErrors in runtime when comparing objects.

  5. m-aciek commented on Jan 20, 2026

    @m-aciek

    […] 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].

    The case of str/Iterable[str] is being addressed separately: #20577. Another unsafe subtype relation is int/bool. I couldn't find more in the standard library.

    I've created a pull request that addresses the check described above, but I wonder if from mypy design perspective there shouldn't be separate flags for each of such relations, for a better control. Please advise. Thank you.

  6. wyattscarpenter commented on Jan 20, 2026

    @wyattscarpenter
    Contributor

    Is int/bool unsafe? I thought it was just unexpected by people, not unsafe per se. Regardless, related: #8363

  7. m-aciek commented on Jan 21, 2026

    @m-aciek

    Is int/bool unsafe? I thought it was just unexpected by people, not unsafe per se.

    It is not (as) unsafe as datatime/date is. Though in my opinion it's the same category as str/Iterable[str], being unsafe through potential unexpectance.

    Regardless, related: #8363

    Thank you for pointing it out. With str/Iterable[str] and int/bool cases being handled separately, with dedicated mypy codes, I'm more inclined to propose a code dedicated only to datetime/date case. I hesitate between strict-date or safe-datetime (are there better candidates? 🤔). I lean towards the second one.

  8. added a commit that references this issue on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions