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

Patch os.DirEntry #533

Closed
Aran-Fey opened this issue Jul 2, 2020 · 4 comments
Closed

Patch os.DirEntry #533

Aran-Fey opened this issue Jul 2, 2020 · 4 comments

Comments

@Aran-Fey
Copy link

Aran-Fey commented Jul 2, 2020

Is your feature request related to a problem? Please describe.
pyfakefs doesn't patch os.DirEntry, which has the unfortunate side effect that os.scandir yields objects for which the check isinstance(direntry, os.DirEntry) fails.

Describe the solution you'd like
os.DirEntry should be replaced with pyfakefs.fake_scandir.DirEntry as long as pyfakefs is active.

Describe alternatives you've considered
...none?

Code snippet that demonstrates the problem:

import os
import pytest

def test_os_direntry_is_patched(fs):
    for direntry in os.scandir():
        assert isinstance(direntry, os.DirEntry)

if __name__ == '__main__':
    pytest.main([__file__])

# >           assert isinstance(direntry, os.DirEntry)
# E           AssertionError: assert False
# E            +  where False = isinstance(<pyfakefs.fake_scandir.DirEntry object at 0x7fac732f6ca0>, <class 'posix.DirEntry'>)
# E            +    where <class 'posix.DirEntry'> = os.DirEntry
@mrbean-bremen
Copy link
Member

There is currently no straightforward way to add this in pyfakefs (the patching works not as in mock.patch), though I may think about this. I'm curious, though - what is your use case here? I understand that it can make sense to differentiate between text and binary streams, as in another issue you wrote, by why check the type of a DirEntry?

@Aran-Fey
Copy link
Author

Aran-Fey commented Jul 3, 2020

Well, essentially I have a bunch of functions that accept path-like objects as input - and os.DirEntry implements that interface, but only starting with python 3.6. So for backwards compatibility with 3.5, DirEntry objects need special treatment, which relies on an isinstance check.

It's certainly not a high-priority issue, but it'd be nice if my test suite didn't have to do so much manual monkeypatching :)

I don't understand why this isn't an easy fix, though? Other path-related functions and classes like os.getcwd and pathlib.Path are patched with implementations from pyfakefs, right? Doing the same thing for os.DirEntry seems straightforward.

@mrbean-bremen
Copy link
Member

Ah, ok, but in that case it may be better to check if it has an __fspath__ attribute.

As for the other attributes - for patched functions this isn't an issue, and for pathlib.Path you will find that isinstance(pathlib.Path) also fails. Until now, this never came up - as I said, I will have a look later, maybe it is easy to change.

@mrbean-bremen
Copy link
Member

I did not succeed in fixing this (had several tries) - closing now, as this is a documented limitation in pyfakefs.

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

2 participants