Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

GH-130798: Add type hints to pathlib.types #131639

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use _PathParser in _explode_path
  • Loading branch information
ap-- committed Mar 26, 2025
commit 4aa254005169e48f714b443f4518ce06b39fc508
9 changes: 5 additions & 4 deletions 9 Lib/pathlib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
_WP = TypeVar("_WP", bound="_WritablePath")
ap-- marked this conversation as resolved.
Show resolved Hide resolved


def _explode_path(path: str, split: Callable[[str], tuple[str, str]]) -> tuple[str, list[str]]:
def _explode_path(path: str, parser: "_PathParser") -> tuple[str, list[str]]:
ap-- marked this conversation as resolved.
Show resolved Hide resolved
"""
Split the path into a 2-tuple (anchor, parts), where *anchor* is the
uppermost parent of the path (equivalent to path.parents[-1]), and
*parts* is a reversed list of parts following the anchor.
"""
split = parser.split
parent, name = split(path)
names = []
while path != parent:
Expand Down Expand Up @@ -99,7 +100,7 @@ def __str__(self) -> str:
@property
def anchor(self) -> str:
"""The concatenation of the drive and root, or ''."""
return _explode_path(str(self), self.parser.split)[0]
return _explode_path(str(self), self.parser)[0]

@property
def name(self) -> str:
Expand Down Expand Up @@ -173,7 +174,7 @@ def with_suffix(self, suffix: str) -> Self:
def parts(self) -> Sequence[str]:
"""An object providing sequence-like access to the
components in the filesystem path."""
anchor, parts = _explode_path(str(self), self.parser.split)
anchor, parts = _explode_path(str(self), self.parser)
if anchor:
parts.append(anchor)
return tuple(reversed(parts))
Expand Down Expand Up @@ -289,7 +290,7 @@ def glob(self, pattern: str, *, recurse_symlinks: Literal[True] = True) -> Itera
"""Iterate over this subtree and yield all existing files (of any
kind, including directories) matching the given relative pattern.
"""
anchor, parts = _explode_path(pattern, self.parser.split)
anchor, parts = _explode_path(pattern, self.parser)
if anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
elif not parts:
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.