From: Batuhan Taşkaya <47358913+isidentical@users.noreply.github.com> Date: Sun, 8 Dec 2019 20:31:15 +0000 (+0300) Subject: bpo-38994: Implement __class_getitem__ for PathLike (GH-17498) X-Git-Tag: v3.9.0a2~59 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=526606baf76e7a5309bb00f3bfaefa861a2014ba;p=thirdparty%2FPython%2Fcpython.git bpo-38994: Implement __class_getitem__ for PathLike (GH-17498) https://bugs.python.org/issue38994 --- diff --git a/Lib/os.py b/Lib/os.py index 52d3f1d74158..c901bd1b8ed9 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -1072,6 +1072,9 @@ class PathLike(abc.ABC): def __subclasshook__(cls, subclass): return hasattr(subclass, '__fspath__') + def __class_getitem__(cls, type): + return cls + if name == 'nt': class _AddedDllDirectory: diff --git a/Lib/pathlib.py b/Lib/pathlib.py index d70fde0ea3b4..f0537cfea19a 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -777,6 +777,9 @@ class PurePath(object): return NotImplemented return self._cparts >= other._cparts + def __class_getitem__(cls, type): + return cls + drive = property(attrgetter('_drv'), doc="""The drive prefix (letter or UNC path), if any.""") diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index bf40cb1e8fa7..f44ddbad7d64 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -4048,6 +4048,9 @@ class TestPEP519(unittest.TestCase): self.assertRaises(ZeroDivisionError, self.fspath, FakePath(ZeroDivisionError())) + def test_pathlike_class_getitem(self): + self.assertIs(os.PathLike[bytes], os.PathLike) + class TimesTests(unittest.TestCase): def test_times(self): diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 058a201aebc1..b8e7fcc2e302 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2217,6 +2217,9 @@ class _BasePathTest(object): class PathTest(_BasePathTest, unittest.TestCase): cls = pathlib.Path + def test_class_getitem(self): + self.assertIs(self.cls[str], self.cls) + def test_concrete_class(self): p = self.cls('a') self.assertIs(type(p), diff --git a/Misc/NEWS.d/next/Library/2019-12-07-18-58-44.bpo-38994.IJYhz_.rst b/Misc/NEWS.d/next/Library/2019-12-07-18-58-44.bpo-38994.IJYhz_.rst new file mode 100644 index 000000000000..b9cb4176350c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-12-07-18-58-44.bpo-38994.IJYhz_.rst @@ -0,0 +1 @@ +Implement ``__class_getitem__`` for ``os.PathLike``, ``pathlib.Path``