1- """PEP 517 build backend pre-building Cython exts before setuptools ."""
1+ """PEP 517 build backend wrapper for pre-building Cython for wheel ."""
22
33from __future__ import annotations
44
55import os
6- import typing as t # noqa: WPS111
7- from contextlib import contextmanager , suppress
6+ import typing as _t # noqa: WPS111
7+ from contextlib import contextmanager , nullcontext , suppress
88from functools import partial
99from pathlib import Path
1010from shutil import copytree
2424 build_editable as _setuptools_build_editable ,
2525 )
2626except ImportError :
27- _setuptools_build_editable = None
27+ _setuptools_build_editable = None # type: ignore[assignment]
2828
2929
3030# isort: split
4444 main as _cythonize_cli_cmd ,
4545 )
4646
47- from ._compat import chdir_cm , nullcontext_cm
47+ from ._compat import chdir_cm
4848from ._cython_configuration import (
4949 get_local_cythonize_config as _get_local_cython_config ,
5050 make_cythonize_cli_args_from_config as _make_cythonize_cli_args_from_config ,
5353from ._transformers import sanitize_rst_roles
5454
5555
56+ if _t .TYPE_CHECKING :
57+ import collections .abc as _c # noqa: WPS111, WPS301
58+
59+
5660__all__ = ( # noqa: PLE0604, WPS410
5761 'build_sdist' ,
5862 'build_wheel' ,
5963 'get_requires_for_build_wheel' ,
6064 'prepare_metadata_for_build_wheel' ,
6165 * (
6266 ()
63- if _setuptools_build_editable is None
67+ if _setuptools_build_editable is None # type: ignore[redundant-expr]
6468 else (
6569 'build_editable' ,
6670 'get_requires_for_build_editable' ,
7074)
7175
7276
77+ _ConfigDict = dict [str , str | list [str ] | None ]
78+
79+
7380CYTHON_TRACING_CONFIG_SETTING = 'with-cython-tracing' # noqa: WPS462
7481"""
7582Config setting name toggle to include line tracing to C-exts.
@@ -87,7 +94,7 @@ def _is_truthy_setting_value(setting_value: str) -> bool:
8794
8895
8996def _get_setting_value (
90- config_settings : dict [ str , str ] | None = None ,
97+ config_settings : _ConfigDict | None = None ,
9198 config_setting_name : str | None = None ,
9299 env_var_name : str | None = None ,
93100 * ,
@@ -102,15 +109,15 @@ def _get_setting_value(
102109 continue
103110
104111 with suppress (lookup_errors ): # type: ignore[arg-type]
105- return _is_truthy_setting_value (src_mapping [src_key ]) # type: ignore[index]
112+ return _is_truthy_setting_value (src_mapping [src_key ]) # type: ignore[arg-type, index]
106113
107114 return default
108115
109116
110117def _include_cython_line_tracing (
111- config_settings : dict [ str , str ] | None = None ,
118+ config_settings : _ConfigDict | None = None ,
112119 * ,
113- default = False ,
120+ default : bool = False ,
114121) -> bool :
115122 return _get_setting_value (
116123 config_settings ,
@@ -121,60 +128,65 @@ def _include_cython_line_tracing(
121128
122129
123130@contextmanager
124- def patched_distutils_cmd_install ():
131+ def patched_distutils_cmd_install () -> _c . Iterator [ None ] :
125132 """Make `install_lib` of `install` cmd always use `platlib`.
126133
127134 :yields: None
128135 """
129136 # Without this, build_lib puts stuff under `*.data/purelib/` folder
130137 orig_finalize = _distutils_install_cmd .finalize_options
131138
132- def new_finalize_options (self ): # noqa: WPS430
139+ def new_finalize_options ( # noqa: WPS430
140+ self : _distutils_install_cmd ,
141+ ) -> None :
133142 self .install_lib = self .install_platlib
134143 orig_finalize (self )
135144
136- _distutils_install_cmd .finalize_options = new_finalize_options
145+ _distutils_install_cmd .finalize_options = new_finalize_options # type: ignore[method-assign]
137146 try : # noqa: WPS501
138147 yield
139148 finally :
140- _distutils_install_cmd .finalize_options = orig_finalize
149+ _distutils_install_cmd .finalize_options = orig_finalize # type: ignore[method-assign]
141150
142151
143152@contextmanager
144- def patched_dist_has_ext_modules ():
153+ def patched_dist_has_ext_modules () -> _c . Iterator [ None ] :
145154 """Make `has_ext_modules` of `Distribution` always return `True`.
146155
147156 :yields: None
148157 """
149158 # Without this, build_lib puts stuff under `*.data/platlib/` folder
150159 orig_func = _DistutilsDistribution .has_ext_modules
151160
152- _DistutilsDistribution .has_ext_modules = lambda * _args , ** _kwargs : True
161+ _DistutilsDistribution .has_ext_modules = lambda * _args , ** _kwargs : True # type: ignore[method-assign]
153162 try : # noqa: WPS501
154163 yield
155164 finally :
156- _DistutilsDistribution .has_ext_modules = orig_func
165+ _DistutilsDistribution .has_ext_modules = orig_func # type: ignore[method-assign]
157166
158167
159168@contextmanager
160- def patched_dist_get_long_description ():
169+ def patched_dist_get_long_description () -> _c . Iterator [ None ] :
161170 """Make `has_ext_modules` of `Distribution` always return `True`.
162171
163172 :yields: None
164173 """
165174 # Without this, build_lib puts stuff under `*.data/platlib/` folder
166175 orig_func = _DistutilsDistributionMetadata .get_long_description
167176
168- def _get_sanitized_long_description (self ): # noqa: WPS430
177+ def _get_sanitized_long_description ( # noqa: WPS430
178+ self : _DistutilsDistributionMetadata ,
179+ ) -> str :
180+ assert self .long_description is not None # noqa: S101 # typing
169181 return sanitize_rst_roles (self .long_description )
170182
171- _DistutilsDistributionMetadata .get_long_description = (
183+ _DistutilsDistributionMetadata .get_long_description = ( # type: ignore[method-assign]
172184 _get_sanitized_long_description
173185 )
174186 try :
175187 yield
176188 finally :
177- _DistutilsDistributionMetadata .get_long_description = orig_func
189+ _DistutilsDistributionMetadata .get_long_description = orig_func # type: ignore[method-assign]
178190
179191
180192def _exclude_dir_path (
@@ -201,7 +213,7 @@ def _exclude_dir_path(
201213
202214
203215@contextmanager
204- def _in_temporary_directory (src_dir : Path ) -> t .Iterator [None ]:
216+ def _in_temporary_directory (src_dir : Path ) -> _c .Iterator [None ]:
205217 with TemporaryDirectory (prefix = '.tmp-ansible-pylibssh-pep517-' ) as tmp_dir :
206218 tmp_dir_path = Path (tmp_dir )
207219 root_tmp_dir_path = tmp_dir_path .parent
@@ -224,8 +236,8 @@ def _prebuild_c_extensions(
224236 * ,
225237 line_trace_cython_when_unset : bool = False ,
226238 build_inplace : bool = False ,
227- config_settings : dict [ str , str ] | None = None ,
228- ) -> t . Generator [None , t . Any , t . Any ]:
239+ config_settings : _ConfigDict | None = None ,
240+ ) -> _c . Iterator [None ]:
229241 """Pre-build C-extensions in a temporary directory, when needed.
230242
231243 This context manager also patches metadata, setuptools and distutils.
@@ -240,19 +252,21 @@ def _prebuild_c_extensions(
240252 )
241253
242254 build_dir_ctx = (
243- nullcontext_cm ()
255+ nullcontext ()
244256 if build_inplace
245257 else _in_temporary_directory (src_dir = Path .cwd ().resolve ())
246258 )
247259 with build_dir_ctx :
248260 config = _get_local_cython_config ()
249261
250- cythonize_args = _make_cythonize_cli_args_from_config (config )
262+ cythonize_args = _make_cythonize_cli_args_from_config (
263+ config ,
264+ )
251265 with _patched_cython_env (
252266 config ['env' ],
253267 cython_line_tracing_requested = cython_line_tracing_requested ,
254268 ):
255- _cythonize_cli_cmd (cythonize_args )
269+ _cythonize_cli_cmd (cythonize_args ) # type: ignore[no-untyped-call]
256270 with patched_distutils_cmd_install ():
257271 with patched_dist_has_ext_modules ():
258272 yield
@@ -261,7 +275,7 @@ def _prebuild_c_extensions(
261275@patched_dist_get_long_description ()
262276def build_wheel (
263277 wheel_directory : str ,
264- config_settings : dict [ str , str ] | None = None ,
278+ config_settings : _ConfigDict | None = None ,
265279 metadata_directory : str | None = None ,
266280) -> str :
267281 """Produce a built wheel.
@@ -288,7 +302,7 @@ def build_wheel(
288302@patched_dist_get_long_description ()
289303def build_editable (
290304 wheel_directory : str ,
291- config_settings : dict [ str , str ] | None = None ,
305+ config_settings : _ConfigDict | None = None ,
292306 metadata_directory : str | None = None ,
293307) -> str :
294308 """Produce a built wheel for editable installs.
@@ -313,7 +327,7 @@ def build_editable(
313327
314328
315329def get_requires_for_build_wheel (
316- config_settings : dict [ str , str ] | None = None ,
330+ config_settings : _ConfigDict | None = None ,
317331) -> list [str ]:
318332 """Determine additional requirements for building wheels.
319333
0 commit comments