From 61c9f88d84f659c8964b8a209b7ea89475d7ad31 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 22 Jul 2024 02:58:29 -0400 Subject: [PATCH] Merge overloaded method definitions from typeshed --- setuptools/__init__.py | 22 +++++++++++++++++----- setuptools/command/test.py | 19 ++++++++++++++++--- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/setuptools/__init__.py b/setuptools/__init__.py index afca08be9c..69c1f5acb9 100644 --- a/setuptools/__init__.py +++ b/setuptools/__init__.py @@ -1,10 +1,13 @@ """Extensions to the 'distutils' for large or complex distributions""" +from __future__ import annotations + import functools import os import re import sys -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, TypeVar, overload + 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 # workaround for #4476 @@ -15,7 +18,7 @@ from distutils.errors import DistutilsOptionError from . import logging, monkey -from . import version as _version_module +from .version import __version__ as __version__ from .depends import Require from .discovery import PackageFinder, PEP420PackageFinder from .dist import Distribution @@ -33,11 +36,10 @@ 'find_namespace_packages', ] -__version__ = _version_module.__version__ +_CommandT = TypeVar("_CommandT", bound="_Command") bootstrap_install_from = None - find_packages = PackageFinder.find find_namespace_packages = PEP420PackageFinder.find @@ -221,7 +223,17 @@ def ensure_string_list(self, option): "'%s' must be a list of strings (got %r)" % (option, val) ) - def reinitialize_command(self, command, reinit_subcommands=False, **kw): + @overload # type:ignore[override] # Extra **kw param + def reinitialize_command( + self, command: str, reinit_subcommands: bool = False, **kw + ) -> _Command: ... + @overload + def reinitialize_command( + self, command: _CommandT, reinit_subcommands: bool = False, **kw + ) -> _CommandT: ... + def reinitialize_command( + self, command: str | _Command, reinit_subcommands: bool = False, **kw + ) -> _Command: cmd = _Command.reinitialize_command(self, command, reinit_subcommands) vars(cmd).update(kw) return cmd diff --git a/setuptools/command/test.py b/setuptools/command/test.py index fbdf9fb942..a1e30ee966 100644 --- a/setuptools/command/test.py +++ b/setuptools/command/test.py @@ -1,8 +1,11 @@ +from __future__ import annotations +from collections.abc import Callable import os import operator import sys import contextlib import itertools +from typing import TYPE_CHECKING, Generic, TypeVar, overload import unittest from distutils.errors import DistutilsError, DistutilsOptionError from distutils import log @@ -22,6 +25,12 @@ from more_itertools import unique_everseen from jaraco.functools import pass_none +if TYPE_CHECKING: + from typing_extensions import Self + +_T = TypeVar("_T") +_R = TypeVar("_R") + class ScanningLoader(TestLoader): def __init__(self): @@ -63,11 +72,15 @@ def loadTestsFromModule(self, module, pattern=None): # adapted from jaraco.classes.properties:NonDataProperty -class NonDataProperty: - def __init__(self, fget): +class NonDataProperty(Generic[_T, _R]): + def __init__(self, fget: Callable[[_T], _R]): self.fget = fget - def __get__(self, obj, objtype=None): + @overload + def __get__(self, obj: None, objtype: object = None) -> Self: ... + @overload + def __get__(self, obj: _T, objtype: object = None) -> _R: ... + def __get__(self, obj: _T | None, objtype: object = None) -> Self | _R: if obj is None: return self return self.fget(obj)