diff options
author | Barney Gale <barney.gale@gmail.com> | 2023-05-25 20:24:20 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-25 20:24:20 (GMT) |
commit | bd1b6228d132b8e9836fe352cd8dca2b6c1bd98c (patch) | |
tree | 42df193dad2436c36c67ca3c4ce5235e5eca8845 | |
parent | fea8632ec69d160a11b8ec506900c14989952bc1 (diff) | |
download | cpython-bd1b6228d132b8e9836fe352cd8dca2b6c1bd98c.zip cpython-bd1b6228d132b8e9836fe352cd8dca2b6c1bd98c.tar.gz cpython-bd1b6228d132b8e9836fe352cd8dca2b6c1bd98c.tar.bz2 |
GH-104898: Add __slots__ to os.PathLike (GH-104899)
-rw-r--r-- | Lib/os.py | 2 | ||||
-rw-r--r-- | Lib/pathlib.py | 6 | ||||
-rw-r--r-- | Lib/test/test_os.py | 6 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2023-05-24-22-50-21.gh-issue-104898.UbT2S4.rst | 1 |
4 files changed, 10 insertions, 5 deletions
@@ -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.""" diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 3a7a124..fb78939 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -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. diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 584cc05..c6810c0 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -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 index 0000000..e596ab3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-24-22-50-21.gh-issue-104898.UbT2S4.rst @@ -0,0 +1 @@ +Add missing :attr:`~object.__slots__` to :class:`os.PathLike`. |