Backport Path.is_relative_to to Python 3.8.x

Further Fix to #28.
This commit is contained in:
rlaphoenix 2023-02-13 19:07:24 +00:00
parent d926b4fe9a
commit 6d3caffa32
1 changed files with 7 additions and 1 deletions

View File

@ -22,6 +22,12 @@ from devine.core.config import config
from devine.core.constants import LANGUAGE_MAX_DISTANCE 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: def import_module_by_path(path: Path) -> ModuleType:
"""Import a Python file by Path as a Module.""" """Import a Python file by Path as a Module."""
if not path: if not path:
@ -32,7 +38,7 @@ def import_module_by_path(path: Path) -> ModuleType:
raise ValueError("Path does not exist") raise ValueError("Path does not exist")
# compute package hierarchy for relative import support # 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 = [] name = []
_path = path.parent _path = path.parent
while _path.stem != config.directories.core_dir.stem: while _path.stem != config.directories.core_dir.stem: