Skip to content

A variable annotated with 'date' is not compatible with a value of type 'datetime'. #2487

Closed

Description

@mvaled

datetime is a subclass of date. In the stubs of 'datetime' there's the following TODO:

class datetime:
    # TODO: Is a subclass of date, but this would make some types incompatible.

Is there any record of the incompatibilities? Should we keep an issue open to address this TODO eventually?

Activity

  1. mvaled commented on Sep 26, 2018

    @mvaled
    ContributorAuthor

    The following program is rejected by mypy:

    from datetime import date, datetime
    
    class Date: ...
    class Datetime(Date): ...
    
    now: date = datetime.now()
    rightnow: Date = Datetime()
    
    assert isinstance(now, date)
    $ mypy bug.py 
    bug.py:6: error: Incompatible types in assignment (expression has type "datetime", variable has type "date")
    
  2. JelleZijlstra commented on Sep 26, 2018

    @JelleZijlstra
    Member

    There's probably some incompatible methods. Try modifying the stub to make datetime inherit from date and run tests/mypy_test.py.

    We could fix this by just # type: ignoreing the lines with any incompatible methods.

  3. added 2 commits that reference this issue on Sep 26, 2018
    6bacc45
    4723360
  4. mvaled commented on Sep 26, 2018

    @mvaled
    ContributorAuthor

    @JelleZijlstra I created a PR in #2488

  5. added a commit that references this issue on Oct 2, 2018
    1297caa
  6. added a commit that references this issue on Jan 23, 2019
    31f6f7c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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