Skip to content

Commit

Permalink
Add Status protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
barneygale committed Dec 9, 2024
1 parent 76ef028 commit 5d92785
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Lib/pathlib/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@ def splitdrive(self, path: str) -> tuple[str, str]: ...
def splitext(self, path: str) -> tuple[str, str]: ...
def normcase(self, path: str) -> str: ...
def isabs(self, path: str) -> bool: ...


@runtime_checkable
class Status(Protocol):
"""Protocol for path statuses, which support querying the file type.
Methods may return cached results.
"""
def is_dir(self, *, follow_symlinks: bool = True) -> bool: ...
def is_file(self, *, follow_symlinks: bool = True) -> bool: ...
def is_symlink(self) -> bool: ...
4 changes: 2 additions & 2 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest

from pathlib._abc import UnsupportedOperation, PurePathBase, PathBase
from pathlib._types import Parser
from pathlib._types import Parser, Status
import posixpath

from test.support.os_helper import TESTFN
Expand Down Expand Up @@ -1902,7 +1902,7 @@ def test_iterdir_status(self):
p = self.cls(self.base)
for child in p.iterdir():
entry = child._status
self.assertIsNotNone(entry)
self.assertIsInstance(entry, Status)
self.assertEqual(entry.is_dir(follow_symlinks=False),
child.is_dir(follow_symlinks=False))
self.assertEqual(entry.is_file(follow_symlinks=False),
Expand Down

0 comments on commit 5d92785

Please sign in to comment.