Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pylsp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,13 @@ def find_parents(root, path, names):

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


def match_uri_to_workspace(uri, workspaces):
Expand Down
24 changes: 24 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import multiprocessing
import os
import subprocess
import sys
import time
from threading import Thread
Expand Down Expand Up @@ -215,6 +216,29 @@ def test_find_parents_cross_mount(tmpdir, monkeypatch) -> None:
assert result == []


def test_path_to_dot_name_in_package_cwd(tmp_path) -> None:
(tmp_path / "__init__.py").touch()
script = """
from types import SimpleNamespace

from pylsp.workspace import Document

workspace = SimpleNamespace(_config=None)
assert Document('', workspace).dot_path == ''
assert Document('untitled:Untitled-1', workspace).dot_path == 'Untitled-1'
"""
subprocess.run([sys.executable, "-c", script], cwd=tmp_path, timeout=10, check=True)


def test_path_to_dot_name_nested_package(tmp_path) -> None:
parent = tmp_path / "pkg"
nested = parent / "nested"
nested.mkdir(parents=True)
(parent / "__init__.py").touch()
(nested / "__init__.py").touch()
assert _utils.path_to_dot_name(str(nested / "module.py")) == "pkg.nested.module"


def test_merge_dicts() -> None:
assert _utils.merge_dicts(
{"a": True, "b": {"x": 123, "y": {"hello": "world"}}},
Expand Down