pytest-dev / pytest-dev/pyfakefs

`Path` is replaced with `FakePathlibPathModule` instead of `FakePath` when imported directly

Open
#1,334 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
750
Forks
99
Avg merge
19h 6m
Merged PRs (30d)
6

Description

Bug description

  • The fs fixture replaces Path with pyfakefs.fake_pathlib.FakePathlibPathModule instead of pyfakefs.fake_pathlib.FakePath
  • This is reproduced when the individual Path class is imported directly from pathlib
  • pytest error messages erroneously report Path as FakePath instead of FakePathlibPathModule, hindering debugging efforts
  • When importing by module, the qualified name pathlib.Path is 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 Path has been replaced with FakePath
  • Actual: test_with_class_import fails:
$ 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

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.