From 2a024ca4406cb5f0a198c7dcce6dd8dfad9c78b1 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 26 Jun 2024 01:49:38 -0400 Subject: [PATCH] Mark type aliases with `TypeAlias` to help static checkers --- pkg_resources/__init__.py | 26 +++++++++++------------ setuptools/_path.py | 12 ++++++++--- setuptools/_reqs.py | 8 ++++--- setuptools/build_meta.py | 7 ++++-- setuptools/compat/py311.py | 3 ++- setuptools/config/_apply_pyprojecttoml.py | 9 ++++---- 6 files changed, 39 insertions(+), 26 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 554e1821e9a..d80a986af4c 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"]]]] -_StrictInstallerType = 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] ) diff --git a/setuptools/_path.py b/setuptools/_path.py index fb8ef0e1988..054d3b8b3c1 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 9f83437033c..d0fb3b876f3 100644 --- a/setuptools/_reqs.py +++ b/setuptools/_reqs.py @@ -1,11 +1,13 @@ from functools import lru_cache -from typing import Callable, Iterable, Iterator, TypeVar, Union, overload - +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 c52c872fd0f..d8ab28be74f 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 5069c441c46..cd5abc54071 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 5a8700051e5..dc214701ce8 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 # noqa from setuptools.dist import Distribution # noqa + 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__)