Skip to content

Commit 46ac0bf

Browse files
committed
Stop module-name discovery at the path root
1 parent a362006 commit 46ac0bf

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎pylsp/_utils.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,13 @@ def find_parents(root, path, names):
122122

123123
def path_to_dot_name(path):
124124
"""Given a path to a module, derive its dot-separated full name."""
125-
directory = os.path.dirname(path)
125+
directory = pathlib.Path(os.path.dirname(path))
126126
module_name, _ = os.path.splitext(os.path.basename(path))
127127
full_name = [module_name]
128-
while os.path.exists(os.path.join(directory, "__init__.py")):
129-
this_directory = os.path.basename(directory)
130-
directory = os.path.dirname(directory)
131-
full_name = [this_directory] + full_name
132-
return ".".join(full_name)
128+
while directory.name and (directory / "__init__.py").exists():
129+
full_name.append(directory.name)
130+
directory = directory.parent
131+
return ".".join(reversed(full_name))
133132

134133

135134
def match_uri_to_workspace(uri, workspaces):

‎test/test_utils.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import multiprocessing
55
import os
6+
import subprocess
67
import sys
78
import time
89
from threading import Thread
@@ -215,6 +216,29 @@ def test_find_parents_cross_mount(tmpdir, monkeypatch) -> None:
215216
assert result == []
216217

217218

219+
def test_path_to_dot_name_in_package_cwd(tmp_path) -> None:
220+
(tmp_path / "__init__.py").touch()
221+
script = """
222+
from types import SimpleNamespace
223+
224+
from pylsp.workspace import Document
225+
226+
workspace = SimpleNamespace(_config=None)
227+
assert Document('', workspace).dot_path == ''
228+
assert Document('untitled:Untitled-1', workspace).dot_path == 'Untitled-1'
229+
"""
230+
subprocess.run([sys.executable, "-c", script], cwd=tmp_path, timeout=10, check=True)
231+
232+
233+
def test_path_to_dot_name_nested_package(tmp_path) -> None:
234+
parent = tmp_path / "pkg"
235+
nested = parent / "nested"
236+
nested.mkdir(parents=True)
237+
(parent / "__init__.py").touch()
238+
(nested / "__init__.py").touch()
239+
assert _utils.path_to_dot_name(str(nested / "module.py")) == "pkg.nested.module"
240+
241+
218242
def test_merge_dicts() -> None:
219243
assert _utils.merge_dicts(
220244
{"a": True, "b": {"x": 123, "y": {"hello": "world"}}},

0 commit comments

Comments
 (0)