Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

How to mock methods like `pathlib.Path.is_dir`?

+2
−0

I used to have some testing code for mocking a simple directory structure when working with pathlib. I recently tried to run these tests to learn that some of the internals in pathlib have changed in recent Python versions, breaking my mocking setup. I managed to resolve some of the issues already, but I got stuck on an issue that feels like it shouldn't be one.

Concretely, I have been mocking pathlib.Path.is_dir as follows:

from pathlib import Path
from unittest import mock

some_path = Path("/some/path/")
with mock.patch("pathlib.Path.is_dir") as m_is_dir:
    m_is_dir.side_effect = lambda p: p.name == ""
    print(Path.is_dir(some_path))  # this is just a placeholder

Now, the problem is that the code in newer Python versions (internally) seems to be using some_path.is_dir() instead. As a result, I get a TypeError about a missing positional argument p for my lambda expression. This can be simulated by the following code

with mock.patch("pathlib.Path.is_dir") as m_is_dir:
    m_is_dir.side_effect = lambda p: p.name == ""
    print(some_path.is_dir())  # this is just a placeholder

This can be fixed by rewriting the mocking function

with mock.patch("pathlib.Path.is_dir") as m_is_dir:
    m_is_dir.side_effect = lambda: some_path.name == ""
    print(some_path.is_dir())  # this is just a placeholder 

but of course that breaks the tests for the older versions again.

I have tried to patch some_path.is_dir directly, using

with mock.patch("pathlib.Path.is_dir") as m_is_dir:
    m_is_dir.side_effect = lambda p: p.name == ""
    some_path.is_dir = lambda: m_is_dir(some_path)
    print(some_path.is_dir())  # this is just a placeholder 

but that resulted in an AttributeError because is_dir is read-only.

I actually would have assumed that patching Path.is_dir(p) (indirectly) patch the bound method p.is_dir as well, but that does not seem to be the case. So that leaves me with the question: is there a robust way to patch methods that can be called either way? Or more concretely: how can I patch Path.is_dir so that the following works?

from pathlib import Path
from unittest import mock

some_path = Path("/some/path/")
with mock.patch(???) as m_is_dir:
    m_is_dir.side_effect = lambda p: p.name == ""
    print(Path.is_dir(some_path))
    print(some_path.is_dir())

It might be that there is an obvious solution, but I am in too deep and I don't seem to be able to find it, so any help would be greatly appreciated.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+0
−0

It turns out that this is exactly what autospeccing is for. Using

from pathlib import Path
from unittest import mock

some_path = Path("/some/path/")
with mock.patch("pathlib.Path.is_dir", autospec=True) as m_is_dir:
    m_is_dir.side_effect = lambda p: p.name == ""
    print(Path.is_dir(some_path))
    print(some_path.is_dir())

works as expected.

It was this link to an older version of the docs that put me on the right track.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »