Skip to content

Commit

Permalink
pathlib: import signature and docs for import_path
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Mar 2, 2024
1 parent 6b889f9 commit 847dfc3
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,26 +484,31 @@ class ImportPathMismatchError(ImportError):


def import_path(
p: Union[str, "os.PathLike[str]"],
path: Union[str, "os.PathLike[str]", Path],
*,
mode: Union[str, ImportMode] = ImportMode.prepend,
root: Path,
) -> ModuleType:
"""Import and return a module from the given path, which can be a file (a module) or
"""
Import and return a module from the given path, which can be a file (a module) or
a directory (a package).
The import mechanism used is controlled by the `mode` parameter:
:param path:
Path to the file to import.
:param mode:
Controls the underlying import mechanism that will be used:
* `mode == ImportMode.prepend`: the directory containing the module (or package, taking
`__init__.py` files into account) will be put at the *start* of `sys.path` before
being imported with `importlib.import_module`.
* ImportMode.prepend: the directory containing the module (or package, taking
`__init__.py` files into account) will be put at the *start* of `sys.path` before
being imported with `importlib.import_module`.
* `mode == ImportMode.append`: same as `prepend`, but the directory will be appended
to the end of `sys.path`, if not already in `sys.path`.
* ImportMode.append: same as `prepend`, but the directory will be appended
to the end of `sys.path`, if not already in `sys.path`.
* `mode == ImportMode.importlib`: uses more fine control mechanisms provided by `importlib`
to import the module, which avoids having to muck with `sys.path` at all. It effectively
allows having same-named test modules in different places.
* ImportMode.importlib: uses more fine control mechanisms provided by `importlib`
to import the module, which avoids having to muck with `sys.path` at all. It effectively
allows having same-named test modules in different places.
:param root:
Used as an anchor when mode == ImportMode.importlib to obtain
Expand All @@ -514,10 +519,9 @@ def import_path(
If after importing the given `path` and the module `__file__`
are different. Only raised in `prepend` and `append` modes.
"""
path = Path(path)
mode = ImportMode(mode)

path = Path(p)

if not path.exists():
raise ImportError(path)

Expand Down

0 comments on commit 847dfc3

Please sign in to comment.