0

In my code, I test if config_location is an instance of PosixPath or None:

if isinstance(self.config_location, (PosixPath, type(None))):
    ...

This works fine when run with the normal python interpreter, but fails during my pytest tests using pyfakefs, as the pyfakefs object is of type pyfakefs.fake_pathlib.FakePathlibModule.PosixPath. The obvious solution here would be to import pyfakefs into the tested code and add pyfakefs.fake_pathlib.FakePathlibModule.PosixPath to the isinstance tuple, but this approach seems very clunky to me.

There has to be a better way ?

  • Check the limitations for pyfakefs: http://jmcgeheeiv.github.io/pyfakefs/release/intro.html#:~:text=the%20actual%20user.-,Limitations,-%C2%B6 – Liron Berger May 21 '22 at 15:27
  • It depends on your goal. If you just want to check if the object is of a path type, you could check for `PurePath` instead - both `PosixPath` implementations derive from that. Generally it is not considered good practice to use `isinstance`, but I don't know if there is a better way in your case. – MrBean Bremen May 21 '22 at 16:39

1 Answers1

0

Testing for PurePath instead works.