palantir / python-language-server

An implementation of the Language Server Protocol for Python
MIT License
2.61k stars 283 forks source link

"TypeError: 'PosixPath' object is not subscriptable" with latest jedi #841

Open craftyguy opened 4 years ago

craftyguy commented 4 years ago

With these two commits, jedi started using PosixPath objects instead of strings to represent paths:

https://github.com/davidhalter/jedi/commit/480a464179180c67bc7fc2ac61cd4a8ddb7a68d8 https://github.com/davidhalter/jedi/commit/db0e90763be0f65de1a03a270f10272b46184892

So any attempts to use str methods (e.g. endswith, startswith) and subscripting will fail now in PLS:

  File "/usr/lib/python3.8/site-packages/pyls/uris.py", line 110, in _normalize_win_path
    if path[:2] == '//':
TypeError: 'PosixPath' object is not subscriptable

I was able to resolve that above crash with a simple path = str(path) before it was subscripted, but I don't know if there are other areas in the PLS codebase that expect paths to be strings that would also need to be corrected.

Full traceback:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/pyls_jsonrpc/endpoint.py", line 113, in consume
    self._handle_request(message['id'], message['method'], message.get('params'))
  File "/usr/lib/python3.8/site-packages/pyls_jsonrpc/endpoint.py", line 182, in _handle_request
    handler_result = handler(params)
  File "/usr/lib/python3.8/site-packages/pyls_jsonrpc/dispatchers.py", line 23, in handler
    return method(**(params or {}))
  File "/usr/lib/python3.8/site-packages/pyls/python_ls.py", line 352, in m_text_document__references
    return self.references(textDocument['uri'], position, exclude_declaration)
  File "/usr/lib/python3.8/site-packages/pyls/python_ls.py", line 278, in references
    return flatten(self._hook(
  File "/usr/lib/python3.8/site-packages/pyls/python_ls.py", line 156, in _hook
    return hook_handlers(config=self.config, workspace=workspace, document=doc, **kwargs)
  File "/usr/lib/python3.8/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/usr/lib/python3.8/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/usr/lib/python3.8/site-packages/pluggy/manager.py", line 337, in traced_hookexec
    return outcome.get_result()
  File "/usr/lib/python3.8/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/usr/lib/python3.8/site-packages/pluggy/callers.py", line 52, in from_call
    result = func()
  File "/usr/lib/python3.8/site-packages/pluggy/manager.py", line 335, in <lambda>
    outcome = _Result.from_call(lambda: oldcall(hook, hook_impls, kwargs))
  File "/usr/lib/python3.8/site-packages/pluggy/manager.py", line 84, in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  File "/usr/lib/python3.8/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/usr/lib/python3.8/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/usr/lib/python3.8/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/usr/lib/python3.8/site-packages/pyls/plugins/references.py", line 18, in pyls_references
    return [{
  File "/usr/lib/python3.8/site-packages/pyls/plugins/references.py", line 19, in <listcomp>
    'uri': uris.uri_with(document.uri, path=d.module_path) if d.module_path else document.uri,
  File "/usr/lib/python3.8/site-packages/pyls/uris.py", line 88, in uri_with
    path, _netloc = _normalize_win_path(path)
  File "/usr/lib/python3.8/site-packages/pyls/uris.py", line 110, in _normalize_win_path
    if path[:2] == '//':
TypeError: 'PosixPath' object is not subscriptable
otreblan commented 4 years ago

I had a similar error when trying to go to a definition with coc.nvim.

[Trace - 22:16:25] Sending request 'textDocument/definition - (25)'.
Params: {
    "textDocument": {
        "uri": "file:///home/aru/Code/FetchCord/fetch_cord/out.py"
    },
    "position": {
        "line": 15,
        "character": 13
    }
}

[Trace - 22:16:25] Received response 'textDocument/definition - (25)' in 10ms. Request failed: AttributeError: 'PosixPath' object has no attribute 'endswith' (-32602).
Error data: {
    "traceback": [
        "  File \"/usr/lib/python3.8/site-packages/pyls_jsonrpc/endpoint.py\", line 113, in consume\n    self._handle_request(message['id'], message['method'], message.get('params'))\n",
        "  File \"/usr/lib/python3.8/site-packages/pyls_jsonrpc/endpoint.py\", line 182, in _handle_request\n    handler_result = handler(params)\n",
        "  File \"/usr/lib/python3.8/site-packages/pyls_jsonrpc/dispatchers.py\", line 23, in handler\n    return method(**(params or {}))\n",
        "  File \"/usr/lib/python3.8/site-packages/pyls/python_ls.py\", line 325, in m_text_document__definition\n    return self.definitions(textDocument['uri'], position)\n",
        "  File \"/usr/lib/python3.8/site-packages/pyls/python_ls.py\", line 247, in definitions\n    return flatten(self._hook('pyls_definitions', doc_uri, position=position))\n",
        "  File \"/usr/lib/python3.8/site-packages/pyls/python_ls.py\", line 156, in _hook\n    return hook_handlers(config=self.config, workspace=workspace, document=doc, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/hooks.py\", line 286, in __call__\n    return self._hookexec(self, self.get_hookimpls(), kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/manager.py\", line 93, in _hookexec\n    return self._inner_hookexec(hook, methods, kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/manager.py\", line 337, in traced_hookexec\n    return outcome.get_result()\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/callers.py\", line 80, in get_result\n    raise ex[1].with_traceback(ex[2])\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/callers.py\", line 52, in from_call\n    result = func()\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/manager.py\", line 335, in <lambda>\n    outcome = _Result.from_call(lambda: oldcall(hook, hook_impls, kwargs))\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/manager.py\", line 84, in <lambda>\n    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/callers.py\", line 208, in _multicall\n    return outcome.get_result()\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/callers.py\", line 80, in get_result\n    raise ex[1].with_traceback(ex[2])\n",
        "  File \"/usr/lib/python3.8/site-packages/pluggy/callers.py\", line 187, in _multicall\n    res = hook_impl.function(*args)\n",
        "  File \"/usr/lib/python3.8/site-packages/pyls/plugins/definition.py\", line 12, in pyls_definitions\n    definitions = document.jedi_script().goto(\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/api/helpers.py\", line 465, in wrapper\n    return func(self, line, column, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/api/__init__.py\", line 371, in goto\n    return self._goto(line, column, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/api/__init__.py\", line 396, in _goto\n    names = list(name.goto())\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/names.py\", line 203, in goto\n    values = infer_call_of_leaf(context, name, cut_own_trailer=True)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/helpers.py\", line 105, in infer_call_of_leaf\n    values = context.infer_node(base)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/context.py\", line 222, in infer_node\n    return infer_node(self, node)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 137, in infer_node\n    return _infer_node_if_inferred(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 150, in _infer_node_if_inferred\n    return _infer_node_cached(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/cache.py\", line 43, in wrapper\n    rv = function(obj, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 155, in _infer_node_cached\n    return _infer_node(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/debug.py\", line 82, in wrapper\n    return func(*args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 63, in wrapper\n    return func(context, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 165, in _infer_node\n    return infer_atom(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 290, in infer_atom\n    return context.py__getattribute__(atom, position=position)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/context.py\", line 75, in py__getattribute__\n    values = ValueSet.from_sets(name.infer() for name in names)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/common/value.py\", line 69, in from_sets\n    for set_ in sets:\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/context.py\", line 75, in <genexpr>\n    values = ValueSet.from_sets(name.infer() for name in names)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/names.py\", line 279, in infer\n    return tree_name_to_values(\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/plugins/__init__.py\", line 21, in wrapper\n    return built_functions[public_name](*args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/plugins/stdlib.py\", line 842, in wrapper\n    return func(inference_state, context, tree_name)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 709, in tree_name_to_values\n    types = infer_expr_stmt(context, node, tree_name)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 63, in wrapper\n    return func(context, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 365, in infer_expr_stmt\n    return _infer_expr_stmt(context, stmt, seek_name)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/debug.py\", line 82, in wrapper\n    return func(*args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 398, in _infer_expr_stmt\n    value_set = context.infer_node(rhs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/context.py\", line 222, in infer_node\n    return infer_node(self, node)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 137, in infer_node\n    return _infer_node_if_inferred(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 150, in _infer_node_if_inferred\n    return _infer_node_cached(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/cache.py\", line 43, in wrapper\n    rv = function(obj, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 155, in _infer_node_cached\n    return _infer_node(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/debug.py\", line 82, in wrapper\n    return func(*args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 63, in wrapper\n    return func(context, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 178, in _infer_node\n    value_set = context.infer_node(first_child)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/context.py\", line 222, in infer_node\n    return infer_node(self, node)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 137, in infer_node\n    return _infer_node_if_inferred(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 150, in _infer_node_if_inferred\n    return _infer_node_cached(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/cache.py\", line 43, in wrapper\n    rv = function(obj, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 155, in _infer_node_cached\n    return _infer_node(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/debug.py\", line 82, in wrapper\n    return func(*args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 63, in wrapper\n    return func(context, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 165, in _infer_node\n    return infer_atom(context, element)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 290, in infer_atom\n    return context.py__getattribute__(atom, position=position)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/context.py\", line 75, in py__getattribute__\n    values = ValueSet.from_sets(name.infer() for name in names)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/common/value.py\", line 69, in from_sets\n    for set_ in sets:\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/context.py\", line 75, in <genexpr>\n    values = ValueSet.from_sets(name.infer() for name in names)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/names.py\", line 279, in infer\n    return tree_name_to_values(\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/plugins/__init__.py\", line 21, in wrapper\n    return built_functions[public_name](*args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/plugins/stdlib.py\", line 842, in wrapper\n    return func(inference_state, context, tree_name)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/syntax_tree.py\", line 715, in tree_name_to_values\n    types = imports.infer_import(context, tree_name)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/cache.py\", line 43, in wrapper\n    rv = function(obj, *args, **kwargs)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/imports.py\", line 55, in infer_import\n    _prepare_infer_import(module_context, tree_name)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/imports.py\", line 115, in _prepare_infer_import\n    importer = Importer(module_context.inference_state, tuple(import_path),\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/imports.py\", line 169, in __init__\n    debug.speed('import %s %s' % (import_path, module_context))\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/context.py\", line 216, in __repr__\n    return '%s(%s)' % (self.__class__.__name__, self._value)\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/value/module.py\", line 228, in __repr__\n    self.is_stub()\n",
        "  File \"/usr/lib/python3.8/site-packages/jedi/inference/value/module.py\", line 159, in is_stub\n    if self._path is not None and self._path.endswith('.pyi'):\n"
    ]
}

[Error  - 22:16:25] Request textDocument/definition failed.
  Message: AttributeError: 'PosixPath' object has no attribute 'endswith'
  Code: -32602 
[object Object]
juergenhoetzel commented 4 years ago

I had a similar error when trying to go to a definition with coc.nvim.

  • Arch Linux.
  • python-jedi 0.17.0-1
  • python-language-server 0.34.1-2

I can confirm this issue on Arch Linux.

It's caused by a missing parso version restriction of jedi 0.17.0 and thus installing a incompatible version of python-parso

This version restriction was added in jedi-0.17.1:

parso>=0.7.0,<0.8.0

Unfortunately, if you are using the system package of Arch Linux, there is currently no other solution than to downgrading python-argo:

bash sudo pacman -U python-parso-0.7.1-1-any.pkg.tar.zst

worked for me.