Skip to content

Commit 31e876e

Browse files
skshetryBradyJ27
authored andcommitted
fix PathInfo instantiation in Python 3.12 (treeverse#10328)
1 parent bd771b7 commit 31e876e

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

dvc/testing/path_info.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import pathlib
33
import posixpath
4+
import sys
45
from typing import Callable, ClassVar
56
from urllib.parse import urlparse
67

@@ -27,12 +28,13 @@ class PathInfo(pathlib.PurePath, _BasePath):
2728
__slots__ = ()
2829
scheme = "local"
2930

30-
def __new__(cls, *args):
31-
# Construct a proper subclass depending on current os
32-
if cls is PathInfo:
33-
cls = WindowsPathInfo if os.name == "nt" else PosixPathInfo
31+
if sys.version_info < (3, 12):
3432

35-
return cls._from_parts(args) # type: ignore[attr-defined]
33+
def __new__(cls, *args):
34+
if cls is PathInfo:
35+
cls = WindowsPathInfo if os.name == "nt" else PosixPathInfo
36+
37+
return cls._from_parts(args) # type: ignore[attr-defined]
3638

3739
def as_posix(self):
3840
f = self._flavour # type: ignore[attr-defined]
@@ -67,7 +69,7 @@ def relpath(self, other):
6769

6870
def isin(self, other):
6971
if isinstance(other, (str, bytes)):
70-
other = self.__class__(other)
72+
other = self.__class__(other) # type: ignore[arg-type]
7173
elif self.__class__ != other.__class__:
7274
return False
7375
# Use cached casefolded parts to compare paths

dvc/testing/tmp_dir.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@ def url(self):
6767
def config(self):
6868
return {"url": self.url}
6969

70-
def __new__(cls, *args, **kwargs):
71-
if cls is TmpDir:
72-
cls = WindowsTmpDir if os.name == "nt" else PosixTmpDir
70+
if sys.version_info < (3, 12):
71+
72+
def __new__(cls, *args, **kwargs):
73+
if cls is TmpDir:
74+
cls = WindowsTmpDir if os.name == "nt" else PosixTmpDir
7375

74-
if sys.version_info >= (3, 12):
75-
return object.__new__(cls)
76-
else: # noqa: RET505
7776
# init parameter and `_init` method has been removed in Python 3.10.
7877
kw = {"init": False} if sys.version_info < (3, 10) else {}
7978
self = cls._from_parts(args, **kw) # type: ignore[attr-defined]

0 commit comments

Comments
 (0)