Skip to content

Commit 46059ef

Browse files
committed
Skip pathless definitions when collecting document symbols
1 parent a362006 commit 46059ef

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎pylsp/plugins/symbols.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ def pylsp_document_symbols(config, document):
9090
else:
9191
continue
9292

93-
if _include_def(d) and Path(document.path) == Path(d.module_path):
93+
if (
94+
_include_def(d)
95+
and d.module_path is not None
96+
and Path(document.path) == Path(d.module_path)
97+
):
9498
tuple_range = _tuple_range(d)
9599
if tuple_range in exclude:
96100
continue

‎test/plugins/test_symbols.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,44 @@ def test_symbols_non_existing_file(config, workspace, tmpdir) -> None:
118118
helper_check_symbols_all_scope(symbols)
119119

120120

121+
@pytest.mark.parametrize("all_scopes", [False, True])
122+
@pytest.mark.parametrize("include_import_symbols", [False, True])
123+
def test_symbols_imported_namedtuple(
124+
config, temp_workspace_factory, all_scopes, include_import_symbols
125+
) -> None:
126+
workspace = temp_workspace_factory(
127+
{
128+
"__init__.py": "",
129+
"a.py": 'from .b import MyNamedTuple\na_symbol = "a_symbol"\n',
130+
"b.py": (
131+
"from collections import namedtuple\n"
132+
'MyNamedTuple = namedtuple("MyNamedTuple", ["abc"])\n'
133+
),
134+
}
135+
)
136+
doc = workspace.get_document(
137+
uris.from_fs_path(os.path.join(workspace.root_path, "a.py"))
138+
)
139+
config.update(
140+
{
141+
"plugins": {
142+
"jedi_symbols": {
143+
"all_scopes": all_scopes,
144+
"include_import_symbols": include_import_symbols,
145+
}
146+
}
147+
}
148+
)
149+
150+
symbols = pylsp_document_symbols(config, doc)
151+
152+
expected = {"a_symbol": SymbolKind.Variable}
153+
if include_import_symbols:
154+
expected["MyNamedTuple"] = SymbolKind.Class
155+
assert {symbol["name"]: symbol["kind"] for symbol in symbols} == expected
156+
assert all(symbol["location"]["uri"] == doc.uri for symbol in symbols)
157+
158+
121159
@pytest.mark.skipif(
122160
PY2 or not LINUX or not CI, reason="tested on linux and python 3 only"
123161
)

0 commit comments

Comments
 (0)