From deb1d5a9f4b8c1b8c722e4ab844863469b882387 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 25 Oct 2024 13:17:22 -0400 Subject: [PATCH] type `Distribution.get_command_obj` to not return `None` with `create=True` --- distutils/dist.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/distutils/dist.py b/distutils/dist.py index ef4f4e02..e8c5236b 100644 --- a/distutils/dist.py +++ b/distutils/dist.py @@ -15,7 +15,7 @@ import warnings from collections.abc import Iterable from email import message_from_file -from typing import TYPE_CHECKING, TypeVar, overload +from typing import TYPE_CHECKING, Literal, TypeVar, overload from packaging.utils import canonicalize_name, canonicalize_version @@ -31,6 +31,7 @@ from .util import check_environ, rfc822_escape, strtobool if TYPE_CHECKING: + # type-only import because of mutual dependence between these modules from .cmd import Command _CommandT = TypeVar("_CommandT", bound="Command") @@ -837,7 +838,15 @@ def get_command_class(self, command): raise DistutilsModuleError(f"invalid command '{command}'") - def get_command_obj(self, command, create=True): + @overload + def get_command_obj( + self, command: str, create: Literal[True] = True + ) -> Command: ... + @overload + def get_command_obj( + self, command: str, create: Literal[False] + ) -> Command | None: ... + def get_command_obj(self, command: str, create: bool = True) -> Command | None: """Return the command object for 'command'. Normally this object is cached on a previous call to 'get_command_obj()'; if no command object for 'command' is in the cache, then we either create and