Skip to content

Commit 738ca05

Browse files
committed
Skip generated symbols without a module path
1 parent a362006 commit 738ca05

2 files changed

Lines changed: 41 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,39 @@ def test_symbols_all_scopes_with_jedi_environment(workspace) -> None:
130130
doc.update_config(settings)
131131
symbols = pylsp_document_symbols(doc._config, doc)
132132
helper_check_symbols_all_scope(symbols)
133+
134+
135+
@pytest.mark.parametrize("all_scopes", [False, True])
136+
@pytest.mark.parametrize("include_import_symbols", [False, True])
137+
def test_symbols_imported_namedtuple(
138+
config, temp_workspace_factory, all_scopes, include_import_symbols
139+
):
140+
workspace = temp_workspace_factory(
141+
{
142+
"__init__.py": "",
143+
"b.py": (
144+
"from collections import namedtuple\n"
145+
'MyNamedTuple = namedtuple("MyNamedTuple", ["abc"])\n'
146+
),
147+
"a.py": 'from .b import MyNamedTuple\na_symbol = "a_symbol"\n',
148+
}
149+
)
150+
config.update(
151+
{
152+
"plugins": {
153+
"jedi_symbols": {
154+
"all_scopes": all_scopes,
155+
"include_import_symbols": include_import_symbols,
156+
}
157+
}
158+
}
159+
)
160+
uri = uris.from_fs_path(os.path.join(workspace.root_path, "a.py"))
161+
symbols = pylsp_document_symbols(config, workspace.get_document(uri))
162+
163+
expected_names = (
164+
["MyNamedTuple", "a_symbol"] if include_import_symbols else ["a_symbol"]
165+
)
166+
assert [symbol["name"] for symbol in symbols] == expected_names
167+
assert all(symbol["location"]["uri"] == uri for symbol in symbols)
168+
assert symbols[-1]["location"]["range"]["start"] == {"line": 1, "character": 0}

0 commit comments

Comments
 (0)