diff --git a/src/ktools/pathlib.py b/src/ktools/pathlib.py index 2e43bba..cd8b12e 100644 --- a/src/ktools/pathlib.py +++ b/src/ktools/pathlib.py @@ -9,7 +9,7 @@ from pathlib import PosixPath as PosixPathBase from pathlib import WindowsPath as WindowsPathBase from platform import system from threading import RLock -from typing import Iterable +from typing import Callable, Iterable _UMASK_LOCK = RLock() _CODE_OSERROR_DIRECTORY_NOT_EMPTY = 66 if system() == 'FreeBSD' else 39 @@ -19,6 +19,17 @@ _LOGGER = getLogger(name=__name__) class Path(PathBase): 'Extending the built-in `Path`.' + def __new__(cls, *args: str, **kwargs: str): + if cls is Path: + cls = WindowsPath if os_name == 'nt' else PosixPath + if hasattr(cls, '_from_parts'): + cls._from_parts: Callable[[Iterable[str]], WindowsPath | PosixPath] + # version < 3.12 + return cls._from_parts(args) + else: + # 3.12 on + return object.__new__(cls) + def _ensure_parentdirs_inner( self, relative_path: Path, mode: int | None = None, uid: int | None = None, gid: int | None = None) -> Path: