Skip to content

Commit c44fb1e

Browse files
committed
bdist_wheel typing improvement
1 parent 52d7324 commit c44fb1e

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

mypy.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exclude = (?x)(
1414
| ^.+?/(_vendor|extern)/ # Vendored
1515
| ^setuptools/_distutils/ # Vendored
1616
| ^setuptools/config/_validate_pyproject/ # Auto-generated
17-
| ^setuptools/tests/bdist_wheel_testdata/ # Duplicate module name
17+
| ^setuptools/tests/bdist_wheel_testdata/ # Duplicate module name
1818
)
1919

2020
# Ignoring attr-defined because setuptools wraps a lot of distutils classes, adding new attributes,

setuptools/command/bdist_wheel.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
from zipfile import ZIP_DEFLATED, ZIP_STORED
2424

2525
from .. import Command, __version__
26+
from .egg_info import egg_info as egg_info_cls
2627
from ..extern.wheel.metadata import pkginfo_to_metadata
2728
from ..extern.packaging import tags
2829
from ..extern.packaging import version as _packaging_version
2930
from ..extern.wheel.wheelfile import WheelFile
3031

3132
if TYPE_CHECKING:
32-
import types
33+
from _typeshed import ExcInfo
3334

3435

3536
def safe_name(name: str) -> str:
@@ -152,12 +153,14 @@ def safer_version(version: str) -> str:
152153
def remove_readonly(
153154
func: Callable[..., object],
154155
path: str,
155-
excinfo: tuple[type[Exception], Exception, types.TracebackType],
156+
excinfo: ExcInfo,
156157
) -> None:
157158
remove_readonly_exc(func, path, excinfo[1])
158159

159160

160-
def remove_readonly_exc(func: Callable[..., object], path: str, exc: Exception) -> None:
161+
def remove_readonly_exc(
162+
func: Callable[..., object], path: str, exc: BaseException
163+
) -> None:
161164
os.chmod(path, stat.S_IWRITE)
162165
func(path)
163166

@@ -232,40 +235,47 @@ class bdist_wheel(Command):
232235

233236
def initialize_options(self) -> None:
234237
self.bdist_dir: str | None = None
235-
self.data_dir = None
238+
self.data_dir: str | None = None
236239
self.plat_name: str | None = None
237-
self.plat_tag = None
240+
self.plat_tag: str | None = None
238241
self.format = "zip"
239242
self.keep_temp = False
240243
self.dist_dir: str | None = None
241-
self.egginfo_dir = None
244+
self.egginfo_dir: str | None = None
242245
self.root_is_pure: bool | None = None
243-
self.skip_build = None
246+
self.skip_build = False
244247
self.relative = False
245248
self.owner = None
246249
self.group = None
247250
self.universal: bool = False
248-
self.compression: str | int = "deflated"
251+
self.compression: int = ZIP_DEFLATED
249252
self.python_tag: str = python_tag()
250253
self.build_number: str | None = None
251254
self.py_limited_api: str | Literal[False] = False
252255
self.plat_name_supplied = False
253256

254-
def finalize_options(self):
255-
if self.bdist_dir is None:
257+
def finalize_options(self) -> None:
258+
if not self.bdist_dir:
256259
bdist_base = self.get_finalized_command("bdist").bdist_base
257260
self.bdist_dir = os.path.join(bdist_base, "wheel")
258261

259-
egg_info = self.distribution.get_command_obj("egg_info")
262+
egg_info = cast(egg_info_cls, self.distribution.get_command_obj("egg_info"))
260263
egg_info.ensure_finalized() # needed for correct `wheel_dist_name`
261264

262265
self.data_dir = self.wheel_dist_name + ".data"
263-
self.plat_name_supplied = self.plat_name is not None
266+
self.plat_name_supplied = bool(self.plat_name)
264267

265-
try:
266-
self.compression = self.supported_compressions[self.compression]
267-
except KeyError:
268-
raise ValueError(f"Unsupported compression: {self.compression}") from None
268+
# Handle compression not being an int or a supported value
269+
if not (
270+
isinstance(self.compression, int)
271+
and self.compression in self.supported_compressions.values()
272+
):
273+
try:
274+
self.compression = self.supported_compressions[str(self.compression)]
275+
except KeyError:
276+
raise ValueError(
277+
f"Unsupported compression: {self.compression}"
278+
) from None
269279

270280
need_options = ("dist_dir", "plat_name", "skip_build")
271281

@@ -295,21 +305,21 @@ def finalize_options(self):
295305
raise ValueError("Build tag (build-number) must start with a digit.")
296306

297307
@property
298-
def wheel_dist_name(self):
308+
def wheel_dist_name(self) -> str:
299309
"""Return distribution full name with - replaced with _"""
300-
components = (
310+
components = [
301311
safer_name(self.distribution.get_name()),
302312
safer_version(self.distribution.get_version()),
303-
)
313+
]
304314
if self.build_number:
305-
components += (self.build_number,)
315+
components.append(self.build_number)
306316
return "-".join(components)
307317

308318
def get_tag(self) -> tuple[str, str, str]:
309319
# bdist sets self.plat_name if unset, we should only use it for purepy
310320
# wheels if the user supplied it.
311-
if self.plat_name_supplied:
312-
plat_name = cast(str, self.plat_name)
321+
if self.plat_name_supplied and self.plat_name:
322+
plat_name = self.plat_name
313323
elif self.root_is_pure:
314324
plat_name = "any"
315325
else:
@@ -453,7 +463,7 @@ def run(self):
453463

454464
def write_wheelfile(
455465
self, wheelfile_base: str, generator: str = f"setuptools ({__version__})"
456-
):
466+
) -> None:
457467
from email.message import Message
458468

459469
msg = Message()
@@ -527,7 +537,7 @@ def license_paths(self) -> Iterable[str]:
527537

528538
return files
529539

530-
def egg2dist(self, egginfo_path: str, distinfo_path: str):
540+
def egg2dist(self, egginfo_path: str, distinfo_path: str) -> None:
531541
"""Convert an .egg-info directory into a .dist-info directory"""
532542

533543
def adios(p: str) -> None:

tools/vendored.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def rewrite_wheel(pkg_files: Path):
117117

118118
# Rewrite vendored imports to use setuptools's own vendored libraries
119119
for path in pkg_files.iterdir():
120+
# Upstream issue: https://github.com/jaraco/path/issues/226
120121
if path.suffix == '.py': # type: ignore[attr-defined]
121122
code = path.read_text()
122123
if path.name == 'wheelfile.py':
@@ -154,7 +155,7 @@ class WheelError(Exception):
154155
code,
155156
flags=re.MULTILINE,
156157
)
157-
158+
# Upstream issue: https://github.com/jaraco/path/issues/226
158159
path.write_text(code) # type: ignore[attr-defined]
159160

160161

0 commit comments

Comments
 (0)