From e0eb9ce8049b66cc6c2713e8661840dd2782e9f3 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Thu, 21 May 2026 18:45:17 +0300 Subject: [PATCH] Remove `typing.AnyStr` usage --- setuptools/_distutils/util.py | 8 +++++--- setuptools/command/bdist_egg.py | 8 +++++--- setuptools/glob.py | 12 +++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/setuptools/_distutils/util.py b/setuptools/_distutils/util.py index 47bb5af5e4..a773a534ab 100644 --- a/setuptools/_distutils/util.py +++ b/setuptools/_distutils/util.py @@ -17,7 +17,7 @@ import sysconfig import tempfile from collections.abc import Callable, Iterable, Mapping -from typing import TYPE_CHECKING, AnyStr +from typing import TYPE_CHECKING, TypeVar from jaraco.functools import pass_none @@ -31,6 +31,8 @@ _Ts = TypeVarTuple("_Ts") + _StrBytes = TypeVar("_StrBytes", str, bytes) + def get_host_platform() -> str: """ @@ -140,8 +142,8 @@ def convert_path(pathname: str | os.PathLike[str]) -> str: def change_root( - new_root: AnyStr | os.PathLike[AnyStr], pathname: AnyStr | os.PathLike[AnyStr] -) -> AnyStr: + new_root: _StrBytes | os.PathLike[_StrBytes], pathname: _StrBytes | os.PathLike[_StrBytes] +) -> _StrBytes: """Return 'pathname' with 'new_root' prepended. If 'pathname' is relative, this is equivalent to "os.path.join(new_root,pathname)". Otherwise, it requires making 'pathname' relative and then joining the diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index 02c94faff0..273b502fdd 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py @@ -12,7 +12,7 @@ from collections.abc import Iterator from sysconfig import get_path, get_platform, get_python_version from types import CodeType -from typing import TYPE_CHECKING, AnyStr, Literal +from typing import TYPE_CHECKING, Literal, TypeVar from setuptools import Command from setuptools.extension import Library @@ -27,6 +27,8 @@ from _typeshed import GenericPath + _StrOrBytesT = TypeVar("_StrOrBytesT", bound=str | bytes) + # Same as zipfile._ZipFileMode from typeshed _ZipFileMode: TypeAlias = Literal["r", "w", "x", "a"] @@ -43,8 +45,8 @@ def strip_module(filename): def sorted_walk( - dir: GenericPath[AnyStr], -) -> Iterator[tuple[AnyStr, list[AnyStr], list[AnyStr]]]: + dir: GenericPath[_StrOrBytesT], +) -> Iterator[tuple[_StrOrBytesT, list[_StrOrBytesT], list[_StrOrBytesT]]]: """Do os.walk in a reproducible way, independent of indeterministic filesystem readdir order """ diff --git a/setuptools/glob.py b/setuptools/glob.py index 1dfff2cd50..b1e3fe0633 100644 --- a/setuptools/glob.py +++ b/setuptools/glob.py @@ -12,15 +12,17 @@ import os import re from collections.abc import Iterable, Iterator -from typing import TYPE_CHECKING, AnyStr, overload +from typing import TYPE_CHECKING, TypeVar, overload if TYPE_CHECKING: from _typeshed import BytesPath, StrOrBytesPath, StrPath + _StrOrBytesT = TypeVar("_StrOrBytesT", bound=str | bytes) + __all__ = ["glob", "iglob", "escape"] -def glob(pathname: AnyStr, recursive: bool = False) -> list[AnyStr]: +def glob(pathname: _StrOrBytesT, recursive: bool = False) -> list[_StrOrBytesT]: """Return a list of paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la @@ -34,7 +36,7 @@ def glob(pathname: AnyStr, recursive: bool = False) -> list[AnyStr]: return list(iglob(pathname, recursive=recursive)) -def iglob(pathname: AnyStr, recursive: bool = False) -> Iterator[AnyStr]: +def iglob(pathname: _StrOrBytesT, recursive: bool = False) -> Iterator[_StrOrBytesT]: """Return an iterator which yields the paths matching a pathname pattern. The pattern may contain simple shell-style wildcards a la @@ -52,7 +54,7 @@ def iglob(pathname: AnyStr, recursive: bool = False) -> Iterator[AnyStr]: return it -def _iglob(pathname: AnyStr, recursive: bool) -> Iterator[AnyStr]: +def _iglob(pathname: _StrOrBytesT, recursive: bool) -> Iterator[_StrOrBytesT]: dirname, basename = os.path.split(pathname) glob_in_dir = glob2 if recursive and _isrecursive(basename) else glob1 @@ -73,7 +75,7 @@ def _iglob(pathname: AnyStr, recursive: bool) -> Iterator[AnyStr]: # drive or UNC path. Prevent an infinite recursion if a drive or UNC path # contains magic characters (i.e. r'\\?\C:'). if dirname != pathname and has_magic(dirname): - dirs: Iterable[AnyStr] = _iglob(dirname, recursive) + dirs: Iterable[_StrOrBytesT] = _iglob(dirname, recursive) else: dirs = [dirname] if not has_magic(basename):