Skip to content

typing: allow PathLike objects in addition to strs #181

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 1 commit into
base: main
Choose a base branch
from
Open
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
61 changes: 37 additions & 24 deletions src/pyproject_hooks/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
from os.path import abspath
from os.path import join as pjoin
from subprocess import STDOUT, check_call, check_output
from typing import TYPE_CHECKING, Any, Iterator, Mapping, Optional, Sequence
from typing import (
TYPE_CHECKING,
Any,
Iterator,
List,
Mapping,
Optional,
Sequence,
Union,
)

from ._in_process import _in_proc_script_path

StrPath = Union[str, "os.PathLike[str]"]

if TYPE_CHECKING:
from typing import Protocol

Expand All @@ -18,19 +29,19 @@ class SubprocessRunner(Protocol):

def __call__(
self,
cmd: Sequence[str],
cwd: Optional[str] = None,
cmd: Sequence[StrPath],
cwd: Optional[StrPath] = None,
extra_environ: Optional[Mapping[str, str]] = None,
) -> None:
...


def write_json(obj: Mapping[str, Any], path: str, **kwargs) -> None:
def write_json(obj: Mapping[str, Any], path: StrPath, **kwargs) -> None:
with open(path, "w", encoding="utf-8") as f:
json.dump(obj, f, **kwargs)


def read_json(path: str) -> Mapping[str, Any]:
def read_json(path: StrPath) -> Mapping[str, Any]:
with open(path, encoding="utf-8") as f:
return json.load(f)

Expand All @@ -43,7 +54,7 @@ def __init__(
traceback: str,
message: Optional[str] = None,
backend_name: Optional[str] = None,
backend_path: Optional[Sequence[str]] = None,
backend_path: Optional[Sequence[StrPath]] = None,
) -> None:
# Preserving arg order for the sake of API backward compatibility.
self.backend_name = backend_name
Expand All @@ -68,8 +79,8 @@ def __init__(self, traceback: str) -> None:


def default_subprocess_runner(
cmd: Sequence[str],
cwd: Optional[str] = None,
cmd: Sequence[StrPath],
cwd: Optional[StrPath] = None,
extra_environ: Optional[Mapping[str, str]] = None,
) -> None:
"""The default method of calling the wrapper subprocess.
Expand All @@ -84,8 +95,8 @@ def default_subprocess_runner(


def quiet_subprocess_runner(
cmd: Sequence[str],
cwd: Optional[str] = None,
cmd: Sequence[StrPath],
cwd: Optional[StrPath] = None,
extra_environ: Optional[Mapping[str, str]] = None,
) -> None:
"""Call the subprocess while suppressing output.
Expand All @@ -99,7 +110,7 @@ def quiet_subprocess_runner(
check_output(cmd, cwd=cwd, env=env, stderr=STDOUT)


def norm_and_check(source_tree: str, requested: str) -> str:
def norm_and_check(source_tree: StrPath, requested: StrPath) -> str:
"""Normalise and check a backend path.

Ensure that the requested backend path is specified as a relative path,
Expand Down Expand Up @@ -128,11 +139,11 @@ class BuildBackendHookCaller:

def __init__(
self,
source_dir: str,
source_dir: StrPath,
build_backend: str,
backend_path: Optional[Sequence[str]] = None,
backend_path: Optional[Sequence[StrPath]] = None,
runner: Optional["SubprocessRunner"] = None,
python_executable: Optional[str] = None,
python_executable: Optional[StrPath] = None,
) -> None:
"""
:param source_dir: The source directory to invoke the build backend for
Expand All @@ -147,9 +158,11 @@ def __init__(

self.source_dir = abspath(source_dir)
self.build_backend = build_backend
if backend_path:
backend_path = [norm_and_check(self.source_dir, p) for p in backend_path]
self.backend_path = backend_path
self.backend_path: Optional[List[str]] = (
[norm_and_check(self.source_dir, p) for p in backend_path]
if backend_path is not None
else None
)
self._subprocess_runner = runner
if not python_executable:
python_executable = sys.executable
Expand Down Expand Up @@ -199,7 +212,7 @@ def get_requires_for_build_wheel(

def prepare_metadata_for_build_wheel(
self,
metadata_directory: str,
metadata_directory: StrPath,
config_settings: Optional[Mapping[str, Any]] = None,
_allow_fallback: bool = True,
) -> str:
Expand Down Expand Up @@ -232,9 +245,9 @@ def prepare_metadata_for_build_wheel(

def build_wheel(
self,
wheel_directory: str,
wheel_directory: StrPath,
config_settings: Optional[Mapping[str, Any]] = None,
metadata_directory: Optional[str] = None,
metadata_directory: Optional[StrPath] = None,
) -> str:
"""Build a wheel from this project.

Expand Down Expand Up @@ -282,7 +295,7 @@ def get_requires_for_build_editable(

def prepare_metadata_for_build_editable(
self,
metadata_directory: str,
metadata_directory: StrPath,
config_settings: Optional[Mapping[str, Any]] = None,
_allow_fallback: bool = True,
) -> Optional[str]:
Expand Down Expand Up @@ -314,9 +327,9 @@ def prepare_metadata_for_build_editable(

def build_editable(
self,
wheel_directory: str,
wheel_directory: StrPath,
config_settings: Optional[Mapping[str, Any]] = None,
metadata_directory: Optional[str] = None,
metadata_directory: Optional[StrPath] = None,
) -> str:
"""Build an editable wheel from this project.

Expand Down Expand Up @@ -359,7 +372,7 @@ def get_requires_for_build_sdist(

def build_sdist(
self,
sdist_directory: str,
sdist_directory: StrPath,
config_settings: Optional[Mapping[str, Any]] = None,
) -> str:
"""Build an sdist from this project.
Expand Down