fix PathInfo instantiation in Python 3.12 (#10328)

This commit is contained in:
skshetry
2024-02-28 14:40:56 +05:45
committed by GitHub
parent 491155cdb0
commit 4d3a631e60
2 changed files with 13 additions and 12 deletions
+8 -6
View File
@@ -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
+5 -6
View File
@@ -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]