8000 GH-104898: Add __slots__ to os.PathLike by barneygale · Pull Request #104899 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-104898: Add __slots__ to os.PathLike #104899

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 1 commit into from
May 25, 2023
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
2 changes: 2 additions & 0 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
6 changes: 1 addition & 5 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def __repr__(self):
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
Expand Down Expand Up @@ -707,10 +707,6 @@ def match(self, path_pattern, *, case_sensitive=None):
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.
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4640,6 +4640,12 @@ class A(os.PathLike):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing :attr:`~object.__slots__` to :class:`os.PathLike`.
0