time-machine icon indicating copy to clipboard operation
time-machine copied to clipboard

Add pytest-freezegun support to migration CLI

Open adamchainz opened this issue 4 months ago • 7 comments

Description

The migration CLI could potentially rewrite use of the freezer fixture and freeze_time marker from pytest-freezegun.

adamchainz avatar Aug 19 '25 14:08 adamchainz

The migration CLI could potentially rewrite

Which migration CLI are you referring to here? Are you referring to django-upgrade?

kingbuzzman avatar Aug 19 '25 14:08 kingbuzzman

https://time-machine.readthedocs.io/en/latest/migration.html#migration-cli

adamchainz avatar Aug 19 '25 15:08 adamchainz

🤯 NICE!

kingbuzzman avatar Aug 19 '25 15:08 kingbuzzman

Issue with this: thefreezer-gun pytest fixture is called freezer and the import is called freeze_gun, ruff doesn't complain about you reusing the name of the name. With time_machine we run into this issue.

we can rename the fixure tm, delorian, chrono_device, chrono, flux_capacitor, temporal_device.... what do you think?

import freezegun

def test_before_a():
    with freezegun.freeze_time(datetime.now() - timedelta(days=100)):
         ....

@pytest.mark.django_db
def test_before_b(freezer):
    freezer.move_to("2000-01-01")
    ....

currently:

import time_machine

def test_after_b():
    with time_machine.travel(datetime.now() - timedelta(days=100), tick=False):
         ....

@pytest.mark.django_db
def test_after_b(time_machine):
    time_machine.travel("2000-01-01", tick=False)
    ....

While I agree, this should not have happened in the first place, this causes friction at the moment.

kingbuzzman avatar Aug 22 '25 09:08 kingbuzzman

I don't want to rename the fixture solely to make it easier to write a migration CLI that works in all circumstances.

The CLI is a best-effort tool, aiming to be slightly better than find/replace. We should be fine with leaving some cases where it doesn't work great. In your example, the user can fix things up for the Ruff rule by moving import time_machine into the functions that use it.

I can keep the time_machine fixture name, and detect that freezer is being used AND also import freeze_gun and simply turn everything into a fixture... otherwise it will do the default behavior.. thoughts?

You're proposing rewriting tests from direct module usage to fixture usage? I wouldn't feel comfortable adding that, I think it would be too hard to guarantee correctness in all situations.

I think a basic fixture rewrite, even if it sometimes causes name collisions, will be fine.

adamchainz avatar Aug 26 '25 12:08 adamchainz

I could do: good enough then, convert 1-to-1, the linter could then fail, but that would be their problem to solve.

speaking from my own bias point of view though, i have a large code base that i'm using it as a guinea pig and i was trying to remove work for myself.. but yeah ill simply it

kingbuzzman avatar Aug 26 '25 12:08 kingbuzzman

Just noticed that this doesn't work, definitely would be helpful.

wyardley avatar Dec 13 '25 06:12 wyardley