]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38994: Implement __class_getitem__ for PathLike (GH-17498)
authorBatuhan Taşkaya <47358913+isidentical@users.noreply.github.com>
Sun, 8 Dec 2019 20:31:15 +0000 (23:31 +0300)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 8 Dec 2019 20:31:15 +0000 (12:31 -0800)
https://bugs.python.org/issue38994

Lib/os.py
Lib/pathlib.py
Lib/test/test_os.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2019-12-07-18-58-44.bpo-38994.IJYhz_.rst [new file with mode: 0644]

index 52d3f1d7415854e7baf7f5ac564ef4629c648449..c901bd1b8ed9d85a935533534e9bf2fec7fe823b 100644 (file)
--- 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:
index d70fde0ea3b452ce0aa8b41c3bfe8fbc95a32997..f0537cfea19ab64c44d685036d1e6fdad306557d 100644 (file)
@@ -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.""")
 
index bf40cb1e8fa7da2be67a9924576ff757ed3f203d..f44ddbad7d64183817a547a71775987cab8fae8f 100644 (file)
@@ -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):
index 058a201aebc1d81534f122224c854a53f0b44d0d..b8e7fcc2e3029cb403861de9ff750f3cd66e2455 100644 (file)
@@ -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 (file)
index 0000000..b9cb417
--- /dev/null
@@ -0,0 +1 @@
+Implement ``__class_getitem__`` for ``os.PathLike``, ``pathlib.Path``