io.open
patch failing with numpy
's genfromtxt
#840
Replies: 4 comments 1 reply
-
Okay, root causing further, in return _file_openers[ext](found, mode=mode,
encoding=encoding, newline=newline)
In [1]: import io
In [2]: _file_openers[ext]
Out[2]: <function io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)>
In [3]: io.open
Out[3]: <bound method FakeIoModule.open of <pyfakefs.fake_io.FakeIoModule object at 0x115b7ac50>> So for some reason, Manual Patching This works, properly patching in import io
from unittest.mock import patch
expected_file_data = np.array([[1.0, 2.0], [3.0, 4.0]])
with patch("numpy.lib._datasource._file_openers", {None: io.open}):
file_data = np.genfromtxt(str(csv_file), delimiter=",", skip_header=True)
np.testing.assert_allclose(file_data, expected_file_data) |
Beta Was this translation helpful? Give feedback.
-
Okay, I renamed this issue to more reflect the root cause. I guess the new ask is for I am unsure if this is possible without investigating how |
Beta Was this translation helpful? Give feedback.
-
Thanks - I will have a look at this. May take some time, as there are a couple of other issues I want to address first... |
Beta Was this translation helpful? Give feedback.
-
Ok, I had a closer look, and found the problem. This is a common problem with global variables initialized before patching starts, in this case this happens while importing There is no really nice way to address this, but there are a couple of possibilities. def test_with_numpy_genfromtxt(thisdir_fs) -> None:
...
import numpy as np
file_data = np.genfromtxt(str(csv_file), delimiter=",", skip_header=True) This works if you make sure that Another possibility is to reload the problematic module: import importlib
@pytest.fixture(name="thisdir_fs")
def fixture_thisdir_fs(fs):
importlib.reload(np.lib._datasource)
fs.add_real_directory(THIS_DIRECTORY, read_only=False)
return fs This works, though you have to rely on an internal The same can also be done if using the modules_to_reload argument: from pyfakefs.fake_filesystem_unittest import Patcher
@pytest.fixture(name="thisdir_fs")
def fixture_thisdir_fs():
with Patcher(modules_to_reload=[np.lib._datasource]) as p:
p.fs.add_real_directory(THIS_DIRECTORY, read_only=False)
yield p.fs which would achieve the same effect. A somewhat similar, but more specific version would be to patch the file openers directly: from unittest import mock
@pytest.fixture(name="thisdir_fs")
def fixture_thisdir_fs(fs):
with mock.patch(f"{__name__}.np.lib._datasource._file_openers") as fo:
fo._file_openers = {None: io.open}
fs.add_real_directory(THIS_DIRECTORY, read_only=False)
yield fs though this depends on even more internals. I'll move this to discussions, as this is something that could happen elsewhere. |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
It seems
numpy.genfromtxt
is failing withpyfakefs
, it's throwing aFileNotFoundError
despite the file actually being present.How To Reproduce
Running this test will yield the following
FileNotFoundError
:Your environment
Beta Was this translation helpful? Give feedback.
All reactions