Skip to content

Commit 458c33d

Browse files
committed
Subclass from os.PathLike
1 parent 68d2891 commit 458c33d

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

Lib/os.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,8 @@ class PathLike(abc.ABC):
10791079

10801080
"""Abstract base class for implementing the file system path protocol."""
10811081

1082+
__slots__ = ()
1083+
10821084
@abc.abstractmethod
10831085
def __fspath__(self):
10841086
"""Return the file system path representation of the object."""

Lib/pathlib.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def match(self, path_pattern, *, case_sensitive=None):
666666
return True
667667

668668

669-
class PurePath(_BasePurePath):
669+
class PurePath(_BasePurePath, os.PathLike):
670670
"""Base class for manipulating paths without I/O.
671671
672672
PurePath represents a filesystem path and offers operations which
@@ -716,11 +716,6 @@ def as_uri(self):
716716
return prefix + urlquote_from_bytes(os.fsencode(path))
717717

718718

719-
# Can't subclass os.PathLike from PurePath and keep the constructor
720-
# optimizations in PurePath.__slots__.
721-
os.PathLike.register(PurePath)
722-
723-
724719
class PurePosixPath(PurePath):
725720
"""PurePath subclass for non-Windows systems.
726721

Lib/test/test_os.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4640,6 +4640,13 @@ class A(os.PathLike):
46404640
def test_pathlike_class_getitem(self):
46414641
self.assertIsInstance(os.PathLike[bytes], types.GenericAlias)
46424642

4643+
def test_pathlike_subclass_slots(self):
4644+
class A(os.PathLike):
4645+
__slots__ = ()
4646+
def __fspath__(self):
4647+
return ''
4648+
self.assertFalse(hasattr(A(), '__dict__'))
4649+
46434650

46444651
class TimesTests(unittest.TestCase):
46454652
def test_times(self):

0 commit comments

Comments
 (0)