question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make FakePathlibModule.Path compare equal to pathlib.Path

See original GitHub issue

Sometimes it happens that a test case compares a fake path to a real path - usually when you’ve created a pathlib.Path in pytest.mark.parametrize, like so:

def my_function():
    return pathlib.Path()

@pytest.mark.parametrize('expected_result', [pathlib.Path()])
def test_my_function(fs, expected_result):
    assert my_function() == expected_result

This fails with AssertionError: assert WindowsPath('.') == WindowsPath('.').

This could of course be avoided by passing in a string and converting it to a path inside the test, but in reality there isn’t always such a convenient workaround. Here’s a more realistic example, where an object with loads of attributes - some of which are Paths - is loaded from disk and compared to the expected result:

import pytest
from pathlib import Path
from my_module import MyClass

@pytest.mark.parametrize('attrs', [
    {'foo': False,
     'origin': Path(),
     'bar': 17,
     'paths': [Path('/'), Path('somewhere')]},
])
def test_serialization(fs, attrs):
    obj = MyClass(**attrs)
    obj.dump('myobj.dump')
    
    loaded_obj = MyClass.load_file('myobj.dump')
    assert vars(loaded_obj) == attrs

As I’m sure you can see, having to convert all of those attributes to Paths inside the test is rather annoying. For this reason it would be very convenient if fake Paths would compare equal to real Paths.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcgeheeivcommented, May 23, 2021

Great idea, @mrbean-bremen. Done.

1reaction
Aran-Feycommented, Apr 29, 2019

For now, I’ve worked around the problem by adding this monkey patch to my conftest.py:

# monkeypatch Path.__eq__ so that pyfakefs FakePaths compare equal to real pathlib.Paths
from pathlib import Path
from pyfakefs.fake_pathlib import FakePath

# Path somehow gets monkeypatched during testing, so in order to have access
# to the original class we'll simply create an instance of it
PATH = object.__new__(Path)

def path_eq(self, other):
    Path = type(PATH)

    if isinstance(other, (Path, FakePath)):
        return str(self) == str(other)

    return super(Path, self).__eq__(other)

Path.__eq__ = path_eq
Read more comments on GitHub >

github_iconTop Results From Across the Web

Make FakePathlibModule.Path compare equal to pathlib.Path
Sometimes it happens that a test case compares a fake path to a real path - usually when you've created a pathlib.
Read more >
pathlib — Object-oriented filesystem paths — Python 3.11.1 ...
Source code: Lib/pathlib.py This module offers classes representing filesystem paths with semantics appropriate for different operating systems.
Read more >
Python 3's pathlib Module: Taming the File System
In this tutorial, you will see how to work with file paths—names of directories and files—in Python. You will learn new ways to...
Read more >
How to get absolute path of a pathlib.Path object?
Use resolve(). Simply use Path.resolve() like this: p = p.resolve(). This makes your path absolute and replaces all relative parts with ...
Read more >
pyfakefs - Bountysource
When creating a directory by UNC path (like //host/share/dir) at least two functions on this path ... Make FakePathlibModule.Path compare equal to pathlib....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found