Skip to content

bpo-38994: Implement __class_getitem__ for PathLike #17498

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 2 commits into from
Dec 8, 2019
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
3 changes: 3 additions & 0 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.""")

Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement ``__class_getitem__`` for ``os.PathLike``, ``pathlib.Path``
Copy link
Member

Choose a reason for hiding this comment

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

This news item is slightly misleading as the change was made to pathlib.PurePath, not pathlib.Path.

Copy link
Member

Choose a reason for hiding this comment

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

And @isidentical forgot to give himself credit for the change. :)