From 7317eec828ae77d6ec1d8f9ff0f83ece85ed70c1 Mon Sep 17 00:00:00 2001 From: isidentical Date: Sat, 7 Dec 2019 19:02:40 +0300 Subject: [PATCH 1/2] bpo-38994: Implement __class_getitem__ for PathLike --- Lib/os.py | 3 +++ Lib/test/test_os.py | 3 +++ .../next/Library/2019-12-07-18-58-44.bpo-38994.IJYhz_.rst | 1 + 3 files changed, 7 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-12-07-18-58-44.bpo-38994.IJYhz_.rst diff --git a/Lib/os.py b/Lib/os.py index 52d3f1d7415854..c901bd1b8ed9d8 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -1072,6 +1072,9 @@ def __fspath__(self): def __subclasshook__(cls, subclass): return hasattr(subclass, '__fspath__') + def __class_getitem__(cls, type): + return cls + if name == 'nt': class _AddedDllDirectory: diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index bf40cb1e8fa7da..f44ddbad7d6418 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -4048,6 +4048,9 @@ def test_bad_pathlike(self): 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/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 00000000000000..52b3405307ead4 --- /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`` From 750c17f820eaad6be35bfd0c744553cb8e09e60c Mon Sep 17 00:00:00 2001 From: isidentical Date: Sun, 8 Dec 2019 21:35:38 +0300 Subject: [PATCH 2/2] implement __class_getitem__ for PurePath --- Lib/pathlib.py | 3 +++ Lib/test/test_pathlib.py | 3 +++ .../next/Library/2019-12-07-18-58-44.bpo-38994.IJYhz_.rst | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index d70fde0ea3b452..f0537cfea19ab6 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -777,6 +777,9 @@ def __ge__(self, other): 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_pathlib.py b/Lib/test/test_pathlib.py index 058a201aebc1d8..b8e7fcc2e3029c 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -2217,6 +2217,9 @@ def test_complex_symlinks_relative_dot_dot(self): 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 index 52b3405307ead4..b9cb4176350cbc 100644 --- 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 @@ -1 +1 @@ -Implement ``__class_getitem__`` for ``os.PathLike`` +Implement ``__class_getitem__`` for ``os.PathLike``, ``pathlib.Path``