@@ -484,26 +484,31 @@ class ImportPathMismatchError(ImportError):
484
484
485
485
486
486
def import_path (
487
- p : Union [str , "os.PathLike[str]" ],
487
+ path : Union [str , "os.PathLike[str]" ],
488
488
* ,
489
489
mode : Union [str , ImportMode ] = ImportMode .prepend ,
490
490
root : Path ,
491
491
) -> ModuleType :
492
- """Import and return a module from the given path, which can be a file (a module) or
492
+ """
493
+ Import and return a module from the given path, which can be a file (a module) or
493
494
a directory (a package).
494
495
495
- The import mechanism used is controlled by the `mode` parameter:
496
+ :param path:
497
+ Path to the file to import.
496
498
497
- * `mode == ImportMode.prepend`: the directory containing the module (or package, taking
498
- `__init__.py` files into account) will be put at the *start* of `sys.path` before
499
- being imported with `importlib.import_module`.
499
+ :param mode:
500
+ Controls the underlying import mechanism that will be used:
500
501
501
- * `mode == ImportMode.append`: same as `prepend`, but the directory will be appended
502
- to the end of `sys.path`, if not already in `sys.path`.
502
+ * ImportMode.prepend: the directory containing the module (or package, taking
503
+ `__init__.py` files into account) will be put at the *start* of `sys.path` before
504
+ being imported with `importlib.import_module`.
503
505
504
- * `mode == ImportMode.importlib`: uses more fine control mechanisms provided by `importlib`
505
- to import the module, which avoids having to muck with `sys.path` at all. It effectively
506
- allows having same-named test modules in different places.
506
+ * ImportMode.append: same as `prepend`, but the directory will be appended
507
+ to the end of `sys.path`, if not already in `sys.path`.
508
+
509
+ * ImportMode.importlib: uses more fine control mechanisms provided by `importlib`
510
+ to import the module, which avoids having to muck with `sys.path` at all. It effectively
511
+ allows having same-named test modules in different places.
507
512
508
513
:param root:
509
514
Used as an anchor when mode == ImportMode.importlib to obtain
@@ -514,10 +519,9 @@ def import_path(
514
519
If after importing the given `path` and the module `__file__`
515
520
are different. Only raised in `prepend` and `append` modes.
516
521
"""
522
+ path = Path (path )
517
523
mode = ImportMode (mode )
518
524
519
- path = Path (p )
520
-
521
525
if not path .exists ():
522
526
raise ImportError (path )
523
527
0 commit comments