Skip to content

Commit

Permalink
Rename _InstallerTypeT & Mark type aliases with TypeAlias to help sta…
Browse files Browse the repository at this point in the history
…tic checkers
  • Loading branch information
Avasam committed Jul 1, 2024
1 parent 3accd5c commit 08c3b82
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 36 deletions.
46 changes: 23 additions & 23 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
Expand All @@ -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]
)
Expand Down Expand Up @@ -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]: ...
Expand All @@ -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]: ...
Expand All @@ -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]:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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],
Expand Down Expand Up @@ -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
Expand All @@ -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`
Expand Down Expand Up @@ -1259,7 +1259,7 @@ def best_match(
def obtain(
self,
requirement: Requirement,
installer: _InstallerTypeT[_DistributionT],
installer: _StrictInstallerType[_DistributionT],
) -> _DistributionT: ...
@overload
def obtain(
Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions setuptools/_path.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
10 changes: 7 additions & 3 deletions setuptools/_reqs.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
7 changes: 5 additions & 2 deletions setuptools/build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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::
Expand Down
3 changes: 2 additions & 1 deletion setuptools/compat/py311.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions setuptools/config/_apply_pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down

0 comments on commit 08c3b82

Please sign in to comment.