8000 GH-130798: Add type hints to pathlib.types by ap-- · Pull Request #131639 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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
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
10000
Prev Previous commit
Next Next commit
Update types
- pathsegments are `str`: joinpath, __truediv__, __rtruediv__, with_segments
- symlink_to target is `str`
- glob recurse_symlinks is `Literal[True]`
- full_match and glob pattern is `str`
  • Loading branch information
ap-- committed Mar 24, 2025
commit d1583c02e0d883cca70cc6af8736dba326da8c1d
19 changes: 9 additions & 10 deletions Lib/pathlib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
from pathlib._os import magic_open, ensure_distinct_paths, ensure_different_files, copyfileobj
from pathlib import PurePath, Path
from typing import (
Any, BinaryIO, Callable, Generator, Iterator, Optional, Protocol, Sequence, TypeVar, Union,
Any, BinaryIO, Callable, Generator, Iterator, Literal, Optional, Protocol, Sequence, TypeVar,
runtime_checkable,
)


_JP = TypeVar("_JP", bound="_JoinablePath")
_RP = TypeVar("_RP", bound="_ReadablePath")
_WP = TypeVar("_WP", bound="_WritablePath")


def _explode_path(path, split):
def _explode_path(path: str, split: Callable[[str], tuple[str, str]]) -> tuple[str, list[str]]:
"""
Split the path into a 2-tuple (anchor, parts), where *anchor* is the
uppermost parent of the path (equivalent to path.parents[-1]), and
Expand Down Expand Up @@ -85,7 +84,7 @@ def parser(self) -> _PathParser:
raise NotImplementedError

@abstractmethod
def with_segments(self: _JP, *pathsegments: Union[_JP, str]) -> _JP:
def with_segments(self: _JP, *pathsegments: str) -> _JP:
"""Construct a new path object from any number of path-like objects.
Subclasses may override this method to customize how new path objects
are created from methods like `iterdir()`.
Expand Down Expand Up @@ -180,21 +179,21 @@ def parts(self) -> Sequence[str]:
parts.append(anchor)
return tuple(reversed(parts))

def joinpath(self: _JP, *pathsegments: Union[_JP, str]) -> _JP:
def joinpath(self: _JP, *pathsegments: str) -> _JP:
"""Combine this path with one or several arguments, and return a
new path representing either a subpath (if all arguments are relative
paths) or a totally different path (if one of the arguments is
anchored).
"""
return self.with_segments(str(self), *pathsegments)

def __truediv__(self: _JP, key: Union[_JP, str]) -> _JP:
def __truediv__(self: _JP, key: str) -> _JP:
try:
return self.with_segments(str(self), key)
except TypeError:
return NotImplemented

def __rtruediv__(self: _JP, key: Union[_JP, str]) -> _JP:
def __rtruediv__(self: _JP, key: str) -> _JP:
try:
return self.with_segments(key, str(self))
except TypeError:
Expand Down Expand Up @@ -222,7 +221,7 @@ def parents(self: _JP) -> Sequence[_JP]:
parent = split(path)[0]
return tuple(parents)

def full_match(self: _JP, pattern: Union[_JP, str]) -> bool:
def full_match(self: _JP, pattern: str) -> bool:
"""
Return True if this path matches the given glob-style pattern. The
pattern is matched against the entire path.
Expand Down Expand Up @@ -287,7 +286,7 @@ def iterdir(self: _RP) -> Iterator[_RP]:
"""
raise NotImplementedError

def glob(self: _RP, pattern: Union[_RP, str], *, recurse_symlinks: bool = True) -> Iterator[_RP]:
def glob(self: _RP, pattern: str, *, recurse_symlinks: Literal[True] = True) -> Iterator[_RP]:
"""Iterate over this subtree and yield all existing files (of any
kind, including directories) matching the given relative pattern.
"""
Expand Down Expand Up @@ -374,7 +373,7 @@ class _WritablePath(_JoinablePath):
__slots__ = ()

@abstractmethod
def symlink_to(self: _WP, target: _WP, target_is_directory: bool = False) -> None:
def symlink_to(self, target: str, target_is_directory: bool = False) -> None:
"""
Make this path a symlink pointing to the target path.
Note the order of arguments (link, target) is the reverse of os.symlink.
Expand Down
Loading
0