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.

Support pathlib.Path parameter in create_dir()

See original GitHub issue

I created a repository here to show the bug I am getting: https://github.com/shuttle1987/pyfakefs-pathlib-fail

Essentially when this test is run:

def test_pathlib(fs): #fs is the fake filesystem fixture
    """Attempt to use a pathlib path"""

    import pathlib
    base_dir = pathlib.Path('/tmp/corpus_data')
    fs.create_dir(base_dir)

    from example_lib.searcher import determine_labels

The failure is as follows:

$ python -m pytest
================================================ test session starts ================================================
platform linux -- Python 3.5.2, pytest-3.6.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/janis/pyfakefs-pathlib-fail, inifile:
plugins: cov-2.5.1, pyfakefs-3.4.1
collected 1 item                                                                                                    

tests/test_example.py F                                                                                       [100%]

===================================================== FAILURES ======================================================
___________________________________________________ test_pathlib ____________________________________________________

fs = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x7f03fc2b9a90>

>   ???

home/janis/pyfakefs-pathlib-fail/tests/test_example.py:6: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
home/janis/pyfakefs-pathlib-fail/venv/lib/python3.5/site-packages/pyfakefs/fake_filesystem.py:2137: in create_dir
    ???
home/janis/pyfakefs-pathlib-fail/venv/lib/python3.5/site-packages/pyfakefs/fake_filesystem.py:1362: in absnormpath
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x7f03fc2b9a90>, file_path = PosixPath('/tmp/corpus_data')

>   ???
E   AttributeError: 'PosixPath' object has no attribute 'startswith'

home/janis/pyfakefs-pathlib-fail/venv/lib/python3.5/site-packages/pyfakefs/fake_filesystem.py:1578: AttributeError
============================================= 1 failed in 0.12 seconds ==============================================

As far as I can tell from reading the documentation this library is supposed to patch pathlib.Path.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mrbean-bremencommented, May 31, 2018

Thanks for the report! Currently, create_dir is not designed to take a Path parameter, so you have to convert it to a string first, e.g. fs.create_dir (str (base_dir)). But I agree that it would make sense to support this - I will adapt it accordingly.

0reactions
shuttle1987commented, Jun 3, 2018

Thanks for the patch!

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 - explanation of pathlib.Path.mkdir mode argument
If mode is given, it is combined with the process' umask value to determine the file mode and access flags. import pathlib pathlib.Path("/tmp/ ......
Read more >
How to use the pathlib.Path.mkdir function in pathlib - Snyk
To help you get started, we've selected a few pathlib. ... path_pre = Path.cwd() path_tmp = Path(tempfile.mkdtemp()) path_test_dir = Path(__file__).parent ...
Read more >
Python Path – How to Use the Pathlib Module with Examples
According to the official documentation, the .mkdir() method takes three arguments. We will focus only at the moment on parents and exist_ok ....
Read more >
pathlib — Filesystem Paths as Objects — PyMOTW 3
pathlib includes classes for managing filesystem paths formatted using either the POSIX ... If the path already exists, mkdir() raises a FileExistsError ....
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