Skip to content

can't compare datetime.datetime to datetime.date #340

@applecat

Description

@applecat

I have this line in my code:
if self.begin and datetime.now() < self.begin:

And some tests for that code:

@pytest.mark.parametrize("freeze_time, begin", [
    (datetime(2020, 3, 1), datetime(2020, 3, 3)),
    (datetime(2020, 3, 2, 23, 50), datetime(2020, 3, 3)),
])
def test_code(freezer, freeze_time, begin):
    freezer.move_to(freeze_time)
    ...

Tests passed, every thing is ok, but in production I've got this error:
Exception Value: can't compare datetime.datetime to datetime.date

I figured out that self.begin was a datetime.date object, so python can't compare it to datetime.datetime.

But in my tests I see comparison between FakeDatetime and FakeDate and everything goes well:

<class 'freezegun.api.FakeDatetime'> 2020-03-02 23:50:00
<class 'freezegun.api.FakeDate'> 2020-03-03

Is it expected behavior?
Can I catch this type of errors with freezegun?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions