模拟日期时间会在 pyarrow 中产生错误

Mocking datetime yields bug in pyarrow

为了测试,我想像 here 一样模拟 datetime.datetime.now()

我正在测试的功能是通过 pyarrow 读取 table:

import pytest
import pyarrow.parquet as pq
import datetime

mockdate = datetime.datetime(2000, 1, 1, 0, 0, 0)

@pytest.fixture(autouse=True)
def patch_datetime_now(monkeypatch):
    class mydatetime:
        @classmethod
        def now(cls):
            return mockdate

    monkeypatch.setattr(datetime, 'datetime', mydatetime)
    
def function_to_test():
    date = datetime.datetime.now()
    some_table = pq.read_table("abc/def")
    return some_table, date
    
def test_function_to_test():
    
    table, date = function_to_test()
    
    assert date == mockdate

出于某种原因,模拟 now() 函数会在 pyarrow 中产生这个非常奇怪的错误:

ValueError: datetime.datetime size changed, may indicate binary incompatibility. Expected 48 from C header, got 32 from PyObject

知道如何解决这个问题吗?谢谢!

感谢 chepner 的提示。为了将来参考,这是一个可行的解决方案:

import pytest
import pyarrow.parquet as pq
import datetime

mockdate = datetime.datetime(2000, 1, 1, 0, 0, 0)
    
def function_to_test():
    date = datetime.datetime.now()
    some_table = pq.read_table("abc/def")
    return some_table, date
    
def test_function_to_test(mocker):

    mocker.patch(__name__  + '.datetime')
    datetime.datetime.now.return_value = mockdate
    
    table, date = function_to_test()
    
    assert date == mockdate