Skip to content

Commit 0f497d1

Browse files
committed
fix: Give precedence to user-provided paths when they are already listed in sys.path
Issue-248: #248
1 parent e3a303c commit 0f497d1

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/mkdocstrings_handlers/python/handler.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ def __init__(self, config: PythonConfig, base_dir: Path, **kwargs: Any) -> None:
128128
# If it's not absolute, make path relative to the config file path, then make it absolute.
129129
if not os.path.isabs(path):
130130
path = os.path.abspath(base_dir / path) # noqa: PLW2901
131-
# Don't add duplicates.
132-
if path not in search_paths:
133-
search_paths.insert(0, path)
131+
# Remove pre-listed paths.
132+
if path in search_paths:
133+
search_paths.remove(path)
134+
# Give precedence to user-provided paths.
135+
search_paths.insert(0, path)
134136

135137
self._paths = search_paths
136138
self._modules_collection: ModulesCollection = ModulesCollection()

tests/test_handler.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
from __future__ import annotations
44

55
import os
6+
import sys
67
from glob import glob
8+
from pathlib import Path
79
from textwrap import dedent
810
from typing import TYPE_CHECKING
911

1012
import pytest
1113
from griffe import DocstringSectionExamples, DocstringSectionKind, temporary_visited_module
1214

13-
from mkdocstrings_handlers.python.config import PythonOptions
15+
from mkdocstrings_handlers.python.config import PythonConfig, PythonOptions
1416
from mkdocstrings_handlers.python.handler import CollectionError, PythonHandler
1517

1618
if TYPE_CHECKING:
17-
from pathlib import Path
18-
1919
from mkdocstrings.plugin import MkdocstringsPlugin
2020

2121

@@ -167,3 +167,15 @@ def function(self):
167167
module["Class.function"].lineno = None
168168
module["attribute"].lineno = None
169169
assert handler.render(module, PythonOptions(show_source=True))
170+
171+
172+
def test_give_precedence_to_user_paths() -> None:
173+
"""Assert user paths take precedence over default paths."""
174+
last_sys_path = sys.path[-1]
175+
handler = PythonHandler(
176+
base_dir=Path("."),
177+
config=PythonConfig.from_data(paths=[last_sys_path]),
178+
mdx=[],
179+
mdx_config={},
180+
)
181+
assert handler._paths[0] == last_sys_path

0 commit comments

Comments
 (0)