Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

freeze_time not working, if module name starts with "git" #484

Open
the-working-rene opened this issue Nov 29, 2022 · 1 comment
Open

freeze_time not working, if module name starts with "git" #484

the-working-rene opened this issue Nov 29, 2022 · 1 comment

Comments

@the-working-rene
Copy link

The @time_freeze decorator does not work, when a module name starts with git and the test imports from that module.
Renaming the module (i.e. the folder and imports) to any string, which does not contains those three letters at the beginning, works.

I could reproduce the error with Python 3.10, see this minmal example.

Minimal Example Code

Only freezegun as external dependency

git_module_name/minimal.py,

from datetime import datetime as dt


def what_date_is_it_right_now():
    return dt.now()

other_module_name/minimal.py

(exactly the same content in both)

from datetime import datetime as dt


def what_date_is_it_right_now():
    return dt.now()

test/test_minimal.py

from datetime import datetime
from unittest import TestCase

from freezegun import freeze_time

from git_module_name.mininmal import what_date_is_it_right_now as what_date_is_it_right_now_git
from other_module_name.mininmal import what_date_is_it_right_now as what_date_is_it_right_now_other


class TimeTest(TestCase):
    @freeze_time("2001-02-03")
    def test_what_date_is_it_right_now_git(self):
        returned_time = what_date_is_it_right_now_git()
        self.assertEqual(returned_time, datetime.now())

    @freeze_time("2001-02-03")
    def test_what_date_is_it_right_now_other(self):
        returned_time = what_date_is_it_right_now_other()
        self.assertEqual(returned_time, datetime.now())

Test Result

Running python -m unittest tests.test_minimal.TimeTest results in the first test failing and the second one passing:

F. 
======================================================================
FAIL: test_what_date_is_it_right_now_git (tests.test_minimal.TimeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\git\review\freezegun_minimal_example\.venv\lib\site-packages\freezegun\api.py", line 809, in wrapper
    result = func(*args, **kwargs)
  File "E:\git\review\freezegun_minimal_example\tests\test_minimal.py", line 14, in test_what_date_is_it_right_now_git
    self.assertEqual(returned_time, datetime.now())
AssertionError: datetime.datetime(2022, 11, 29, 11, 25, 35, 237124) != FakeDatetime(2001, 2, 3, 0, 0)

----------------------------------------------------------------------
Ran 2 tests in 0.073s

FAILED (failures=1)
@wimglenn
Copy link

wimglenn commented Mar 3, 2024

I think this is because the string "gi" is in the default ignores list and the check in the ignore lists is pretty dumb, using str.startswith. Probably any module name which starts with "gi" will fail similarly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants