Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pathlib2 #408

Closed
sk- opened this issue May 30, 2018 · 7 comments
Closed

Support pathlib2 #408

sk- opened this issue May 30, 2018 · 7 comments

Comments

@sk-
Copy link

sk- commented May 30, 2018

First of all thanks for this project, it's really useful. However, it'd be great if there was support for pathlib2.

In my code I'm importing it as import pathlib2 as pathlib. Which gets patched by pyfakefs, however the patch uses the system pathlib, which means that in my case it only works for Python 3.5 and 3.6, as I'm using the exist_ok argument of mkdir. In Python 3.4 I get the following error:

TypeError: mkdir() got an unexpected keyword argument 'exist_ok' 

And in Python 2.7 the module is not patched at all.

I already managed to get this to work in Python 2.7 (and probably in 3.4),

In my test I had to add

def setUp():
  from pyfakefs import fake_pathlib
  self.modules_to_patch = {'pathlib': fake_pathlib.FakePathlibModule}
  self.setUpPyfakefs()

and I had to change fake_pathlib.py as following

try:
  import pathlib2 as pathlib
except ImportError:
  import pathlib
try:
  from urllib.parse import quote_from_bytes as urlquote_from_bytes
except ImportError:
  from urllib import quote as urlquote_from_bytes

Let me know if you would be interested in accepting this change, so I could prepare a patch.

@mrbean-bremen
Copy link
Member

mrbean-bremen commented May 30, 2018

Sure, that sounds helpful. I actually wasn't aware of pathlib2, but we did something similar with the scandir module which is a backport of scandir to Python < 3.5.
And nice to know that modules_to_patch has been used by someone :)

@matthew16550
Copy link
Contributor

There is another difficulty using pathlib2 with Python 2.7, the version checking in FakePath.resolve() prevents using strict=False.

e.g.

def test_resolve(fs):
    Path('foo').resolve(False)

Causes:
TypeError: resolve() got an unexpected keyword argument 'strict'

@mrbean-bremen
Copy link
Member

Yes, the version checking has been made for pathlib only - the checks have to be changed for pathlib2 support.
@sk- - are you still working on the PR? Do you need help?

@sk-
Copy link
Author

sk- commented Aug 2, 2018

@mrbean-bremen sorry I don't have the time to prepare the PR. Feel free to use any of the code snippets if they are of any help.

@matthew16550
Copy link
Contributor

@mrbean-bremen I probably have time this weekend if you would like me to make a PR?

@mrbean-bremen
Copy link
Member

@sk- no problem, thanks!
@matthew16550 - thanks for the offer - sure, we always welcome contributions! If you've got the time, please go ahead!

jmcgeheeiv pushed a commit that referenced this issue Sep 3, 2018
@mrbean-bremen
Copy link
Member

Implemented in #422.
Thanks for your contribution and your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants