diff --git a/astroid/util.py b/astroid/util.py index 3ddbc09040..510b81cc13 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -5,10 +5,7 @@ from __future__ import annotations -import contextlib -import sys import warnings -from collections.abc import Iterator, Sequence from typing import TYPE_CHECKING, Any, Final, Literal from astroid.exceptions import InferenceError @@ -160,26 +157,3 @@ def safe_infer( return None # there is some kind of ambiguity except StopIteration: return value - - -def _augment_sys_path(additional_paths: Sequence[str]) -> list[str]: - original = list(sys.path) - changes = [] - seen = set() - for additional_path in additional_paths: - if additional_path not in seen: - changes.append(additional_path) - seen.add(additional_path) - - sys.path[:] = changes + sys.path - return original - - -@contextlib.contextmanager -def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]: - """Augment 'sys.path' by adding entries from additional_paths.""" - original = _augment_sys_path(additional_paths) - try: - yield - finally: - sys.path[:] = original diff --git a/tests/resources.py b/tests/resources.py index 853fd796f1..ce0bfb3df9 100644 --- a/tests/resources.py +++ b/tests/resources.py @@ -4,8 +4,10 @@ from __future__ import annotations +import contextlib import os import sys +from collections.abc import Iterator, Sequence from pathlib import Path from astroid import builder @@ -33,3 +35,26 @@ def tearDown(self) -> None: for key in list(sys.path_importer_cache): if key.startswith(datadir): del sys.path_importer_cache[key] + + +def _augment_sys_path(additional_paths: Sequence[str]) -> list[str]: + original = list(sys.path) + changes = [] + seen = set() + for additional_path in additional_paths: + if additional_path not in seen: + changes.append(additional_path) + seen.add(additional_path) + + sys.path[:] = changes + sys.path + return original + + +@contextlib.contextmanager +def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]: + """Augment 'sys.path' by adding entries from additional_paths.""" + original = _augment_sys_path(additional_paths) + try: + yield + finally: + sys.path[:] = original diff --git a/tests/test_modutils.py b/tests/test_modutils.py index 6b815d986c..6635ba186e 100644 --- a/tests/test_modutils.py +++ b/tests/test_modutils.py @@ -22,7 +22,6 @@ from astroid import modutils from astroid.const import PY310_PLUS from astroid.interpreter._import import spec -from astroid.util import augmented_sys_path from . import resources @@ -185,7 +184,7 @@ def test_modpath_from_file_path_order(self) -> None: should be relative to the subdirectory since additional directory has higher precedence.""" with tempfile.TemporaryDirectory() as tmp_dir: - with augmented_sys_path([tmp_dir]): + with resources.augmented_sys_path([tmp_dir]): mod_name = "module" sub_dirname = "subdir" sub_dir = tmp_dir + "/" + sub_dirname