From 4d3a631e6065b54213d91c957d0f1a326a54fb05 Mon Sep 17 00:00:00 2001 From: skshetry <18718008+skshetry@users.noreply.github.com> Date: Wed, 28 Feb 2024 14:40:56 +0545 Subject: [PATCH] fix PathInfo instantiation in Python 3.12 (#10328) --- dvc/testing/path_info.py | 14 ++++++++------ dvc/testing/tmp_dir.py | 11 +++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/dvc/testing/path_info.py b/dvc/testing/path_info.py index 63e028f1c..49d5ee2aa 100644 --- a/dvc/testing/path_info.py +++ b/dvc/testing/path_info.py @@ -1,6 +1,7 @@ import os import pathlib import posixpath +import sys from typing import Callable, ClassVar from urllib.parse import urlparse @@ -27,12 +28,13 @@ class PathInfo(pathlib.PurePath, _BasePath): __slots__ = () scheme = "local" - def __new__(cls, *args): - # Construct a proper subclass depending on current os - if cls is PathInfo: - cls = WindowsPathInfo if os.name == "nt" else PosixPathInfo + if sys.version_info < (3, 12): - return cls._from_parts(args) # type: ignore[attr-defined] + def __new__(cls, *args): + if cls is PathInfo: + cls = WindowsPathInfo if os.name == "nt" else PosixPathInfo + + return cls._from_parts(args) # type: ignore[attr-defined] def as_posix(self): f = self._flavour # type: ignore[attr-defined] @@ -67,7 +69,7 @@ class PathInfo(pathlib.PurePath, _BasePath): def isin(self, other): if isinstance(other, (str, bytes)): - other = self.__class__(other) + other = self.__class__(other) # type: ignore[arg-type] elif self.__class__ != other.__class__: return False # Use cached casefolded parts to compare paths diff --git a/dvc/testing/tmp_dir.py b/dvc/testing/tmp_dir.py index 4e40385d8..57272adb4 100644 --- a/dvc/testing/tmp_dir.py +++ b/dvc/testing/tmp_dir.py @@ -67,13 +67,12 @@ class TmpDir(pathlib.Path): def config(self): return {"url": self.url} - def __new__(cls, *args, **kwargs): - if cls is TmpDir: - cls = WindowsTmpDir if os.name == "nt" else PosixTmpDir + if sys.version_info < (3, 12): + + def __new__(cls, *args, **kwargs): + if cls is TmpDir: + cls = WindowsTmpDir if os.name == "nt" else PosixTmpDir - if sys.version_info >= (3, 12): - return object.__new__(cls) - else: # noqa: RET505 # init parameter and `_init` method has been removed in Python 3.10. kw = {"init": False} if sys.version_info < (3, 10) else {} self = cls._from_parts(args, **kw) # type: ignore[attr-defined]