]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-104898: Add __slots__ to os.PathLike (GH-104899)
authorBarney Gale <barney.gale@gmail.com>
Thu, 25 May 2023 20:24:20 +0000 (21:24 +0100)
committerGitHub <noreply@github.com>
Thu, 25 May 2023 20:24:20 +0000 (21:24 +0100)
Lib/os.py
Lib/pathlib.py
Lib/test/test_os.py
Misc/NEWS.d/next/Library/2023-05-24-22-50-21.gh-issue-104898.UbT2S4.rst [new file with mode: 0644]

index 598c9e502301f759904cdcb830232d9b3fa84041..31b957f13215d538a68f0e55e816b64a53b4a2b9 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -1079,6 +1079,8 @@ class PathLike(abc.ABC):
 
     """Abstract base class for implementing the file system path protocol."""
 
+    __slots__ = ()
+
     @abc.abstractmethod
     def __fspath__(self):
         """Return the file system path representation of the object."""
index 3a7a1241ba77f6d9d0aafe97eaf21237e5972e48..fb78939dcc31bade5ba6a580df9df592135482c5 100644 (file)
@@ -233,7 +233,7 @@ class _PathParents(Sequence):
         return "<{}.parents>".format(type(self._path).__name__)
 
 
-class PurePath(object):
+class PurePath(os.PathLike):
     """Base class for manipulating paths without I/O.
 
     PurePath represents a filesystem path and offers operations which
@@ -707,10 +707,6 @@ class PurePath(object):
                 return False
         return True
 
-# Can't subclass os.PathLike from PurePath and keep the constructor
-# optimizations in PurePath.__slots__.
-os.PathLike.register(PurePath)
-
 
 class PurePosixPath(PurePath):
     """PurePath subclass for non-Windows systems.
index 584cc05ca82a55b1273dd95cd20c0c804f075d1d..c6810c07ae09d8573cc70306a45e9fa97700bfe3 100644 (file)
@@ -4640,6 +4640,12 @@ class TestPEP519(unittest.TestCase):
     def test_pathlike_class_getitem(self):
         self.assertIsInstance(os.PathLike[bytes], types.GenericAlias)
 
+    def test_pathlike_subclass_slots(self):
+        class A(os.PathLike):
+            __slots__ = ()
+            def __fspath__(self):
+                return ''
+        self.assertFalse(hasattr(A(), '__dict__'))
 
 class TimesTests(unittest.TestCase):
     def test_times(self):
diff --git a/Misc/NEWS.d/next/Library/2023-05-24-22-50-21.gh-issue-104898.UbT2S4.rst b/Misc/NEWS.d/next/Library/2023-05-24-22-50-21.gh-issue-104898.UbT2S4.rst
new file mode 100644 (file)
index 0000000..e596ab3
--- /dev/null
@@ -0,0 +1 @@
+Add missing :attr:`~object.__slots__` to :class:`os.PathLike`.