Skip to content

Commit 1f715d5

Browse files
authored
bpo-46483: change PurePath.__class_getitem__ to return GenericAlias (GH-30822)
1 parent c7f20f1 commit 1f715d5

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

Lib/pathlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from operator import attrgetter
1313
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
1414
from urllib.parse import quote_from_bytes as urlquote_from_bytes
15+
from types import GenericAlias
1516

1617

1718
__all__ = [
@@ -690,8 +691,7 @@ def __ge__(self, other):
690691
return NotImplemented
691692
return self._cparts >= other._cparts
692693

693-
def __class_getitem__(cls, type):
694-
return cls
694+
__class_getitem__ = classmethod(GenericAlias)
695695

696696
drive = property(attrgetter('_drv'),
697697
doc="""The drive prefix (letter or UNC path), if any.""")

Lib/test/test_pathlib.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,13 +2429,19 @@ def test_complex_symlinks_relative(self):
24292429
def test_complex_symlinks_relative_dot_dot(self):
24302430
self._check_complex_symlinks(os.path.join('dirA', '..'))
24312431

2432+
def test_class_getitem(self):
2433+
from types import GenericAlias
2434+
2435+
alias = self.cls[str]
2436+
self.assertIsInstance(alias, GenericAlias)
2437+
self.assertIs(alias.__origin__, self.cls)
2438+
self.assertEqual(alias.__args__, (str,))
2439+
self.assertEqual(alias.__parameters__, ())
2440+
24322441

24332442
class PathTest(_BasePathTest, unittest.TestCase):
24342443
cls = pathlib.Path
24352444

2436-
def test_class_getitem(self):
2437-
self.assertIs(self.cls[str], self.cls)
2438-
24392445
def test_concrete_class(self):
24402446
p = self.cls('a')
24412447
self.assertIs(type(p),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Change :meth:`pathlib.PurePath.__class_getitem__` to return
2+
:class:`types.GenericAlias`.

0 commit comments

Comments
 (0)