Skip to content

textDocument/documentSymbol causes pylsp_document_symbols to throw a TypeError when there is an import for a namedtuple type #635

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

Open
ksmai opened this issue Apr 26, 2025 · 0 comments

Comments

@ksmai
Copy link

ksmai commented Apr 26, 2025

When a python module A imports a namedtuple type from another module B, calling textDocument/documentSymbol on A will cause pylsp_document_symbols to throw a TypeError:

TypeError: argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'NoneType'

Steps to reproduce

  1. Set up a python workspace with the files a.py and b.py as follows

    a.py
    from .b import MyNamedTuple
    
    a_symbol = "a_symbol"
    b.py
    from collections import namedtuple
    
    MyNamedTuple = namedtuple("MyNamedTuple", ["abc"])
    
    b_symbol = "b_symbol"
  2. Start pylsp with pylsp --verbose

  3. Send these two messages to pylsp

    LSP messages
    {"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"workspaceFolders": [{"uri": "/home/ksmai/dev/pylsp-repro-namedtuple", "name": "test"}]}}
    {"jsonrpc": "2.0", "id": 2, "method": "textDocument/documentSymbol", "params": {"textDocument": {"uri": "/home/ksmai/dev/pylsp-repro-namedtuple/a.py"}}}

Actual result

An error occurs and no results are returned

Error logs
WARNING - pylsp.config.config - Failed to load hook pylsp_document_symbols: argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'NoneType'
Traceback (most recent call last):
  File "/home/ksmai/dev/pylsp-repro-namedtuple/.venv/lib/python3.12/site-packages/pylsp/config/config.py", line 39, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksmai/dev/pylsp-repro-namedtuple/.venv/lib/python3.12/site-packages/pluggy/_manager.py", line 480, in traced_hookexec
    return outcome.get_result()
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/ksmai/dev/pylsp-repro-namedtuple/.venv/lib/python3.12/site-packages/pluggy/_result.py", line 100, in get_result
    raise exc.with_traceback(exc.__traceback__)
  File "/home/ksmai/dev/pylsp-repro-namedtuple/.venv/lib/python3.12/site-packages/pluggy/_result.py", line 62, in from_call
    result = func()
             ^^^^^^
  File "/home/ksmai/dev/pylsp-repro-namedtuple/.venv/lib/python3.12/site-packages/pluggy/_manager.py", line 477, in <lambda>
    lambda: oldcall(hook_name, hook_impls, caller_kwargs, firstresult)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksmai/dev/pylsp-repro-namedtuple/.venv/lib/python3.12/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/home/ksmai/dev/pylsp-repro-namedtuple/.venv/lib/python3.12/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ksmai/dev/pylsp-repro-namedtuple/.venv/lib/python3.12/site-packages/pylsp/plugins/symbols.py", line 88, in pylsp_document_symbols
    if _include_def(d) and Path(document.path) == Path(d.module_path):
                                                  ^^^^^^^^^^^^^^^^^^^
  File "/home/ksmai/.local/share/uv/python/cpython-3.12.9-linux-x86_64-gnu/lib/python3.12/pathlib.py", line 1162, in __init__
    super().__init__(*args)
  File "/home/ksmai/.local/share/uv/python/cpython-3.12.9-linux-x86_64-gnu/lib/python3.12/pathlib.py", line 373, in __init__
    raise TypeError(
TypeError: argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'NoneType'
LSP response
{"jsonrpc":"2.0","id":2,"result":[]}

Expected result

pylsp should return a_symbol

LSP response
{"jsonrpc":"2.0","id":2,"result":[{"name":"MyNameTuple","containerName":null,"location":{"uri":"\/home\/ksmai\/dev\/pylsp-repro-namedtuple\/a.py","range":{"start":{"line":0,"character":0},"end":{"line":0,"character":27}}},"kind":5},{"name":"a_symbol","containerName":null,"location":{"uri":"\/home\/ksmai\/dev\/pylsp-repro-namedtuple\/a.py","range":{"start":{"line":2,"character":0},"end":{"line":2,"character":21}}},"kind":13}]}

Additional information

If a.py imports b_symbol instead of MyNamedTuple from b.py, pylsp will correctly return a_symbol in its response. In addition, textDocument/documentSymbol works correctly in b.py regardless of what is being imported into a.py.

The full code for reproducing the issue can also be found in this repository.

python version: 3.12.9
pylsp version: v1.12.2

@ksmai ksmai changed the title pylsp_document_symbols fails when the document imports a namedtuple from another module pylsp_document_symbols throws type error when there is an import for a namedtuple type Apr 27, 2025
@ksmai ksmai changed the title pylsp_document_symbols throws type error when there is an import for a namedtuple type pylsp_document_symbols throws TypeError when there is an import for a namedtuple type Apr 27, 2025
@ksmai ksmai changed the title pylsp_document_symbols throws TypeError when there is an import for a namedtuple type textDocument/documentSymbol causes pylsp_document_symbols to throw a TypeError when there is an import for a namedtuple type Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant