Skip to content

Commit f15e802

Browse files
committed
Merge commands overloads from typeshed
1 parent e7e35eb commit f15e802

File tree

5 files changed

+506
-31
lines changed

5 files changed

+506
-31
lines changed

setuptools/__init__.py

Lines changed: 219 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import sys
1313
from abc import abstractmethod
1414
from collections.abc import Mapping
15-
from typing import TYPE_CHECKING, TypeVar, overload
15+
from typing import TYPE_CHECKING, Literal, TypeVar, overload
1616

1717
sys.path.extend(((vendor_path := os.path.join(os.path.dirname(os.path.dirname(__file__)), 'setuptools', '_vendor')) not in sys.path) * [vendor_path]) # fmt: skip
1818
# workaround for #4476
@@ -30,6 +30,29 @@
3030

3131
import distutils.core
3232

33+
if TYPE_CHECKING:
34+
from .command.alias import alias
35+
from .command.bdist_egg import bdist_egg
36+
from .command.bdist_rpm import bdist_rpm
37+
from .command.bdist_wheel import bdist_wheel
38+
from .command.build import build
39+
from .command.build_clib import build_clib
40+
from .command.build_ext import build_ext
41+
from .command.build_py import build_py
42+
from .command.develop import develop
43+
from .command.dist_info import dist_info
44+
from .command.easy_install import easy_install
45+
from .command.editable_wheel import editable_wheel
46+
from .command.egg_info import egg_info
47+
from .command.install import install
48+
from .command.install_egg_info import install_egg_info
49+
from .command.install_lib import install_lib
50+
from .command.install_scripts import install_scripts
51+
from .command.rotate import rotate
52+
from .command.saveopts import saveopts
53+
from .command.sdist import sdist
54+
from .command.setopt import setopt
55+
3356
__all__ = [
3457
'setup',
3558
'Distribution',
@@ -175,6 +198,200 @@ def __init__(self, dist: Distribution, **kw) -> None:
175198
super().__init__(dist)
176199
vars(self).update(kw)
177200

201+
if TYPE_CHECKING:
202+
# Note: Commands that setuptools doesn't re-expose are considered deprecated (they must be imported from distutils directly)
203+
# So we're not listing them here. This list comes directly from the setuptools/command folder. Minus the test command.
204+
@overload # type: ignore[no-overload-impl] # This overrides from distutils
205+
def get_finalized_command( # pyright: ignore[reportNoOverloadImplementation] # This overrides form distutils
206+
self, command: Literal["alias"], create: bool = True
207+
) -> alias: ...
208+
@overload
209+
def get_finalized_command(
210+
self, command: Literal["bdist_egg"], create: bool = True
211+
) -> bdist_egg: ...
212+
@overload
213+
def get_finalized_command(
214+
self, command: Literal["bdist_rpm"], create: bool = True
215+
) -> bdist_rpm: ...
216+
@overload
217+
def get_finalized_command(
218+
self, command: Literal["bdist_wheel"], create: bool = True
219+
) -> bdist_wheel: ...
220+
@overload
221+
def get_finalized_command(
222+
self, command: Literal["build"], create: bool = True
223+
) -> build: ...
224+
@overload
225+
def get_finalized_command(
226+
self, command: Literal["build_clib"], create: bool = True
227+
) -> build_clib: ...
228+
@overload
229+
def get_finalized_command(
230+
self, command: Literal["build_ext"], create: bool = True
231+
) -> build_ext: ...
232+
@overload
233+
def get_finalized_command(
234+
self, command: Literal["build_py"], create: bool = True
235+
) -> build_py: ...
236+
@overload
237+
def get_finalized_command(
238+
self, command: Literal["develop"], create: bool = True
239+
) -> develop: ...
240+
@overload
241+
def get_finalized_command(
242+
self, command: Literal["dist_info"], create: bool = True
243+
) -> dist_info: ...
244+
@overload
245+
def get_finalized_command(
246+
self, command: Literal["easy_install"], create: bool = True
247+
) -> easy_install: ...
248+
@overload
249+
def get_finalized_command(
250+
self, command: Literal["editable_wheel"], create: bool = True
251+
) -> editable_wheel: ...
252+
@overload
253+
def get_finalized_command(
254+
self, command: Literal["egg_info"], create: bool = True
255+
) -> egg_info: ...
256+
@overload
257+
def get_finalized_command(
258+
self, command: Literal["install"], create: bool = True
259+
) -> install: ...
260+
@overload
261+
def get_finalized_command(
262+
self, command: Literal["install_egg_info"], create: bool = True
263+
) -> install_egg_info: ...
264+
@overload
265+
def get_finalized_command(
266+
self, command: Literal["install_lib"], create: bool = True
267+
) -> install_lib: ...
268+
@overload
269+
def get_finalized_command(
270+
self, command: Literal["install_scripts"], create: bool = True
271+
) -> install_scripts: ...
272+
@overload
273+
def get_finalized_command(
274+
self, command: Literal["rotate"], create: bool = True
275+
) -> rotate: ...
276+
@overload
277+
def get_finalized_command(
278+
self, command: Literal["saveopts"], create: bool = True
279+
) -> saveopts: ...
280+
@overload
281+
def get_finalized_command(
282+
self, command: Literal["sdist"], create: bool = True
283+
) -> sdist: ...
284+
@overload
285+
def get_finalized_command(
286+
self, command: Literal["setopt"], create: bool = True
287+
) -> setopt: ...
288+
@overload
289+
def get_finalized_command(
290+
self, command: str, create: bool = True
291+
) -> Command: ...
292+
293+
@overload
294+
def reinitialize_command(
295+
self, command: Literal["alias"], reinit_subcommands: bool = False, **kw
296+
) -> alias: ...
297+
@overload
298+
def reinitialize_command(
299+
self, command: Literal["bdist_egg"], reinit_subcommands: bool = False, **kw
300+
) -> bdist_egg: ...
301+
@overload
302+
def reinitialize_command(
303+
self, command: Literal["bdist_rpm"], reinit_subcommands: bool = False, **kw
304+
) -> bdist_rpm: ...
305+
@overload
306+
def reinitialize_command(
307+
self,
308+
command: Literal["bdist_wheel"],
309+
reinit_subcommands: bool = False,
310+
**kw,
311+
) -> bdist_wheel: ...
312+
@overload
313+
def reinitialize_command(
314+
self, command: Literal["build"], reinit_subcommands: bool = False, **kw
315+
) -> build: ...
316+
@overload
317+
def reinitialize_command(
318+
self, command: Literal["build_clib"], reinit_subcommands: bool = False, **kw
319+
) -> build_clib: ...
320+
@overload
321+
def reinitialize_command(
322+
self, command: Literal["build_ext"], reinit_subcommands: bool = False, **kw
323+
) -> build_ext: ...
324+
@overload
325+
def reinitialize_command(
326+
self, command: Literal["build_py"], reinit_subcommands: bool = False, **kw
327+
) -> build_py: ...
328+
@overload
329+
def reinitialize_command(
330+
self, command: Literal["develop"], reinit_subcommands: bool = False, **kw
331+
) -> develop: ...
332+
@overload
333+
def reinitialize_command(
334+
self, command: Literal["dist_info"], reinit_subcommands: bool = False, **kw
335+
) -> dist_info: ...
336+
@overload
337+
def reinitialize_command(
338+
self,
339+
command: Literal["easy_install"],
340+
reinit_subcommands: bool = False,
341+
**kw,
342+
) -> easy_install: ...
343+
@overload
344+
def reinitialize_command(
345+
self,
346+
command: Literal["editable_wheel"],
347+
reinit_subcommands: bool = False,
348+
**kw,
349+
) -> editable_wheel: ...
350+
@overload
351+
def reinitialize_command(
352+
self, command: Literal["egg_info"], reinit_subcommands: bool = False, **kw
353+
) -> egg_info: ...
354+
@overload
355+
def reinitialize_command(
356+
self, command: Literal["install"], reinit_subcommands: bool = False, **kw
357+
) -> install: ...
358+
@overload
359+
def reinitialize_command(
360+
self,
361+
command: Literal["install_egg_info"],
362+
reinit_subcommands: bool = False,
363+
**kw,
364+
) -> install_egg_info: ...
365+
@overload
366+
def reinitialize_command(
367+
self,
368+
command: Literal["install_lib"],
369+
reinit_subcommands: bool = False,
370+
**kw,
371+
) -> install_lib: ...
372+
@overload
373+
def reinitialize_command(
374+
self,
375+
command: Literal["install_scripts"],
376+
reinit_subcommands: bool = False,
377+
**kw,
378+
) -> install_scripts: ...
379+
@overload
380+
def reinitialize_command(
381+
self, command: Literal["rotate"], reinit_subcommands: bool = False, **kw
382+
) -> rotate: ...
383+
@overload
384+
def reinitialize_command(
385+
self, command: Literal["saveopts"], reinit_subcommands: bool = False, **kw
386+
) -> saveopts: ...
387+
@overload
388+
def reinitialize_command(
389+
self, command: Literal["sdist"], reinit_subcommands: bool = False, **kw
390+
) -> sdist: ...
391+
@overload
392+
def reinitialize_command(
393+
self, command: Literal["setopt"], reinit_subcommands: bool = False, **kw
394+
) -> setopt: ...
178395
@overload
179396
def reinitialize_command(
180397
self, command: str, reinit_subcommands: bool = False, **kw
@@ -188,7 +405,7 @@ def reinitialize_command(
188405
) -> Command | _Command:
189406
cmd = _Command.reinitialize_command(self, command, reinit_subcommands)
190407
vars(cmd).update(kw)
191-
return cmd # pyright: ignore[reportReturnType] # pypa/distutils#307
408+
return cmd
192409

193410
@abstractmethod
194411
def initialize_options(self) -> None:

setuptools/command/bdist_wheel.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from .._core_metadata import _safe_license_file
2727
from .._normalization import safer_name
2828
from ..warnings import SetuptoolsDeprecationWarning
29-
from .egg_info import egg_info as egg_info_cls
3029

3130
from distutils import log
3231

@@ -233,7 +232,7 @@ def finalize_options(self) -> None:
233232
self.bdist_dir = os.path.join(bdist_base, "wheel")
234233

235234
if self.dist_info_dir is None:
236-
egg_info = cast(egg_info_cls, self.distribution.get_command_obj("egg_info"))
235+
egg_info = self.distribution.get_command_obj("egg_info")
237236
egg_info.ensure_finalized() # needed for correct `wheel_dist_name`
238237

239238
self.data_dir = self.wheel_dist_name + ".data"

setuptools/command/dist_info.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import shutil
88
from contextlib import contextmanager
99
from pathlib import Path
10-
from typing import cast
10+
from typing import TYPE_CHECKING
1111

1212
from .. import _normalization
1313
from .._shutil import rmdir as _rm
14-
from .egg_info import egg_info as egg_info_cls
1514

1615
from distutils import log
17-
from distutils.core import Command
16+
17+
# Pretend dist_info subclasses setuptools.Command so we get all overrides when type-checking
18+
if TYPE_CHECKING:
19+
from setuptools import Command
20+
else:
21+
from distutils.core import Command
1822

1923

2024
class dist_info(Command):
@@ -54,7 +58,7 @@ def finalize_options(self) -> None:
5458
project_dir = dist.src_root or os.curdir
5559
self.output_dir = Path(self.output_dir or project_dir)
5660

57-
egg_info = cast(egg_info_cls, self.reinitialize_command("egg_info"))
61+
egg_info = self.reinitialize_command("egg_info")
5862
egg_info.egg_base = str(self.output_dir)
5963

6064
if self.tag_date:

setuptools/command/editable_wheel.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,15 @@
2626
from pathlib import Path
2727
from tempfile import TemporaryDirectory
2828
from types import TracebackType
29-
from typing import TYPE_CHECKING, Protocol, TypeVar, cast
29+
from typing import TYPE_CHECKING, Protocol, TypeVar
3030

3131
from .. import Command, _normalization, _path, _shutil, errors, namespaces
3232
from .._path import StrPath
3333
from ..compat import py310, py312
3434
from ..discovery import find_package_path
3535
from ..dist import Distribution
3636
from ..warnings import InformationOnly, SetuptoolsDeprecationWarning
37-
from .build import build as build_cls
3837
from .build_py import build_py as build_py_cls
39-
from .dist_info import dist_info as dist_info_cls
40-
from .egg_info import egg_info as egg_info_cls
41-
from .install import install as install_cls
42-
from .install_scripts import install_scripts as install_scripts_cls
4338

4439
if TYPE_CHECKING:
4540
from typing_extensions import Self
@@ -150,7 +145,7 @@ def run(self) -> None:
150145

151146
def _ensure_dist_info(self):
152147
if self.dist_info_dir is None:
153-
dist_info = cast(dist_info_cls, self.reinitialize_command("dist_info"))
148+
dist_info = self.reinitialize_command("dist_info")
154149
dist_info.output_dir = self.dist_dir
155150
dist_info.ensure_finalized()
156151
dist_info.run()
@@ -197,18 +192,12 @@ def _configure_build(
197192
scripts = str(Path(unpacked_wheel, f"{name}.data", "scripts"))
198193

199194
# egg-info may be generated again to create a manifest (used for package data)
200-
egg_info = cast(
201-
egg_info_cls, dist.reinitialize_command("egg_info", reinit_subcommands=True)
202-
)
195+
egg_info = dist.reinitialize_command("egg_info", reinit_subcommands=True)
203196
egg_info.egg_base = str(tmp_dir)
204197
egg_info.ignore_egg_info_in_manifest = True
205198

206-
build = cast(
207-
build_cls, dist.reinitialize_command("build", reinit_subcommands=True)
208-
)
209-
install = cast(
210-
install_cls, dist.reinitialize_command("install", reinit_subcommands=True)
211-
)
199+
build = dist.reinitialize_command("build", reinit_subcommands=True)
200+
install = dist.reinitialize_command("install", reinit_subcommands=True)
212201

213202
build.build_platlib = build.build_purelib = build.build_lib = build_lib
214203
install.install_purelib = install.install_platlib = install.install_lib = wheel
@@ -221,14 +210,12 @@ def _configure_build(
221210
build_scripts = dist.get_command_obj("build_scripts")
222211
build_scripts.executable = 'python'
223212

224-
install_scripts = cast(
225-
install_scripts_cls, dist.get_command_obj("install_scripts")
226-
)
213+
install_scripts = dist.get_command_obj("install_scripts")
227214
install_scripts.no_ep = True
228215

229216
build.build_temp = str(tmp_dir)
230217

231-
build_py = cast(build_py_cls, dist.get_command_obj("build_py"))
218+
build_py = dist.get_command_obj("build_py")
232219
build_py.compile = False
233220
build_py.existing_egg_info_dir = self._find_egg_info_dir()
234221

0 commit comments

Comments
 (0)