【发布时间】:2022-07-01 05:23:01
【问题描述】:
在我的代码中,我测试config_location 是否是PosixPath 或None 的实例:
if isinstance(self.config_location, (PosixPath, type(None))):
...
这在使用普通 python 解释器运行时工作正常,但在我使用 pyfakefs 的 pytest 测试期间失败,因为 pyfakefs 对象的类型为 pyfakefs.fake_pathlib.FakePathlibModule.PosixPath。这里明显的解决方案是将 pyfakefs 导入到测试代码中,并将pyfakefs.fake_pathlib.FakePathlibModule.PosixPath 添加到 isinstance 元组中,但这种方法对我来说似乎很笨拙。
一定有更好的方法吗?
【问题讨论】:
-
检查 pyfakefs 的限制:jmcgeheeiv.github.io/pyfakefs/release/…
-
这取决于你的目标。如果您只想检查对象是否属于路径类型,则可以改为检查
PurePath- 两个PosixPath实现都源自于此。一般来说,使用isinstance不被认为是好的做法,但我不知道在你的情况下是否有更好的方法。
标签: python python-3.x pyfakefs