[Python-checkins] bpo-46483: change `PurePath.__class_getitem__` to return `GenericAlias` (GH-30822)

isidentical webhook-mailer at python.org
Sun Jan 23 09:48:49 EST 2022


https://github.com/python/cpython/commit/1f715d5bd3bc9ff444e109b6bbd13011913681b1
commit: 1f715d5bd3bc9ff444e109b6bbd13011913681b1
branch: main
author: Nikita Sobolev <mail at sobolevn.me>
committer: isidentical <isidentical at gmail.com>
date: 2022-01-23T17:48:43+03:00
summary:

bpo-46483: change `PurePath.__class_getitem__` to return `GenericAlias` (GH-30822)

files:
A Misc/NEWS.d/next/Library/2022-01-23-11-17-48.bpo-46483.j7qwWb.rst
M Lib/pathlib.py
M Lib/test/test_pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 04b321b9ccf16..d42ee4dc90b43 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -12,6 +12,7 @@
 from operator import attrgetter
 from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
 from urllib.parse import quote_from_bytes as urlquote_from_bytes
+from types import GenericAlias
 
 
 __all__ = [
@@ -690,8 +691,7 @@ def __ge__(self, other):
             return NotImplemented
         return self._cparts >= other._cparts
 
-    def __class_getitem__(cls, type):
-        return cls
+    __class_getitem__ = classmethod(GenericAlias)
 
     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 555c7ee795bd1..1bf21120a36ca 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -2429,13 +2429,19 @@ def test_complex_symlinks_relative(self):
     def test_complex_symlinks_relative_dot_dot(self):
         self._check_complex_symlinks(os.path.join('dirA', '..'))
 
+    def test_class_getitem(self):
+        from types import GenericAlias
+
+        alias = self.cls[str]
+        self.assertIsInstance(alias, GenericAlias)
+        self.assertIs(alias.__origin__, self.cls)
+        self.assertEqual(alias.__args__, (str,))
+        self.assertEqual(alias.__parameters__, ())
+
 
 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/2022-01-23-11-17-48.bpo-46483.j7qwWb.rst b/Misc/NEWS.d/next/Library/2022-01-23-11-17-48.bpo-46483.j7qwWb.rst
new file mode 100644
index 0000000000000..a84503d299a10
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-23-11-17-48.bpo-46483.j7qwWb.rst
@@ -0,0 +1,2 @@
+Change :meth:`pathlib.PurePath.__class_getitem__` to return
+:class:`types.GenericAlias`.



More information about the Python-checkins mailing list