Skip to content

Fully type annotate pathlib.py #12229

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
Apr 20, 2024
Merged
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
22 changes: 14 additions & 8 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# mypy: allow-untyped-defs
import atexit
import contextlib
from enum import Enum
Expand All @@ -23,6 +22,7 @@
import sys
import types
from types import ModuleType
from typing import Any
from typing import Callable
from typing import Dict
from typing import Iterable
Expand Down Expand Up @@ -59,7 +59,7 @@
)


def _ignore_error(exception):
def _ignore_error(exception: Exception) -> bool:
return (
getattr(exception, "errno", None) in _IGNORED_ERRORS
or getattr(exception, "winerror", None) in _IGNORED_WINERRORS
Expand All @@ -71,7 +71,7 @@ def get_lock_path(path: _AnyPurePath) -> _AnyPurePath:


def on_rm_rf_error(
func,
func: Optional[Callable[..., Any]],
path: str,
excinfo: Union[
BaseException,
Expand Down Expand Up @@ -196,7 +196,7 @@ def find_suffixes(root: Path, prefix: str) -> Iterator[str]:
return extract_suffixes(find_prefixed(root, prefix), prefix)


def parse_num(maybe_num) -> int:
def parse_num(maybe_num: str) -> int:
"""Parse number path suffixes, returns -1 on error."""
try:
return int(maybe_num)
Expand Down Expand Up @@ -264,7 +264,9 @@ def create_cleanup_lock(p: Path) -> Path:
return lock_path


def register_cleanup_lock_removal(lock_path: Path, register=atexit.register):
def register_cleanup_lock_removal(
lock_path: Path, register: Any = atexit.register
) -> Any:
"""Register a cleanup function for removing a lock, by default on atexit."""
pid = os.getpid()

Expand Down Expand Up @@ -355,7 +357,7 @@ def cleanup_candidates(root: Path, prefix: str, keep: int) -> Iterator[Path]:
yield Path(entry)


def cleanup_dead_symlinks(root: Path):
def cleanup_dead_symlinks(root: Path) -> None:
for left_dir in root.iterdir():
if left_dir.is_symlink():
if not left_dir.resolve().exists():
Expand Down Expand Up @@ -459,10 +461,14 @@ def parts(s: str) -> Set[str]:
return {sep.join(parts[: i + 1]) or sep for i in range(len(parts))}


def symlink_or_skip(src, dst, **kwargs):
def symlink_or_skip(
src: Union["os.PathLike[str]", str],
dst: Union["os.PathLike[str]", str],
**kwargs: Any,
) -> None:
"""Make a symlink, or skip the test in case symlinks are not supported."""
try:
os.symlink(str(src), str(dst), **kwargs)
os.symlink(src, dst, **kwargs)
except OSError as e:
skip(f"symlinks not supported: {e}")

Expand Down