Skip to content

Merge typeshed's setuptools._distutils annotations #329

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 13 commits into from
Mar 9, 2025
30 changes: 26 additions & 4 deletions distutils/_modified.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
"""Timestamp comparison of files and groups of files."""

from __future__ import annotations

import functools
import os.path
from collections.abc import Callable, Iterable
from typing import Literal, TypeVar

from jaraco.functools import splat

from .compat.py39 import zip_strict
from .errors import DistutilsFileError

_SourcesT = TypeVar(
"_SourcesT", bound="str | bytes | os.PathLike[str] | os.PathLike[bytes]"
)
_TargetsT = TypeVar(
"_TargetsT", bound="str | bytes | os.PathLike[str] | os.PathLike[bytes]"
)


def _newer(source, target):
return not os.path.exists(target) or (
os.path.getmtime(source) > os.path.getmtime(target)
)


def newer(source, target):
def newer(
source: str | bytes | os.PathLike[str] | os.PathLike[bytes],
target: str | bytes | os.PathLike[str] | os.PathLike[bytes],
) -> bool:
"""
Is source modified more recently than target.

@@ -25,12 +39,16 @@ def newer(source, target):
Raises DistutilsFileError if 'source' does not exist.
"""
if not os.path.exists(source):
raise DistutilsFileError(f"file '{os.path.abspath(source)}' does not exist")
raise DistutilsFileError(f"file {os.path.abspath(source)!r} does not exist")

return _newer(source, target)


def newer_pairwise(sources, targets, newer=newer):
def newer_pairwise(
sources: Iterable[_SourcesT],
targets: Iterable[_TargetsT],
newer: Callable[[_SourcesT, _TargetsT], bool] = newer,
) -> tuple[list[_SourcesT], list[_TargetsT]]:
"""
Filter filenames where sources are newer than targets.

@@ -43,7 +61,11 @@ def newer_pairwise(sources, targets, newer=newer):
return tuple(map(list, zip(*newer_pairs))) or ([], [])


def newer_group(sources, target, missing='error'):
def newer_group(
sources: Iterable[str | bytes | os.PathLike[str] | os.PathLike[bytes]],
target: str | bytes | os.PathLike[str] | os.PathLike[bytes],
missing: Literal["error", "ignore", "newer"] = "error",
) -> bool:
"""
Is target out-of-date with respect to any file in sources.

66 changes: 48 additions & 18 deletions distutils/archive_util.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,10 @@
Utility functions for creating archive files (tarballs, zip files,
that sort of thing)."""

from __future__ import annotations

import os
from typing import Literal, overload

try:
import zipfile
@@ -54,14 +57,14 @@ def _get_uid(name):


def make_tarball(
base_name,
base_dir,
compress="gzip",
verbose=False,
dry_run=False,
owner=None,
group=None,
):
base_name: str,
base_dir: str | os.PathLike[str],
compress: Literal["gzip", "bzip2", "xz"] | None = "gzip",
verbose: bool = False,
dry_run: bool = False,
owner: str | None = None,
group: str | None = None,
) -> str:
"""Create a (possibly compressed) tar file from all the files under
'base_dir'.

@@ -122,7 +125,12 @@ def _set_uid_gid(tarinfo):
return archive_name


def make_zipfile(base_name, base_dir, verbose=False, dry_run=False): # noqa: C901
def make_zipfile( # noqa: C901
base_name: str,
base_dir: str | os.PathLike[str],
verbose: bool = False,
dry_run: bool = False,
) -> str:
"""Create a zip file from all the files under 'base_dir'.

The output zip file will be named 'base_name' + ".zip". Uses either the
@@ -204,16 +212,38 @@ def check_archive_formats(formats):
return None


@overload
def make_archive(
base_name: str,
format: str,
root_dir: str | os.PathLike[str] | bytes | os.PathLike[bytes] | None = None,
base_dir: str | None = None,
verbose: bool = False,
dry_run: bool = False,
owner: str | None = None,
group: str | None = None,
) -> str: ...
@overload
def make_archive(
base_name: str | os.PathLike[str],
format: str,
root_dir: str | os.PathLike[str] | bytes | os.PathLike[bytes],
base_dir: str | None = None,
verbose: bool = False,
dry_run: bool = False,
owner: str | None = None,
group: str | None = None,
) -> str: ...
def make_archive(
base_name,
format,
root_dir=None,
base_dir=None,
verbose=False,
dry_run=False,
owner=None,
group=None,
):
base_name: str | os.PathLike[str],
format: str,
root_dir: str | os.PathLike[str] | bytes | os.PathLike[bytes] | None = None,
base_dir: str | None = None,
verbose: bool = False,
dry_run: bool = False,
owner: str | None = None,
group: str | None = None,
) -> str:
"""Create an archive file (eg. zip or tar).

'base_name' is the name of the file to create, minus any format-specific
Loading