Skip to content

Commit 5d2c034

Browse files
committed
Make reinitialize_command's return type Generic when "command" argument is a Command
1 parent 2517976 commit 5d2c034

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

distutils/cmd.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
in the distutils.command package.
55
"""
66

7+
from __future__ import annotations
8+
79
import logging
810
import os
911
import re
1012
import sys
13+
from typing import TypeVar, overload
1114

1215
from . import _modified, archive_util, dir_util, file_util, util
1316
from ._log import log
1417
from .errors import DistutilsOptionError
1518

19+
_CommandT = TypeVar("_CommandT", bound="Command")
20+
1621

1722
class Command:
1823
"""Abstract base class for defining command classes, the "worker bees"
@@ -305,7 +310,17 @@ def get_finalized_command(self, command, create=True):
305310

306311
# XXX rename to 'get_reinitialized_command()'? (should do the
307312
# same in dist.py, if so)
308-
def reinitialize_command(self, command, reinit_subcommands=False):
313+
@overload
314+
def reinitialize_command(
315+
self, command: str, reinit_subcommands: bool = False
316+
) -> Command: ...
317+
@overload
318+
def reinitialize_command(
319+
self, command: _CommandT, reinit_subcommands: bool = False
320+
) -> _CommandT: ...
321+
def reinitialize_command(
322+
self, command: str | Command, reinit_subcommands=False
323+
) -> Command:
309324
return self.distribution.reinitialize_command(command, reinit_subcommands)
310325

311326
def run_command(self, command):

distutils/dist.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
being built/installed/distributed.
55
"""
66

7+
from __future__ import annotations
8+
79
import contextlib
810
import logging
911
import os
@@ -13,6 +15,7 @@
1315
import warnings
1416
from collections.abc import Iterable
1517
from email import message_from_file
18+
from typing import TYPE_CHECKING, TypeVar, overload
1619

1720
from packaging.utils import canonicalize_name, canonicalize_version
1821

@@ -27,6 +30,11 @@
2730
from .fancy_getopt import FancyGetopt, translate_longopt
2831
from .util import check_environ, rfc822_escape, strtobool
2932

33+
if TYPE_CHECKING:
34+
from .cmd import Command
35+
36+
_CommandT = TypeVar("_CommandT", bound="Command")
37+
3038
# Regex to define acceptable Distutils command names. This is not *quite*
3139
# the same as a Python NAME -- I don't allow leading underscores. The fact
3240
# that they're very similar is no coincidence; the default naming scheme is
@@ -900,7 +908,17 @@ def _set_command_options(self, command_obj, option_dict=None): # noqa: C901
900908
except ValueError as msg:
901909
raise DistutilsOptionError(msg)
902910

903-
def reinitialize_command(self, command, reinit_subcommands=False):
911+
@overload
912+
def reinitialize_command(
913+
self, command: str, reinit_subcommands: bool = False
914+
) -> Command: ...
915+
@overload
916+
def reinitialize_command(
917+
self, command: _CommandT, reinit_subcommands: bool = False
918+
) -> _CommandT: ...
919+
def reinitialize_command(
920+
self, command: str | Command, reinit_subcommands=False
921+
) -> Command:
904922
"""Reinitializes a command to the state it was in when first
905923
returned by 'get_command_obj()': ie., initialized but not yet
906924
finalized. This provides the opportunity to sneak option

0 commit comments

Comments
 (0)