From 08c3b821ae48334bb6937850323420ac3936aaf7 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 1 Jul 2024 12:20:48 -0400 Subject: [PATCH 1/2] Rename _InstallerTypeT & Mark type aliases with TypeAlias to help static checkers --- pkg_resources/__init__.py | 46 +++++++++++------------ setuptools/_path.py | 12 ++++-- setuptools/_reqs.py | 10 +++-- setuptools/build_meta.py | 7 +++- setuptools/compat/py311.py | 3 +- setuptools/config/_apply_pyprojecttoml.py | 9 +++-- 6 files changed, 51 insertions(+), 36 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index d47df3f3c5..f9e3ee344b 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -100,7 +100,7 @@ if TYPE_CHECKING: from _typeshed import BytesPath, StrPath, StrOrBytesPath - from typing_extensions import Self + from typing_extensions import Self, TypeAlias warnings.warn( "pkg_resources is deprecated as an API. " @@ -113,20 +113,20 @@ _T = TypeVar("_T") _DistributionT = TypeVar("_DistributionT", bound="Distribution") # Type aliases -_NestedStr = Union[str, Iterable[Union[str, Iterable["_NestedStr"]]]] -_InstallerTypeT = Callable[["Requirement"], "_DistributionT"] -_InstallerType = Callable[["Requirement"], Union["Distribution", None]] -_PkgReqType = Union[str, "Requirement"] -_EPDistType = Union["Distribution", _PkgReqType] -_MetadataType = Union["IResourceProvider", None] -_ResolvedEntryPoint = Any # Can be any attribute in the module -_ResourceStream = Any # TODO / Incomplete: A readable file-like object +_NestedStr: TypeAlias = Union[str, Iterable[Union[str, Iterable["_NestedStr"]]]] +_StrictInstallerType: TypeAlias = Callable[["Requirement"], "_DistributionT"] +_InstallerType: TypeAlias = Callable[["Requirement"], Union["Distribution", None]] +_PkgReqType: TypeAlias = Union[str, "Requirement"] +_EPDistType: TypeAlias = Union["Distribution", _PkgReqType] +_MetadataType: TypeAlias = Union["IResourceProvider", None] +_ResolvedEntryPoint: TypeAlias = Any # Can be any attribute in the module +_ResourceStream: TypeAlias = Any # TODO / Incomplete: A readable file-like object # Any object works, but let's indicate we expect something like a module (optionally has __loader__ or __file__) -_ModuleLike = Union[object, types.ModuleType] +_ModuleLike: TypeAlias = Union[object, types.ModuleType] # Any: Should be _ModuleLike but we end up with issues where _ModuleLike doesn't have _ZipLoaderModule's __loader__ -_ProviderFactoryType = Callable[[Any], "IResourceProvider"] -_DistFinderType = Callable[[_T, str, bool], Iterable["Distribution"]] -_NSHandlerType = Callable[[_T, str, str, types.ModuleType], Union[str, None]] +_ProviderFactoryType: TypeAlias = Callable[[Any], "IResourceProvider"] +_DistFinderType: TypeAlias = Callable[[_T, str, bool], Iterable["Distribution"]] +_NSHandlerType: TypeAlias = Callable[[_T, str, str, types.ModuleType], Union[str, None]] _AdapterT = TypeVar( "_AdapterT", _DistFinderType[Any], _ProviderFactoryType, _NSHandlerType[Any] ) @@ -807,7 +807,7 @@ def resolve( self, requirements: Iterable[Requirement], env: Environment | None, - installer: _InstallerTypeT[_DistributionT], + installer: _StrictInstallerType[_DistributionT], replace_conflicting: bool = False, extras: tuple[str, ...] | None = None, ) -> list[_DistributionT]: ... @@ -817,7 +817,7 @@ def resolve( requirements: Iterable[Requirement], env: Environment | None = None, *, - installer: _InstallerTypeT[_DistributionT], + installer: _StrictInstallerType[_DistributionT], replace_conflicting: bool = False, extras: tuple[str, ...] | None = None, ) -> list[_DistributionT]: ... @@ -834,7 +834,7 @@ def resolve( self, requirements: Iterable[Requirement], env: Environment | None = None, - installer: _InstallerType | None | _InstallerTypeT[_DistributionT] = None, + installer: _InstallerType | None | _StrictInstallerType[_DistributionT] = None, replace_conflicting: bool = False, extras: tuple[str, ...] | None = None, ) -> list[Distribution] | list[_DistributionT]: @@ -940,7 +940,7 @@ def find_plugins( self, plugin_env: Environment, full_env: Environment | None, - installer: _InstallerTypeT[_DistributionT], + installer: _StrictInstallerType[_DistributionT], fallback: bool = True, ) -> tuple[list[_DistributionT], dict[Distribution, Exception]]: ... @overload @@ -949,7 +949,7 @@ def find_plugins( plugin_env: Environment, full_env: Environment | None = None, *, - installer: _InstallerTypeT[_DistributionT], + installer: _StrictInstallerType[_DistributionT], fallback: bool = True, ) -> tuple[list[_DistributionT], dict[Distribution, Exception]]: ... @overload @@ -964,7 +964,7 @@ def find_plugins( self, plugin_env: Environment, full_env: Environment | None = None, - installer: _InstallerType | None | _InstallerTypeT[_DistributionT] = None, + installer: _InstallerType | None | _StrictInstallerType[_DistributionT] = None, fallback: bool = True, ) -> tuple[ list[Distribution] | list[_DistributionT], @@ -1211,7 +1211,7 @@ def best_match( self, req: Requirement, working_set: WorkingSet, - installer: _InstallerTypeT[_DistributionT], + installer: _StrictInstallerType[_DistributionT], replace_conflicting: bool = False, ) -> _DistributionT: ... @overload @@ -1226,7 +1226,7 @@ def best_match( self, req: Requirement, working_set: WorkingSet, - installer: _InstallerType | None | _InstallerTypeT[_DistributionT] = None, + installer: _InstallerType | None | _StrictInstallerType[_DistributionT] = None, replace_conflicting: bool = False, ) -> Distribution | None: """Find distribution best matching `req` and usable on `working_set` @@ -1259,7 +1259,7 @@ def best_match( def obtain( self, requirement: Requirement, - installer: _InstallerTypeT[_DistributionT], + installer: _StrictInstallerType[_DistributionT], ) -> _DistributionT: ... @overload def obtain( @@ -1279,7 +1279,7 @@ def obtain( installer: Callable[[Requirement], None] | _InstallerType | None - | _InstallerTypeT[_DistributionT] = None, + | _StrictInstallerType[_DistributionT] = None, ) -> Distribution | None: """Obtain a distribution matching `requirement` (e.g. via download) diff --git a/setuptools/_path.py b/setuptools/_path.py index fb8ef0e198..054d3b8b3c 100644 --- a/setuptools/_path.py +++ b/setuptools/_path.py @@ -1,11 +1,17 @@ +from __future__ import annotations + import os import sys -from typing import Union +from typing import Union, TYPE_CHECKING + +if TYPE_CHECKING: + from typing_extensions import TypeAlias + if sys.version_info >= (3, 9): - StrPath = Union[str, os.PathLike[str]] # Same as _typeshed.StrPath + StrPath: TypeAlias = Union[str, os.PathLike[str]] # Same as _typeshed.StrPath else: - StrPath = Union[str, os.PathLike] + StrPath: TypeAlias = Union[str, os.PathLike] def ensure_directory(path): diff --git a/setuptools/_reqs.py b/setuptools/_reqs.py index 9f83437033..1f0120de56 100644 --- a/setuptools/_reqs.py +++ b/setuptools/_reqs.py @@ -1,11 +1,15 @@ -from functools import lru_cache -from typing import Callable, Iterable, Iterator, TypeVar, Union, overload +from __future__ import annotations +from functools import lru_cache +from typing import Callable, Iterable, Iterator, TypeVar, Union, overload, TYPE_CHECKING import setuptools.extern.jaraco.text as text from setuptools.extern.packaging.requirements import Requirement +if TYPE_CHECKING: + from typing_extensions import TypeAlias + _T = TypeVar("_T") -_StrOrIter = Union[str, Iterable[str]] +_StrOrIter: TypeAlias = Union[str, Iterable[str]] parse_req: Callable[[str], Requirement] = lru_cache()(Requirement) diff --git a/setuptools/build_meta.py b/setuptools/build_meta.py index c52c872fd0..d8ab28be74 100644 --- a/setuptools/build_meta.py +++ b/setuptools/build_meta.py @@ -38,7 +38,7 @@ import tempfile import warnings from pathlib import Path -from typing import Dict, Iterator, List, Optional, Union, Iterable +from typing import TYPE_CHECKING, Dict, Iterator, List, Union, Iterable import setuptools import distutils @@ -48,6 +48,9 @@ from .warnings import SetuptoolsDeprecationWarning from distutils.util import strtobool +if TYPE_CHECKING: + from typing_extensions import TypeAlias + __all__ = [ 'get_requires_for_build_sdist', @@ -142,7 +145,7 @@ def suppress_known_deprecation(): yield -_ConfigSettings = Optional[Dict[str, Union[str, List[str], None]]] +_ConfigSettings: TypeAlias = Union[Dict[str, Union[str, List[str], None]], None] """ Currently the user can run:: diff --git a/setuptools/compat/py311.py b/setuptools/compat/py311.py index 5069c441c4..cd5abc5407 100644 --- a/setuptools/compat/py311.py +++ b/setuptools/compat/py311.py @@ -6,9 +6,10 @@ if TYPE_CHECKING: from _typeshed import StrOrBytesPath, ExcInfo + from typing_extensions import TypeAlias # Same as shutil._OnExcCallback from typeshed -_OnExcCallback = Callable[[Callable[..., Any], str, BaseException], object] +_OnExcCallback: TypeAlias = Callable[[Callable[..., Any], str, BaseException], object] def shutil_rmtree( diff --git a/setuptools/config/_apply_pyprojecttoml.py b/setuptools/config/_apply_pyprojecttoml.py index f44271c5dd..286c29f98f 100644 --- a/setuptools/config/_apply_pyprojecttoml.py +++ b/setuptools/config/_apply_pyprojecttoml.py @@ -33,11 +33,12 @@ from distutils.dist import _OptionsList from setuptools._importlib import metadata from setuptools.dist import Distribution + from typing_extensions import TypeAlias -EMPTY: Mapping = MappingProxyType({}) # Immutable dict-like -_ProjectReadmeValue = Union[str, Dict[str, str]] -_CorrespFn = Callable[["Distribution", Any, StrPath], None] -_Correspondence = Union[str, _CorrespFn] +EMPTY: MappingProxyType = MappingProxyType({}) # Immutable dict-like +_ProjectReadmeValue: TypeAlias = Union[str, Dict[str, str]] +_CorrespFn: TypeAlias = Callable[["Distribution", Any, StrPath], None] +_Correspondence: TypeAlias = Union[str, _CorrespFn] _logger = logging.getLogger(__name__) From 731cf8b33efff340a859651042b3d5b4faba6679 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 8 Aug 2024 09:45:36 -0400 Subject: [PATCH 2/2] Update setuptools/config/_apply_pyprojecttoml.py --- setuptools/config/_apply_pyprojecttoml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setuptools/config/_apply_pyprojecttoml.py b/setuptools/config/_apply_pyprojecttoml.py index 7187e5f2c3..20f36e85aa 100644 --- a/setuptools/config/_apply_pyprojecttoml.py +++ b/setuptools/config/_apply_pyprojecttoml.py @@ -35,7 +35,7 @@ from setuptools.dist import Distribution from typing_extensions import TypeAlias -EMPTY: MappingProxyType = MappingProxyType({}) # Immutable dict-like +EMPTY: Mapping = MappingProxyType({}) # Immutable dict-like _ProjectReadmeValue: TypeAlias = Union[str, Dict[str, str]] _CorrespFn: TypeAlias = Callable[["Distribution", Any, StrPath], None] _Correspondence: TypeAlias = Union[str, _CorrespFn]