python / python/mypy

datetimes and dates are not substitutable but mypy thinks they are

Open
#9,015 7 comments 20 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs discussion
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided datetime/date comparison with the listed mypy flags and confirm the runtime TypeError versus mypy's lack of a diagnostic. Trace the comparison checking behavior for these Python types; done means mypy reports the incompatible comparison without incorrectly rejecting valid date comparisons.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.