python
runtime_error
ai_generated
true
AssertionError: assert datetime.datetime(2024, 1, 1, 0, 0) == datetime.datetime(2025, 6, 1, 12, 0) # freezegun is active but the imported datetime is the real one
ID: python/pytest-freezegun-datetime-import
80%Fix Rate
87%Confidence
0Evidence
2025-02-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.5.x | active | — | — | — |
Root Cause
The code under test imported datetime as 'from datetime import datetime' before freezegun patched the module, so the test holds a reference to the unpatched class.
generic中文
被测代码在 freezegun patch 模块之前以 'from datetime import datetime' 方式导入了 datetime,导致测试持有未打补丁的类引用。
Workarounds
-
94% success
# BAD: from datetime import datetime # GOOD: import datetime then use datetime.datetime.now() # Test: from freezegun import freeze_time @freeze_time('2025-06-01 12:00:00') def test_x(): assert get_now() == datetime.datetime(2025, 6, 1, 12, 0) -
80% success
import importlib @freeze_time('2025-06-01') def test_x(): import myapp.clock importlib.reload(myapp.clock) assert myapp.clock.now().year == 2025
Dead Ends
Common approaches that don't work:
-
85% fail
tick only affects the patched clock; the unpatched datetime reference is unaffected and still returns real time.
-
92% fail
Timezone is not the issue; the datetime class itself is not the one freezegun patched.