10000 GH-104898: Add __slots__ to os.PathLike (GH-104899) · python/cpython@bd1b622 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd1b622

Browse files
authored
GH-104898: Add __slots__ to os.PathLike (GH-104899)
1 parent fea8632 commit bd1b622

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

Lib/os.py

+2
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

+1-5
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def __repr__(self):
233233
return "<{}.parents>".format(type(self._path).__name__)
234234

235235

236-
class PurePath(object):
236+
class PurePath(os.PathLike):
237237
"""Base class for manipulating paths without I/O.
238238
239239
PurePath represents a filesystem path and offers operations which
@@ -707,10 +707,6 @@ def match(self, path_pattern, *, case_sensitive=None):
707707
return False
708708
return True
709709

710-
# Can't subclass os.PathLike from PurePath and keep the constructor
711-
# optimizations in PurePath.__slots__.
712-
os.PathLike.register(PurePath)
713-
714710

715711
class PurePosixPath(PurePath):
716712
"""PurePath subclass for non-Windows systems.

Lib/test/test_os.py

+6
Original file line numberDiff line numberDiff line change
@@ -4640,6 +4640,12 @@ 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__'))
46434649

46444650
class TimesTests(unittest.TestCase):
46454651
def test_times(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing :attr:`~object.__slots__` to :class:`os.PathLike`.

0 commit comments

Comments
 (0)
0