From 6d3caffa3287379d8aa6cf945759e011757b7977 Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Mon, 13 Feb 2023 19:07:24 +0000 Subject: [PATCH] Backport Path.is_relative_to to Python 3.8.x Further Fix to #28. --- devine/core/utilities.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/devine/core/utilities.py b/devine/core/utilities.py index 190bbc0..1d94cd4 100644 --- a/devine/core/utilities.py +++ b/devine/core/utilities.py @@ -22,6 +22,12 @@ from devine.core.config import config from devine.core.constants import LANGUAGE_MAX_DISTANCE +def is_relative_to_backport(base: Path, path_: Union[Path, str]) -> bool: + """Same as Path.is_relative_to, but back-ported to 3.8.x.""" + path_ = Path(path_) + return path_ == base or path_ in base.parents + + def import_module_by_path(path: Path) -> ModuleType: """Import a Python file by Path as a Module.""" if not path: @@ -32,7 +38,7 @@ def import_module_by_path(path: Path) -> ModuleType: raise ValueError("Path does not exist") # compute package hierarchy for relative import support - if path.is_relative_to(config.directories.core_dir): + if is_relative_to_backport(path, config.directories.core_dir): name = [] _path = path.parent while _path.stem != config.directories.core_dir.stem: