diff --git a/pylsp/plugins/symbols.py b/pylsp/plugins/symbols.py index 3a7beb07..1bf6b938 100644 --- a/pylsp/plugins/symbols.py +++ b/pylsp/plugins/symbols.py @@ -90,7 +90,11 @@ def pylsp_document_symbols(config, document): else: continue - if _include_def(d) and Path(document.path) == Path(d.module_path): + if ( + _include_def(d) + and d.module_path is not None + and Path(document.path) == Path(d.module_path) + ): tuple_range = _tuple_range(d) if tuple_range in exclude: continue diff --git a/test/plugins/test_symbols.py b/test/plugins/test_symbols.py index 242a38a1..bde45320 100644 --- a/test/plugins/test_symbols.py +++ b/test/plugins/test_symbols.py @@ -130,3 +130,39 @@ def test_symbols_all_scopes_with_jedi_environment(workspace) -> None: doc.update_config(settings) symbols = pylsp_document_symbols(doc._config, doc) helper_check_symbols_all_scope(symbols) + + +@pytest.mark.parametrize("all_scopes", [False, True]) +@pytest.mark.parametrize("include_import_symbols", [False, True]) +def test_symbols_imported_namedtuple( + config, temp_workspace_factory, all_scopes, include_import_symbols +): + workspace = temp_workspace_factory( + { + "__init__.py": "", + "b.py": ( + "from collections import namedtuple\n" + 'MyNamedTuple = namedtuple("MyNamedTuple", ["abc"])\n' + ), + "a.py": 'from .b import MyNamedTuple\na_symbol = "a_symbol"\n', + } + ) + config.update( + { + "plugins": { + "jedi_symbols": { + "all_scopes": all_scopes, + "include_import_symbols": include_import_symbols, + } + } + } + ) + uri = uris.from_fs_path(os.path.join(workspace.root_path, "a.py")) + symbols = pylsp_document_symbols(config, workspace.get_document(uri)) + + expected_names = ( + ["MyNamedTuple", "a_symbol"] if include_import_symbols else ["a_symbol"] + ) + assert [symbol["name"] for symbol in symbols] == expected_names + assert all(symbol["location"]["uri"] == uri for symbol in symbols) + assert symbols[-1]["location"]["range"]["start"] == {"line": 1, "character": 0}