pathlib.py:在Windows上实例化“ PosixPath”

发布于 2021-01-29 16:42:25

从其存储库中克隆了 thefuck
源代码(这是真实的项目名称,是的,我知道…)。尝试通过以下命令将其安装以进行开发:

pip install -r requirements.txt
python setup.py develop

工作正常。之后尝试通过以下方式运行测试:

py.test

出现以下错误:

================================== FAILURES ===================================
_______________________________ test_load_rule ________________________________

mocker = <pytest_mock.MockFixture object at 0x03275A10>

    def test_load_rule(mocker):
        match = object()
        get_new_command = object()
        load_source = mocker.patch(
            'thefuck.main.load_source',
            return_value=Mock(match=match,
                              get_new_command=get_new_command,
                              enabled_by_default=True,
                              priority=900))
        assert main.load_rule(Path('/rules/bash.py')) \
               == Rule('bash', match, get_new_command, priority=900)
>       load_source.assert_called_once_with('bash', '/rules/bash.py')

tests\test_main.py:20:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
C:\Python34\lib\unittest\mock.py:782: in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

_mock_self = <MagicMock name='load_source' id='52977776'>
args = ('bash', '/rules/bash.py'), kwargs = {}
expected = (('bash', '/rules/bash.py'), {})
_error_message = <function NonCallableMock.assert_called_with.<locals>._error_me
ssage at 0x032A1030>
actual = call('bash', '\\rules\\bash.py'), cause = None

    def assert_called_with(_mock_self, *args, **kwargs):
        """assert that the mock was called with the specified arguments.

            Raises an AssertionError if the args and keyword args passed in are
            different to the last call to the mock."""
        self = _mock_self
        if self.call_args is None:
            expected = self._format_mock_call_signature(args, kwargs)
            raise AssertionError('Expected call: %s\nNot called' % (expected,))

        def _error_message():
            msg = self._format_mock_failure_message(args, kwargs)
            return msg
        expected = self._call_matcher((args, kwargs))
        actual = self._call_matcher(self.call_args)
        if expected != actual:
            cause = expected if isinstance(expected, Exception) else None
>           raise AssertionError(_error_message()) from cause
E           AssertionError: Expected call: load_source('bash', '/rules/bash.py')

E           Actual call: load_source('bash', '\\rules\\bash.py')

C:\Python34\lib\unittest\mock.py:771: AssertionError
__________________ TestGetRules.test_get[conf_rules0-rules0] __________________

self = <tests.test_main.TestGetRules object at 0x03286F50>
monkeypatch = <_pytest.monkeypatch.monkeypatch object at 0x03286CD0>
glob = <MagicMock name='glob' id='52981200'>, conf_rules = []
rules = ['bash', 'lisp', 'bash', 'lisp']

    @pytest.mark.parametrize('conf_rules, rules', [
        (conf.DEFAULT_RULES, ['bash', 'lisp', 'bash', 'lisp']),
        (types.RulesNamesList(['bash']), ['bash', 'bash'])])
    def test_get(self, monkeypatch, glob, conf_rules, rules):
>       glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]

tests\test_main.py:35:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'pathlib.PosixPath'>, args = ('bash.py',), kwargs = {}
self = PosixPath('bash.py')

    def __new__(cls, *args, **kwargs):
        if cls is Path:
            cls = WindowsPath if os.name == 'nt' else PosixPath
        self = cls._from_parts(args, init=False)
        if not self._flavour.is_supported:
            raise NotImplementedError("cannot instantiate %r on your system"
>                                     % (cls.__name__,))
E           NotImplementedError: cannot instantiate 'PosixPath' on your system

C:\Python34\lib\site-packages\pathlib-1.0.1-py3.4.egg\pathlib.py:939: NotImpleme
ntedError
__________________ TestGetRules.test_get[conf_rules1-rules1] __________________

self = <tests.test_main.TestGetRules object at 0x032BAB50>
monkeypatch = <_pytest.monkeypatch.monkeypatch object at 0x03275A30>
glob = <MagicMock name='glob' id='53194576'>, conf_rules = ['bash']
rules = ['bash', 'bash']

    @pytest.mark.parametrize('conf_rules, rules', [
        (conf.DEFAULT_RULES, ['bash', 'lisp', 'bash', 'lisp']),
        (types.RulesNamesList(['bash']), ['bash', 'bash'])])
    def test_get(self, monkeypatch, glob, conf_rules, rules):
>       glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]

tests\test_main.py:35:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

cls = <class 'pathlib.PosixPath'>, args = ('bash.py',), kwargs = {}
self = PosixPath('bash.py')

    def __new__(cls, *args, **kwargs):
        if cls is Path:
            cls = WindowsPath if os.name == 'nt' else PosixPath
        self = cls._from_parts(args, init=False)
        if not self._flavour.is_supported:
            raise NotImplementedError("cannot instantiate %r on your system"
>                                     % (cls.__name__,))
E           NotImplementedError: cannot instantiate 'PosixPath' on your system

C:\Python34\lib\site-packages\pathlib-1.0.1-py3.4.egg\pathlib.py:939: NotImpleme
ntedError
______________________________ test_side_effect _______________________________

ssh_error = ('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n@
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!  ...own_hosts', <function ssh_e
rror.<locals>.reset at 0x033F16F0>, <function ssh_error.<locals>.known_hosts at
0x033F1198>)

    def test_side_effect(ssh_error):
        errormsg, path, reset, known_hosts = ssh_error
        command = Command('ssh user@host', stderr=errormsg)
        side_effect(command, None)
        expected = ['123.234.567.890 asdjkasjdakjsd\n', '111.222.333.444 qwepoiw
qepoiss\n']
>       assert known_hosts(path) == expected
E       assert ['123.234.567...oiwqepoiss\n'] == ['123.234.567....oiwqepoiss\n']

E         At index 1 diff: '98.765.432.321 ejioweojwejrosj\n' != '111.222.333.44
4 qwepoiwqepoiss\n'
E         Left contains more items, first extra item: '111.222.333.444 qwepoiwqe
poiss\n'
E         Use -v to get the full diff

tests\rules\test_ssh_known_host.py:61: AssertionError
=============== 4 failed, 122 passed, 2 skipped in 0.56 seconds ===============

C:\code\thefuck>

我怀疑该工具仅适用于Linux和类似Unix的命令行。

  • 此错误的原因是什么?
  • 如果它是python库的一部分,而该库尚未为Windows实施,那么是否有任何快速修复方法?

我想开始从事开源项目,以成为更好的开发人员。使用Linux完成我的开发任务不是问题。如果我知道更多有经验的开发人员对此的意见,将会对我有帮助。

关注者
0
被浏览
45
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    此错误的原因是,在WindowsPosixPath上未实现。但是这里有PurePosixPath,您可以在想要PosixPath用于平台独立处理POSIX路径的任何地方使用它。或者,实际上您可能希望拥有依赖于平台的路径(/在Linux和\Windows上),在这种情况下,您应该使用Path

    文档(继承图下方):

    在某些特殊情况下,纯路径很有用。例如:

    1.
    如果要在Unix计算机上操作Windows路径(反之亦然)。在Unix上运行时,无法实例化WindowsPath,但是可以实例化PureWindowsPath。




知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看