Skip to content

bpo-46483: change PurePath.__class_getitem__ to return GenericAlias #30822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down Expand Up @@ -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.""")
Expand Down
12 changes: 9 additions & 3 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test now works for all _BasePathTest subclasses, previously it was only checking PurePath.

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),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change :meth:`pathlib.PurePath.__class_getitem__` to return
:class:`types.GenericAlias`.