6
6
from os .path import abspath
7
7
from os .path import join as pjoin
8
8
from subprocess import STDOUT , check_call , check_output
9
- from typing import TYPE_CHECKING , Any , Iterator , Mapping , Optional , Sequence
9
+ from typing import (
10
+ TYPE_CHECKING ,
11
+ Any ,
12
+ Iterator ,
13
+ List ,
14
+ Mapping ,
15
+ Optional ,
16
+ Sequence ,
17
+ Union ,
18
+ )
10
19
11
20
from ._in_process import _in_proc_script_path
12
21
22
+ StrPath = Union [str , "os.PathLike[str]" ]
23
+
13
24
if TYPE_CHECKING :
14
25
from typing import Protocol
15
26
@@ -18,19 +29,19 @@ class SubprocessRunner(Protocol):
18
29
19
30
def __call__ (
20
31
self ,
21
- cmd : Sequence [str ],
22
- cwd : Optional [str ] = None ,
32
+ cmd : Sequence [StrPath ],
33
+ cwd : Optional [StrPath ] = None ,
23
34
extra_environ : Optional [Mapping [str , str ]] = None ,
24
35
) -> None :
25
36
...
26
37
27
38
28
- def write_json (obj : Mapping [str , Any ], path : str , ** kwargs ) -> None :
39
+ def write_json (obj : Mapping [str , Any ], path : StrPath , ** kwargs ) -> None :
29
40
with open (path , "w" , encoding = "utf-8" ) as f :
30
41
json .dump (obj , f , ** kwargs )
31
42
32
43
33
- def read_json (path : str ) -> Mapping [str , Any ]:
44
+ def read_json (path : StrPath ) -> Mapping [str , Any ]:
34
45
with open (path , encoding = "utf-8" ) as f :
35
46
return json .load (f )
36
47
@@ -43,7 +54,7 @@ def __init__(
43
54
traceback : str ,
44
55
message : Optional [str ] = None ,
45
56
backend_name : Optional [str ] = None ,
46
- backend_path : Optional [Sequence [str ]] = None ,
57
+ backend_path : Optional [Sequence [StrPath ]] = None ,
47
58
) -> None :
48
59
# Preserving arg order for the sake of API backward compatibility.
49
60
self .backend_name = backend_name
@@ -68,8 +79,8 @@ def __init__(self, traceback: str) -> None:
68
79
69
80
70
81
def default_subprocess_runner (
71
- cmd : Sequence [str ],
72
- cwd : Optional [str ] = None ,
82
+ cmd : Sequence [StrPath ],
83
+ cwd : Optional [StrPath ] = None ,
73
84
extra_environ : Optional [Mapping [str , str ]] = None ,
74
85
) -> None :
75
86
"""The default method of calling the wrapper subprocess.
@@ -84,8 +95,8 @@ def default_subprocess_runner(
84
95
85
96
86
97
def quiet_subprocess_runner (
87
- cmd : Sequence [str ],
88
- cwd : Optional [str ] = None ,
98
+ cmd : Sequence [StrPath ],
99
+ cwd : Optional [StrPath ] = None ,
89
100
extra_environ : Optional [Mapping [str , str ]] = None ,
90
101
) -> None :
91
102
"""Call the subprocess while suppressing output.
@@ -99,7 +110,7 @@ def quiet_subprocess_runner(
99
110
check_output (cmd , cwd = cwd , env = env , stderr = STDOUT )
100
111
101
112
102
- def norm_and_check (source_tree : str , requested : str ) -> str :
113
+ def norm_and_check (source_tree : StrPath , requested : StrPath ) -> str :
103
114
"""Normalise and check a backend path.
104
115
105
116
Ensure that the requested backend path is specified as a relative path,
@@ -128,11 +139,11 @@ class BuildBackendHookCaller:
128
139
129
140
def __init__ (
130
141
self ,
131
- source_dir : str ,
142
+ source_dir : StrPath ,
132
143
build_backend : str ,
133
- backend_path : Optional [Sequence [str ]] = None ,
144
+ backend_path : Optional [Sequence [StrPath ]] = None ,
134
145
runner : Optional ["SubprocessRunner" ] = None ,
135
- python_executable : Optional [str ] = None ,
146
+ python_executable : Optional [StrPath ] = None ,
136
147
) -> None :
137
148
"""
138
149
:param source_dir: The source directory to invoke the build backend for
@@ -147,9 +158,11 @@ def __init__(
147
158
148
159
self .source_dir = abspath (source_dir )
149
160
self .build_backend = build_backend
150
- if backend_path :
151
- backend_path = [norm_and_check (self .source_dir , p ) for p in backend_path ]
152
- self .backend_path = backend_path
161
+ self .backend_path : Optional [List [str ]] = (
162
+ [norm_and_check (self .source_dir , p ) for p in backend_path ]
163
+ if backend_path is not None
164
+ else None
165
+ )
153
166
self ._subprocess_runner = runner
154
167
if not python_executable :
155
168
python_executable = sys .executable
@@ -199,7 +212,7 @@ def get_requires_for_build_wheel(
199
212
200
213
def prepare_metadata_for_build_wheel (
201
214
self ,
202
- metadata_directory : str ,
215
+ metadata_directory : StrPath ,
203
216
config_settings : Optional [Mapping [str , Any ]] = None ,
204
217
_allow_fallback : bool = True ,
205
218
) -> str :
@@ -232,9 +245,9 @@ def prepare_metadata_for_build_wheel(
232
245
233
246
def build_wheel (
234
247
self ,
235
- wheel_directory : str ,
248
+ wheel_directory : StrPath ,
236
249
config_settings : Optional [Mapping [str , Any ]] = None ,
237
- metadata_directory : Optional [str ] = None ,
250
+ metadata_directory : Optional [StrPath ] = None ,
238
251
) -> str :
239
252
"""Build a wheel from this project.
240
253
@@ -282,7 +295,7 @@ def get_requires_for_build_editable(
282
295
283
296
def prepare_metadata_for_build_editable (
284
297
self ,
285
- metadata_directory : str ,
298
+ metadata_directory : StrPath ,
286
299
config_settings : Optional [Mapping [str , Any ]] = None ,
287
300
_allow_fallback : bool = True ,
288
301
) -> Optional [str ]:
@@ -314,9 +327,9 @@ def prepare_metadata_for_build_editable(
314
327
315
328
def build_editable (
316
329
self ,
317
- wheel_directory : str ,
330
+ wheel_directory : StrPath ,
318
331
config_settings : Optional [Mapping [str , Any ]] = None ,
319
- metadata_directory : Optional [str ] = None ,
332
+ metadata_directory : Optional [StrPath ] = None ,
320
333
) -> str :
321
334
"""Build an editable wheel from this project.
322
335
@@ -359,7 +372,7 @@ def get_requires_for_build_sdist(
359
372
360
373
def build_sdist (
361
374
self ,
362
- sdist_directory : str ,
375
+ sdist_directory : StrPath ,
363
376
config_settings : Optional [Mapping [str , Any ]] = None ,
364
377
) -> str :
365
378
"""Build an sdist from this project.
0 commit comments