pytest-dev / pytest-dev/pyfakefs
`Path` is replaced with `FakePathlibPathModule` instead of `FakePath` when imported directly
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 750
- Forks
- 99
- Avg merge
- 19h 6m
- Merged PRs (30d)
- 6
Description
Bug description
- The
fsfixture replacesPathwithpyfakefs.fake_pathlib.FakePathlibPathModuleinstead ofpyfakefs.fake_pathlib.FakePath - This is reproduced when the individual
Pathclass is imported directly frompathlib pytesterror messages erroneously reportPathasFakePathinstead ofFakePathlibPathModule, hindering debugging efforts- When importing by module, the qualified name
pathlib.Pathis replaced as expected
How To Reproduce
Below is a test case to reproduce the issue:
#!/usr/bin/env python3
import pathlib
from pathlib import Path
import pytest
from pyfakefs import fake_pathlib
from pyfakefs.fake_pathlib import FakePath
def test_with_class_import(request: pytest.FixtureRequest):
print("\nPath before patching:", Path)
_ = request.getfixturevalue("fs")
print("Path after patching:", Path)
assert Path is fake_pathlib.FakePath
def test_with_module_import(request: pytest.FixtureRequest):
print("\npathlib.Path before patching:", pathlib.Path)
_ = request.getfixturevalue("fs")
print("pathlib.Path after patching:", pathlib.Path)
assert FakePath is pathlib.Path
assert fake_pathlib.FakePath is pathlib.Path
- Expected: both tests succeed, because
Pathhas been replaced withFakePath - Actual:
test_with_class_importfails:
$ pytest -v
====================================== FAILURES ======================================
_______________________________ test_with_class_import _______________________________
request = <FixtureRequest for <Function test_with_class_import>>
def test_with_class_import(request: pytest.FixtureRequest):
print("\nPath before patching:", Path)
_ = request.getfixturevalue("fs")
print("Path after patching:", Path)
> assert Path is fake_pathlib.FakePath
E AssertionError: assert Path is <class 'pyfakefs.fake_pathlib.FakePath'>
E + where <class 'pyfakefs.fake_pathlib.FakePath'> = fake_pathlib.FakePath
What is more, the error message itself does not reflect the actual nature of Path:
- Expected:
where <module 'pyfakefs.fake_pathlib.FakePathlibPathModule'> = fake_pathlib.FakePath - Actual:
where <class 'pyfakefs.fake_pathlib.FakePath'> = fake_pathlib.FakePath
The console output reveals the type of Path:
$ pytest -s
test_pyfakefs.py::test_with_class_import
Path before patching: <class 'pathlib.Path'>
Path after patching: <pyfakefs.fake_pathlib.FakePathlibPathModule object at 0x7faec5bbbcb0>
FAILED
test_pyfakefs.py::test_with_module_import
pathlib.Path before patching: <class 'pathlib.Path'>
pathlib.Path after patching: <class 'pyfakefs.fake_pathlib.FakePath'>
PASSED
Notes
- Importing individual classes is discouraged by §2.2 of the Google python style guide.
- This behavior was observed when testing a
typerCLI withpyfakefs; specifically, anannotation == Pathcheck fails.
Environment
- Linux-7.1.3+deb14-amd64-x86_64-with-glibc2.43
- Python 3.14.6 (main, Jun 23 2026, 15:18:23) [Clang 22.1.3 ]
- pyfakefs 6.2.0
- pytest 9.1.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the fs fixture and the pathlib replacements in pyfakefs.fake_pathlib; reproduce both direct-class and module imports from the supplied tests. Trace why the direct Path reference receives FakePathlibPathModule, then add regression coverage showing both imports resolve consistently and pytest reports the actual replacement type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 70/100