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

FakeShutilModule.disk_usage() does not accept Path objects, in contrast to shutil.disk_usage() #699

Closed
spezold opened this issue Aug 8, 2022 · 3 comments · Fixed by #700
Labels

Comments

@spezold
Copy link

spezold commented Aug 8, 2022

An unpached shutil.disk_usage() function happily accepts instances of pathlib.Path for its path parameter. However, with pyfakefs, this does not seem to work; at least on my system (Windows 10, Python 3.7.9 with pyfakefs 4.6.3 and Python 3.10.5 with pyfakefs 4.5.6). Here is a minimum working example:

import shutil
from pathlib import Path
from unittest import main

from pyfakefs.fake_filesystem_unittest import TestCase


class MyTestCase(TestCase):
    
    def setUp(self):
        print("before", shutil.disk_usage(Path(".")))  # Works fine (i.e. prints usage tuple)
        self.setUpPyfakefs()
        print("after, str", shutil.disk_usage("."))  # Still works fine (i.e. prints usage tuple)
        print("after, Path", shutil.disk_usage(Path(".")))  # raises an error
        
    def test_disk_usage(self):
        pass
        
        
if __name__ == "__main__":
    main()
  • With Python 3.7.9, pyfakefs 4.6.3, I get: TypeError: 'WindowsPath' object is not subscriptable
  • With Python 3.10.5, pyfakefs 4.5.6, I get: AttributeError: 'WindowsPath' object has no attribute 'startswith'

From what I saw, both errors seem to result from the fact that FakeShutilModule.disk_usage() simply passes its path parameter to filesystem.get_disk_usage(). This, in my case, is a FakeFilesystem instance, whose get_disk_usage() explicitly seems to accept strings only for its path parameter.

Is this an error? Or am I doing something wrong here?

@mrbean-bremen
Copy link
Member

Thanks - yes, this is an error. Usually it is documented if a Path object can be passed, but for disk_usage they seem to have forgotten that. I just tested, this has been introduced in Python 3.7.

@mrbean-bremen
Copy link
Member

@spezold - should be fixed in master now, please check!

@spezold
Copy link
Author

spezold commented Aug 9, 2022

@mrbean-bremen Wow, thanks a lot for your rapid response. I can confirm that it works (just checked with pyfakefs's current master on my Python 3.10 setup)

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

Successfully merging a pull request may close this issue.

2 participants