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

Fix loading of fake modules for relative imports #1081

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,10 +1244,10 @@ def fake_module_path(self, name: str) -> str:
module_path = fs.joinpaths(path, base_path)
py_module_path = module_path + ".py"
if fs.exists(py_module_path):
return py_module_path
return fs.absnormpath(py_module_path)
init_path = fs.joinpaths(module_path, "__init__.py")
if fs.exists(init_path):
return init_path
return fs.absnormpath(init_path)
return ""

def find_spec(
Expand Down
9 changes: 9 additions & 0 deletions pyfakefs/tests/fake_filesystem_unittest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,15 @@ def test_fake_import_dotted_module(self):
assert module.__name__ == "fakepkg.fake_module"
assert module.number == 42

def test_fake_relative_import(self):
fake_module_path = Path("site-packages") / "fake_module.py"
self.fs.create_file(fake_module_path, contents="number = 42")
sys.path.insert(0, str(fake_module_path.parent))
module = importlib.import_module("fake_module")
del sys.path[0]
assert module.__name__ == "fake_module"
assert module.number == 42


if __name__ == "__main__":
unittest.main()