From e78f4c4a19ed66f88af41fed481869bc045bed6d Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 19 Feb 2024 18:46:26 -0500 Subject: [PATCH 01/18] Reorder names to be closer to implementation --- stubs/setuptools/pkg_resources/__init__.pyi | 121 ++++++++++---------- 1 file changed, 60 insertions(+), 61 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index e26bd8142d6e..d15e1c9a7357 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -27,13 +27,13 @@ def fixup_namespace_packages(path_item: str, parent=None) -> None: ... class WorkingSet: entries: list[str] def __init__(self, entries: Iterable[str] | None = None) -> None: ... - def require(self, *requirements: _NestedStr) -> Sequence[Distribution]: ... - def run_script(self, requires: str, script_name: str) -> None: ... - def iter_entry_points(self, group: str, name: str | None = None) -> Generator[EntryPoint, None, None]: ... def add_entry(self, entry: str) -> None: ... def __contains__(self, dist: Distribution) -> bool: ... - def __iter__(self) -> Iterator[Distribution]: ... def find(self, req: Requirement) -> Distribution | None: ... + def iter_entry_points(self, group: str, name: str | None = None) -> Generator[EntryPoint, None, None]: ... + def run_script(self, requires: str, script_name: str) -> None: ... + def __iter__(self) -> Iterator[Distribution]: ... + def add(self, dist: Distribution, entry: str | None = None, insert: bool = True, replace: bool = False) -> None: ... def resolve( self, requirements: Iterable[Requirement], @@ -42,31 +42,28 @@ class WorkingSet: replace_conflicting: bool = False, extras=None, ) -> list[Distribution]: ... - def add(self, dist: Distribution, entry: str | None = None, insert: bool = True, replace: bool = False) -> None: ... - def subscribe(self, callback: Callable[[Distribution], object], existing: bool = True) -> None: ... def find_plugins( self, plugin_env: Environment, full_env: Environment | None = None, installer=None, fallback: bool = True ) -> tuple[list[Distribution], dict[Distribution, Exception]]: ... + def require(self, *requirements: _NestedStr) -> Sequence[Distribution]: ... + def subscribe(self, callback: Callable[[Distribution], object], existing: bool = True) -> None: ... working_set: WorkingSet - require = working_set.require -run_script = working_set.run_script -run_main = run_script iter_entry_points = working_set.iter_entry_points add_activation_listener = working_set.subscribe +run_script = working_set.run_script +run_main = run_script class Environment: def __init__( self, search_path: Sequence[str] | None = None, platform: str | None = ..., python: str | None = ... ) -> None: ... + def can_add(self, dist: Distribution) -> bool: ... + def remove(self, dist: Distribution) -> None: ... + def scan(self, search_path: Sequence[str] | None = None) -> None: ... def __getitem__(self, project_name: str) -> list[Distribution]: ... - def __iter__(self) -> Iterator[str]: ... def add(self, dist: Distribution) -> None: ... - def remove(self, dist: Distribution) -> None: ... - def can_add(self, dist: Distribution) -> bool: ... - def __add__(self, other: Distribution | Environment) -> Environment: ... - def __iadd__(self, other: Distribution | Environment) -> Self: ... @overload def best_match( self, req: Requirement, working_set: WorkingSet, installer: None = None, replace_conflicting: bool = False @@ -79,7 +76,9 @@ class Environment: def obtain(self, requirement: Requirement, installer: None = None) -> None: ... @overload def obtain(self, requirement: Requirement, installer: Callable[[Requirement], _T]) -> _T: ... - def scan(self, search_path: Sequence[str] | None = None) -> None: ... + def __iter__(self) -> Iterator[str]: ... + def __iadd__(self, other: Distribution | Environment) -> Self: ... + def __add__(self, other: Distribution | Environment) -> Environment: ... def parse_requirements(strs: str | Iterable[str]) -> Generator[Requirement, None, None]: ... @@ -92,17 +91,17 @@ class Requirement(packaging_requirements.Requirement): extras: tuple[str, ...] # type: ignore[assignment] # incompatible override of attribute on base class specs: list[tuple[str, str]] def __init__(self, requirement_string: str) -> None: ... + def __eq__(self, other_requirement: object) -> bool: ... + def __contains__(self, item: Distribution | str | tuple[str, ...]) -> bool: ... @staticmethod def parse(s: str | Iterable[str]) -> Requirement: ... - def __contains__(self, item: Distribution | str | tuple[str, ...]) -> bool: ... - def __eq__(self, other_requirement: object) -> bool: ... def load_entry_point(dist: _EPDistType, group: str, name: str) -> Any: ... -def get_entry_info(dist: _EPDistType, group: str, name: str) -> EntryPoint | None: ... @overload def get_entry_map(dist: _EPDistType, group: None = None) -> dict[str, dict[str, EntryPoint]]: ... @overload def get_entry_map(dist: _EPDistType, group: str) -> dict[str, EntryPoint]: ... +def get_entry_info(dist: _EPDistType, group: str, name: str) -> EntryPoint | None: ... class EntryPoint: pattern: ClassVar[Pattern[str]] @@ -119,6 +118,9 @@ class EntryPoint: extras: tuple[str, ...] = (), dist: Distribution | None = None, ) -> None: ... + def load(self, require: bool = True, env: Environment | None = ..., installer: _InstallerType | None = ...) -> Any: ... + def resolve(self) -> Any: ... + def require(self, env: Environment | None = None, installer: _InstallerType | None = None) -> None: ... @classmethod def parse(cls, src: str, dist: Distribution | None = None) -> EntryPoint: ... @classmethod @@ -127,9 +129,6 @@ class EntryPoint: def parse_map( cls, data: dict[str, str | Sequence[str]] | str | Sequence[str], dist: Distribution | None = None ) -> dict[str, EntryPoint]: ... - def load(self, require: bool = True, env: Environment | None = ..., installer: _InstallerType | None = ...) -> Any: ... - def require(self, env: Environment | None = None, installer: _InstallerType | None = None) -> None: ... - def resolve(self) -> Any: ... def find_distributions(path_item: str, only: bool = False) -> Generator[Distribution, None, None]: ... @overload @@ -143,28 +142,28 @@ SOURCE_DIST: int CHECKOUT_DIST: int DEVELOP_DIST: int -def resource_exists(package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... -def resource_stream(package_or_requirement: _PkgReqType, resource_name: str) -> IO[bytes]: ... -def resource_string(package_or_requirement: _PkgReqType, resource_name: str) -> bytes: ... -def resource_isdir(package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... -def resource_listdir(package_or_requirement: _PkgReqType, resource_name: str) -> list[str]: ... -def resource_filename(package_or_requirement: _PkgReqType, resource_name: str) -> str: ... -def set_extraction_path(path: str) -> None: ... -def cleanup_resources(force: bool = False) -> list[str]: ... @type_check_only class _IResourceManager(Protocol): def resource_exists(self, package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... + def resource_isdir(self, package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... + def resource_filename(self, package_or_requirement: _PkgReqType, resource_name: str) -> str: ... def resource_stream(self, package_or_requirement: _PkgReqType, resource_name: str) -> IO[bytes]: ... def resource_string(self, package_or_requirement: _PkgReqType, resource_name: str) -> bytes: ... - def resource_isdir(self, package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... def resource_listdir(self, package_or_requirement: _PkgReqType, resource_name: str) -> list[str]: ... - def resource_filename(self, package_or_requirement: _PkgReqType, resource_name: str) -> str: ... - def set_extraction_path(self, path: str) -> None: ... - def cleanup_resources(self, force: bool = ...) -> list[str]: ... - def get_cache_path(self, archive_name: str, names: Iterable[str] = ...) -> str: ... def extraction_error(self) -> None: ... + def get_cache_path(self, archive_name: str, names: Iterable[str] = ...) -> str: ... def postprocess(self, tempname: str, filename: str) -> None: ... + def set_extraction_path(self, path: str) -> None: ... + def cleanup_resources(self, force: bool = ...) -> list[str]: ... +def resource_exists(package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... +def resource_isdir(package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... +def resource_filename(package_or_requirement: _PkgReqType, resource_name: str) -> str: ... +def resource_stream(package_or_requirement: _PkgReqType, resource_name: str) -> IO[bytes]: ... +def resource_string(package_or_requirement: _PkgReqType, resource_name: str) -> bytes: ... +def resource_listdir(package_or_requirement: _PkgReqType, resource_name: str) -> list[str]: ... +def set_extraction_path(path: str) -> None: ... +def cleanup_resources(force: bool = False) -> list[str]: ... @overload def get_provider(moduleOrReq: str) -> IResourceProvider: ... @overload @@ -248,27 +247,10 @@ class NullProvider: # Doesn't actually extend NullProvider class Distribution(NullProvider): PKG_INFO: ClassVar[str] - location: str project_name: str - @property - def hashcmp(self) -> tuple[Incomplete, int, str, Incomplete | None, str, str]: ... - def __hash__(self) -> int: ... - def __lt__(self, other: Distribution) -> bool: ... - def __le__(self, other: Distribution) -> bool: ... - def __gt__(self, other: Distribution) -> bool: ... - def __ge__(self, other: Distribution) -> bool: ... - def __eq__(self, other: object) -> bool: ... - def __ne__(self, other: object) -> bool: ... - @property - def key(self) -> str: ... - @property - def extras(self) -> list[str]: ... - @property - def version(self) -> str: ... - @property - def parsed_version(self) -> packaging_version.Version: ... py_version: str platform: str | None + location: str precedence: int def __init__( self, @@ -284,22 +266,39 @@ class Distribution(NullProvider): def from_location( cls, location: str, basename: str, metadata: _MetadataType = None, **kw: str | None | int ) -> Distribution: ... + @property + def hashcmp(self) -> tuple[Incomplete, int, str, Incomplete | None, str, str]: ... + def __hash__(self) -> int: ... + def __lt__(self, other: Distribution) -> bool: ... + def __le__(self, other: Distribution) -> bool: ... + def __gt__(self, other: Distribution) -> bool: ... + def __ge__(self, other: Distribution) -> bool: ... + def __eq__(self, other: object) -> bool: ... + def __ne__(self, other: object) -> bool: ... + @property + def key(self) -> str: ... + @property + def parsed_version(self) -> packaging_version.Version: ... + @property + def version(self) -> str: ... + def requires(self, extras: tuple[str, ...] = ()) -> list[Requirement]: ... + def activate(self, path: list[str] | None = None, replace: bool = False) -> None: ... + def egg_name(self) -> str: ... # type: ignore[override] # supertype's egg_name is a variable, not a method @classmethod def from_filename(cls, filename: str, metadata: _MetadataType = None, **kw: str | None | int) -> Distribution: ... - def activate(self, path: list[str] | None = None, replace: bool = False) -> None: ... def as_requirement(self) -> Requirement: ... - def requires(self, extras: tuple[str, ...] = ()) -> list[Requirement]: ... - def check_version_conflict(self) -> None: ... - def has_version(self) -> bool: ... - def clone(self, **kw: str | int | None) -> Requirement: ... - def egg_name(self) -> str: ... # type: ignore[override] # supertype's egg_name is a variable, not a method - def get_entry_info(self, group: str, name: str) -> EntryPoint | None: ... - def insert_on(self, path, loc: Incomplete | None = None, replace: bool = False) -> None: ... + def load_entry_point(self, group: str, name: str) -> Any: ... @overload def get_entry_map(self, group: None = None) -> dict[str, dict[str, EntryPoint]]: ... @overload def get_entry_map(self, group: str) -> dict[str, EntryPoint]: ... - def load_entry_point(self, group: str, name: str) -> Any: ... + def get_entry_info(self, group: str, name: str) -> EntryPoint | None: ... + def insert_on(self, path, loc: Incomplete | None = None, replace: bool = False) -> None: ... + def check_version_conflict(self) -> None: ... + def has_version(self) -> bool: ... + def clone(self, **kw: str | int | None) -> Requirement: ... + @property + def extras(self) -> list[str]: ... class DistInfoDistribution(Distribution): PKG_INFO: ClassVar[Literal["METADATA"]] From 7df4da38cbe36e6274b5d9a9406d661771283cb6 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 19 Feb 2024 19:39:53 -0500 Subject: [PATCH 02/18] Complete and strict typing for pkg_resources --- pyrightconfig.stricter.json | 2 +- .../setuptools/@tests/stubtest_allowlist.txt | 6 +- stubs/setuptools/pkg_resources/__init__.pyi | 145 +++++++++++------- .../pkg_resources/extern/__init__.pyi | 20 +++ .../setuptools/setuptools/extern/__init__.pyi | 27 ++-- 5 files changed, 124 insertions(+), 76 deletions(-) create mode 100644 stubs/setuptools/pkg_resources/extern/__init__.pyi diff --git a/pyrightconfig.stricter.json b/pyrightconfig.stricter.json index 8579b6dd516b..847a586c60a0 100644 --- a/pyrightconfig.stricter.json +++ b/pyrightconfig.stricter.json @@ -74,7 +74,7 @@ "stubs/redis", "stubs/requests", "stubs/requests-oauthlib", - "stubs/setuptools", + "stubs/setuptools/setuptools", "stubs/stripe", "stubs/tqdm", "stubs/ttkthemes", diff --git a/stubs/setuptools/@tests/stubtest_allowlist.txt b/stubs/setuptools/@tests/stubtest_allowlist.txt index 9edb8ef70a0b..8203636ed8fc 100644 --- a/stubs/setuptools/@tests/stubtest_allowlist.txt +++ b/stubs/setuptools/@tests/stubtest_allowlist.txt @@ -1,6 +1,7 @@ # Is always set in __init__ pkg_resources.PathMetadata.egg_info pkg_resources.EggMetadata.loader +pkg_resources.ZipProvider.loader # 1 used for True as a default value setuptools._distutils.dist.Distribution.get_command_obj @@ -44,8 +45,3 @@ setuptools._distutils.* # Other vendored code setuptools._vendor.* pkg_resources._vendor.* -# Deprecated in favor of importlib.resources, importlib.metadata and their backports -# So like distutils, we only add what we need to reference. -pkg_resources.AvailableDistributions -pkg_resources.ResourceManager -pkg_resources.extern diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index d15e1c9a7357..7e06559214ce 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -1,13 +1,13 @@ -import importlib.abc import types -import zipimport -from _typeshed import Incomplete -from abc import ABCMeta +from _typeshed import Incomplete, StrPath, Unused from collections.abc import Callable, Generator, Iterable, Iterator, Sequence -from io import BytesIO +from io import BufferedIOBase, BytesIO +from pkgutil import get_importer as get_importer from re import Pattern -from typing import IO, Any, ClassVar, Literal, Protocol, TypeVar, overload, type_check_only +from typing import IO, Any, BinaryIO, ClassVar, Final, Literal, NamedTuple, Protocol, TypeVar, overload, type_check_only from typing_extensions import Self, TypeAlias +from zipfile import ZipInfo +from zipimport import zipimporter from ._vendored_packaging import requirements as packaging_requirements, version as packaging_version @@ -18,11 +18,16 @@ _InstallerType: TypeAlias = Callable[[Requirement], Distribution | None] _EPDistType: TypeAlias = Distribution | Requirement | str _MetadataType: TypeAlias = IResourceProvider | None _PkgReqType: TypeAlias = str | Requirement -_DistFinderType: TypeAlias = Callable[[_Importer, str, bool], Generator[Distribution, None, None]] -_NSHandlerType: TypeAlias = Callable[[_Importer, str, str, types.ModuleType], str] +_DistFinderType: TypeAlias = Callable[[types._LoaderProtocol, str, bool], Iterator[Distribution]] +_NSHandlerType: TypeAlias = Callable[[types._LoaderProtocol, str, str, types.ModuleType], str | None] +_ModuleLike: TypeAlias = object # Optionally has __loader__ or __file__ +_ZipManifestDict: TypeAlias = dict[str, ZipInfo] + +@type_check_only +class _ResourceStream(BufferedIOBase, BinaryIO, Protocol): ... # type: ignore[misc] # pyright: ignore def declare_namespace(packageName: str) -> None: ... -def fixup_namespace_packages(path_item: str, parent=None) -> None: ... +def fixup_namespace_packages(path_item: str, parent: str | None = None) -> None: ... class WorkingSet: entries: list[str] @@ -40,10 +45,14 @@ class WorkingSet: env: Environment | None = None, installer: _InstallerType | None = None, replace_conflicting: bool = False, - extras=None, + extras: tuple[str, ...] | None = None, ) -> list[Distribution]: ... def find_plugins( - self, plugin_env: Environment, full_env: Environment | None = None, installer=None, fallback: bool = True + self, + plugin_env: Environment, + full_env: Environment | None = None, + installer: _InstallerType | None = None, + fallback: bool = True, ) -> tuple[list[Distribution], dict[Distribution, Exception]]: ... def require(self, *requirements: _NestedStr) -> Sequence[Distribution]: ... def subscribe(self, callback: Callable[[Distribution], object], existing: bool = True) -> None: ... @@ -91,7 +100,7 @@ class Requirement(packaging_requirements.Requirement): extras: tuple[str, ...] # type: ignore[assignment] # incompatible override of attribute on base class specs: list[tuple[str, str]] def __init__(self, requirement_string: str) -> None: ... - def __eq__(self, other_requirement: object) -> bool: ... + def __eq__(self, other: object) -> bool: ... def __contains__(self, item: Distribution | str | tuple[str, ...]) -> bool: ... @staticmethod def parse(s: str | Iterable[str]) -> Requirement: ... @@ -118,7 +127,10 @@ class EntryPoint: extras: tuple[str, ...] = (), dist: Distribution | None = None, ) -> None: ... - def load(self, require: bool = True, env: Environment | None = ..., installer: _InstallerType | None = ...) -> Any: ... + @overload + def load(self, require: Literal[False], env: Environment | None = None, installer: _InstallerType | None = None) -> Any: ... + @overload + def load(self, require: Literal[True] = True, *args: Unused, **kwargs: Unused) -> Any: ... def resolve(self) -> Any: ... def require(self, env: Environment | None = None, installer: _InstallerType | None = None) -> None: ... @classmethod @@ -136,14 +148,15 @@ def get_distribution(dist: _D) -> _D: ... @overload def get_distribution(dist: _PkgReqType) -> Distribution: ... -EGG_DIST: int -BINARY_DIST: int -SOURCE_DIST: int -CHECKOUT_DIST: int -DEVELOP_DIST: int +PY_MAJOR: Final[str] +EGG_DIST: Final = 3 +BINARY_DIST: Final = 2 +SOURCE_DIST: Final = 1 +CHECKOUT_DIST: Final = 0 +DEVELOP_DIST: Final = -1 -@type_check_only -class _IResourceManager(Protocol): +class ResourceManager: + extraction_path: str | None def resource_exists(self, package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... def resource_isdir(self, package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... def resource_filename(self, package_or_requirement: _PkgReqType, resource_name: str) -> str: ... @@ -151,19 +164,21 @@ class _IResourceManager(Protocol): def resource_string(self, package_or_requirement: _PkgReqType, resource_name: str) -> bytes: ... def resource_listdir(self, package_or_requirement: _PkgReqType, resource_name: str) -> list[str]: ... def extraction_error(self) -> None: ... - def get_cache_path(self, archive_name: str, names: Iterable[str] = ...) -> str: ... + def get_cache_path(self, archive_name: str, names: Iterable[str] = ()) -> str: ... def postprocess(self, tempname: str, filename: str) -> None: ... def set_extraction_path(self, path: str) -> None: ... - def cleanup_resources(self, force: bool = ...) -> list[str]: ... - -def resource_exists(package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... -def resource_isdir(package_or_requirement: _PkgReqType, resource_name: str) -> bool: ... -def resource_filename(package_or_requirement: _PkgReqType, resource_name: str) -> str: ... -def resource_stream(package_or_requirement: _PkgReqType, resource_name: str) -> IO[bytes]: ... -def resource_string(package_or_requirement: _PkgReqType, resource_name: str) -> bytes: ... -def resource_listdir(package_or_requirement: _PkgReqType, resource_name: str) -> list[str]: ... -def set_extraction_path(path: str) -> None: ... -def cleanup_resources(force: bool = False) -> list[str]: ... + def cleanup_resources(self, force: bool = False) -> list[str]: ... + +__resource_manager: ResourceManager +resource_exists = __resource_manager.resource_exists +resource_isdir = __resource_manager.resource_isdir +resource_filename = __resource_manager.resource_filename +resource_stream = __resource_manager.resource_stream +resource_string = __resource_manager.resource_string +resource_listdir = __resource_manager.resource_listdir +set_extraction_path = __resource_manager.set_extraction_path +cleanup_resources = __resource_manager.cleanup_resources + @overload def get_provider(moduleOrReq: str) -> IResourceProvider: ... @overload @@ -202,27 +217,27 @@ class ContextualVersionConflict(VersionConflict): class UnknownExtra(ResolutionError): ... +AvailableDistributions = Environment + class ExtractionError(Exception): - manager: _IResourceManager + manager: ResourceManager cache_path: str original_error: Exception -class _Importer(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader, metaclass=ABCMeta): ... - def register_finder(importer_type: type, distribution_finder: _DistFinderType) -> None: ... def register_loader_type(loader_type: type, provider_factory: Callable[[types.ModuleType], IResourceProvider]) -> None: ... def register_namespace_handler(importer_type: type, namespace_handler: _NSHandlerType) -> None: ... class IResourceProvider(IMetadataProvider, Protocol): - def get_resource_filename(self, manager: _IResourceManager, resource_name): ... - def get_resource_stream(self, manager: _IResourceManager, resource_name): ... - def get_resource_string(self, manager: _IResourceManager, resource_name): ... - def has_resource(self, resource_name): ... - def resource_isdir(self, resource_name): ... - def resource_listdir(self, resource_name): ... + def get_resource_filename(self, manager: ResourceManager, resource_name: str) -> StrPath: ... + def get_resource_stream(self, manager: ResourceManager, resource_name: str) -> _ResourceStream: ... + def get_resource_string(self, manager: ResourceManager, resource_name: str) -> bytes: ... + def has_resource(self, resource_name: str) -> bool: ... + def resource_isdir(self, resource_name: str) -> bool: ... + def resource_listdir(self, resource_name: str) -> list[str]: ... -def invalid_marker(text) -> SyntaxError | Literal[False]: ... -def evaluate_marker(text, extra: Incomplete | None = None): ... +def invalid_marker(text: str) -> SyntaxError | Literal[False]: ... +def evaluate_marker(text: str, extra: str | None = None) -> bool: ... class NullProvider: egg_name: str | None @@ -230,21 +245,21 @@ class NullProvider: loader: types._LoaderProtocol | None module_path: str | None - def __init__(self, module) -> None: ... - def get_resource_filename(self, manager: _IResourceManager, resource_name) -> str: ... - def get_resource_stream(self, manager: _IResourceManager, resource_name) -> BytesIO: ... - def get_resource_string(self, manager: _IResourceManager, resource_name): ... - def has_resource(self, resource_name) -> bool: ... + def __init__(self, module: _ModuleLike) -> None: ... + def get_resource_filename(self, manager: ResourceManager, resource_name: str) -> str: ... + def get_resource_stream(self, manager: ResourceManager, resource_name: str) -> BytesIO: ... + def get_resource_string(self, manager: ResourceManager, resource_name: str) -> bytes: ... + def has_resource(self, resource_name: str) -> bool: ... def has_metadata(self, name: str) -> bool | None: ... def get_metadata(self, name: str) -> str: ... def get_metadata_lines(self, name: str) -> Generator[str, None, None]: ... - def resource_isdir(self, resource_name) -> bool: ... + def resource_isdir(self, resource_name: str) -> bool: ... def metadata_isdir(self, name: str) -> bool: ... - def resource_listdir(self, resource_name) -> list[str]: ... + def resource_listdir(self, resource_name: str) -> list[str]: ... def metadata_listdir(self, name: str) -> list[str]: ... def run_script(self, script_name: str, namespace: dict[str, Any]) -> None: ... -# Doesn't actually extend NullProvider +# Doesn't actually extend NullProvider, solves a typing issue in pytype_test.py class Distribution(NullProvider): PKG_INFO: ClassVar[str] project_name: str @@ -264,7 +279,7 @@ class Distribution(NullProvider): ) -> None: ... @classmethod def from_location( - cls, location: str, basename: str, metadata: _MetadataType = None, **kw: str | None | int + cls, location: str, basename: str, metadata: _MetadataType = None, **kw: str | int | None ) -> Distribution: ... @property def hashcmp(self) -> tuple[Incomplete, int, str, Incomplete | None, str, str]: ... @@ -285,7 +300,7 @@ class Distribution(NullProvider): def activate(self, path: list[str] | None = None, replace: bool = False) -> None: ... def egg_name(self) -> str: ... # type: ignore[override] # supertype's egg_name is a variable, not a method @classmethod - def from_filename(cls, filename: str, metadata: _MetadataType = None, **kw: str | None | int) -> Distribution: ... + def from_filename(cls, filename: str, metadata: _MetadataType = None, **kw: str | int | None) -> Distribution: ... def as_requirement(self) -> Requirement: ... def load_entry_point(self, group: str, name: str) -> Any: ... @overload @@ -293,7 +308,7 @@ class Distribution(NullProvider): @overload def get_entry_map(self, group: str) -> dict[str, EntryPoint]: ... def get_entry_info(self, group: str, name: str) -> EntryPoint | None: ... - def insert_on(self, path, loc: Incomplete | None = None, replace: bool = False) -> None: ... + def insert_on(self, path: list[StrPath], loc: Incomplete | None = None, replace: bool = False) -> None: ... def check_version_conflict(self) -> None: ... def has_version(self) -> bool: ... def clone(self, **kw: str | int | None) -> Requirement: ... @@ -314,16 +329,30 @@ class PathMetadata(DefaultProvider): module_path: str def __init__(self, path: str, egg_info: str) -> None: ... +class ZipManifests(dict[str, MemoizedZipManifests.manifest_mod]): + @classmethod + def build(cls, path: StrPath | IO[bytes]) -> _ZipManifestDict: ... + # Can't do `load = build` because of https://github.com/python/mypy/issues/6700 + load = ZipManifests.build # noqa: F821 + +class MemoizedZipManifests(ZipManifests): + class manifest_mod(NamedTuple): + manifest: _ZipManifestDict + mtime: float + def load(self, path: str) -> _ZipManifestDict: ... # type: ignore[override] + class ZipProvider(EggProvider): eagers: list[str] | None zip_pre: str + # ZipProvider's loader should always be a zipimporter or equivalent + loader: zipimporter @property - def zipinfo(self): ... + def zipinfo(self) -> _ZipManifestDict: ... class EggMetadata(ZipProvider): - loader: zipimport.zipimporter + loader: zipimporter module_path: str - def __init__(self, importer: zipimport.zipimporter) -> None: ... + def __init__(self, importer: zipimporter) -> None: ... class EmptyProvider(NullProvider): module_path: None @@ -332,7 +361,8 @@ class EmptyProvider(NullProvider): empty_provider: EmptyProvider class FileMetadata(EmptyProvider): - def __init__(self, path: str) -> None: ... + path: StrPath + def __init__(self, path: StrPath) -> None: ... class PEP440Warning(RuntimeWarning): ... @@ -349,7 +379,6 @@ def get_platform() -> str: ... def get_supported_platform() -> str: ... def compatible_platforms(provided: str | None, required: str | None) -> bool: ... def get_default_cache() -> str: ... -def get_importer(path_item: str) -> _Importer: ... def ensure_directory(path: str) -> None: ... def normalize_path(filename: str) -> str: ... diff --git a/stubs/setuptools/pkg_resources/extern/__init__.pyi b/stubs/setuptools/pkg_resources/extern/__init__.pyi new file mode 100644 index 000000000000..1a2b2d849ebe --- /dev/null +++ b/stubs/setuptools/pkg_resources/extern/__init__.pyi @@ -0,0 +1,20 @@ +from collections.abc import Generator, Iterable, Sequence +from importlib.machinery import ModuleSpec +from types import ModuleType + +class VendorImporter: + root_name: str + vendored_names: set[str] + vendor_pkg: str + def __init__(self, root_name: str, vendored_names: Iterable[str] = (), vendor_pkg: str | None = None) -> None: ... + @property + def search_path(self) -> Generator[str, None, None]: ... + def load_module(self, fullname: str) -> ModuleType: ... + def create_module(self, spec: ModuleSpec) -> ModuleType: ... + def exec_module(self, module: ModuleType) -> None: ... + def find_spec( + self, fullname: str, path: Sequence[str] | None = None, target: ModuleType | None = None + ) -> ModuleSpec | None: ... + def install(self) -> None: ... + +names: tuple[str, ...] diff --git a/stubs/setuptools/setuptools/extern/__init__.pyi b/stubs/setuptools/setuptools/extern/__init__.pyi index 080cdf737a86..1a2b2d849ebe 100644 --- a/stubs/setuptools/setuptools/extern/__init__.pyi +++ b/stubs/setuptools/setuptools/extern/__init__.pyi @@ -1,17 +1,20 @@ -from _typeshed import Incomplete -from typing import Any +from collections.abc import Generator, Iterable, Sequence +from importlib.machinery import ModuleSpec +from types import ModuleType class VendorImporter: - root_name: Any - vendored_names: Any - vendor_pkg: Any - def __init__(self, root_name, vendored_names=(), vendor_pkg: Incomplete | None = None) -> None: ... + root_name: str + vendored_names: set[str] + vendor_pkg: str + def __init__(self, root_name: str, vendored_names: Iterable[str] = (), vendor_pkg: str | None = None) -> None: ... @property - def search_path(self) -> None: ... - def load_module(self, fullname): ... - def create_module(self, spec): ... - def exec_module(self, module) -> None: ... - def find_spec(self, fullname, path: Incomplete | None = None, target: Incomplete | None = None): ... + def search_path(self) -> Generator[str, None, None]: ... + def load_module(self, fullname: str) -> ModuleType: ... + def create_module(self, spec: ModuleSpec) -> ModuleType: ... + def exec_module(self, module: ModuleType) -> None: ... + def find_spec( + self, fullname: str, path: Sequence[str] | None = None, target: ModuleType | None = None + ) -> ModuleSpec | None: ... def install(self) -> None: ... -names: Any +names: tuple[str, ...] From e35218b60fe8a1be88baa36fbb3c4c58e0c82b3f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Feb 2024 01:00:22 +0000 Subject: [PATCH 03/18] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/setuptools/pkg_resources/__init__.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 7e06559214ce..8ef18b839b6b 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -339,6 +339,7 @@ class MemoizedZipManifests(ZipManifests): class manifest_mod(NamedTuple): manifest: _ZipManifestDict mtime: float + def load(self, path: str) -> _ZipManifestDict: ... # type: ignore[override] class ZipProvider(EggProvider): From 24a8983e9779c8bb91d9984c37d30205c0776017 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 20 Feb 2024 02:51:05 -0500 Subject: [PATCH 04/18] More fixes from merging return types --- stubs/setuptools/pkg_resources/__init__.pyi | 102 +++++++++++--------- 1 file changed, 58 insertions(+), 44 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 7e06559214ce..59ca4ce96e95 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -1,30 +1,34 @@ import types from _typeshed import Incomplete, StrPath, Unused -from collections.abc import Callable, Generator, Iterable, Iterator, Sequence +from collections.abc import Callable, Generator, Iterable, Iterator, Mapping, Sequence from io import BufferedIOBase, BytesIO +from itertools import chain +from os import PathLike from pkgutil import get_importer as get_importer from re import Pattern -from typing import IO, Any, BinaryIO, ClassVar, Final, Literal, NamedTuple, Protocol, TypeVar, overload, type_check_only +from typing import IO, Any, BinaryIO, ClassVar, Final, Literal, NamedTuple, NoReturn, Protocol, TypeVar, overload, type_check_only from typing_extensions import Self, TypeAlias from zipfile import ZipInfo from zipimport import zipimporter from ._vendored_packaging import requirements as packaging_requirements, version as packaging_version +@type_check_only +class _ResourceStream(BufferedIOBase, BinaryIO, Protocol): ... # type: ignore[misc] # pyright: ignore + _T = TypeVar("_T") _D = TypeVar("_D", bound=Distribution) -_NestedStr: TypeAlias = str | Iterable[str | Iterable[Any]] +_NestedStr: TypeAlias = str | Iterable[_NestedStr] _InstallerType: TypeAlias = Callable[[Requirement], Distribution | None] -_EPDistType: TypeAlias = Distribution | Requirement | str -_MetadataType: TypeAlias = IResourceProvider | None _PkgReqType: TypeAlias = str | Requirement -_DistFinderType: TypeAlias = Callable[[types._LoaderProtocol, str, bool], Iterator[Distribution]] -_NSHandlerType: TypeAlias = Callable[[types._LoaderProtocol, str, str, types.ModuleType], str | None] +_EPDistType: TypeAlias = Distribution | _PkgReqType +_MetadataType: TypeAlias = IResourceProvider | None _ModuleLike: TypeAlias = object # Optionally has __loader__ or __file__ +_ProviderFactoryType: TypeAlias = Callable[[_ModuleLike], IResourceProvider] +_DistFinderType: TypeAlias = Callable[[_T, str, bool], Iterator[Distribution]] +_NSHandlerType: TypeAlias = Callable[[_T, str, str, types.ModuleType], str | None] _ZipManifestDict: TypeAlias = dict[str, ZipInfo] - -@type_check_only -class _ResourceStream(BufferedIOBase, BinaryIO, Protocol): ... # type: ignore[misc] # pyright: ignore +_AdapterT = TypeVar("_AdapterT", _DistFinderType[Any], _ProviderFactoryType, _NSHandlerType[Any]) def declare_namespace(packageName: str) -> None: ... def fixup_namespace_packages(path_item: str, parent: str | None = None) -> None: ... @@ -87,9 +91,9 @@ class Environment: def obtain(self, requirement: Requirement, installer: Callable[[Requirement], _T]) -> _T: ... def __iter__(self) -> Iterator[str]: ... def __iadd__(self, other: Distribution | Environment) -> Self: ... - def __add__(self, other: Distribution | Environment) -> Environment: ... + def __add__(self, other: Distribution | Environment) -> Self: ... -def parse_requirements(strs: str | Iterable[str]) -> Generator[Requirement, None, None]: ... +def parse_requirements(strs: str | Iterable[str]) -> map[Requirement]: ... class RequirementParseError(packaging_requirements.InvalidRequirement): ... @@ -105,7 +109,7 @@ class Requirement(packaging_requirements.Requirement): @staticmethod def parse(s: str | Iterable[str]) -> Requirement: ... -def load_entry_point(dist: _EPDistType, group: str, name: str) -> Any: ... +def load_entry_point(dist: _EPDistType, group: str, name: str) -> types.ModuleType: ... @overload def get_entry_map(dist: _EPDistType, group: None = None) -> dict[str, dict[str, EntryPoint]]: ... @overload @@ -113,7 +117,6 @@ def get_entry_map(dist: _EPDistType, group: str) -> dict[str, EntryPoint]: ... def get_entry_info(dist: _EPDistType, group: str, name: str) -> EntryPoint | None: ... class EntryPoint: - pattern: ClassVar[Pattern[str]] name: str module_name: str attrs: tuple[str, ...] @@ -128,19 +131,22 @@ class EntryPoint: dist: Distribution | None = None, ) -> None: ... @overload - def load(self, require: Literal[False], env: Environment | None = None, installer: _InstallerType | None = None) -> Any: ... + def load( + self, require: Literal[True] = True, env: Environment | None = None, installer: _InstallerType | None = None + ) -> types.ModuleType: ... @overload - def load(self, require: Literal[True] = True, *args: Unused, **kwargs: Unused) -> Any: ... - def resolve(self) -> Any: ... + def load(self, require: Literal[False], *args: Unused, **kwargs: Unused) -> types.ModuleType: ... + def resolve(self) -> types.ModuleType: ... def require(self, env: Environment | None = None, installer: _InstallerType | None = None) -> None: ... + pattern: ClassVar[Pattern[str]] @classmethod - def parse(cls, src: str, dist: Distribution | None = None) -> EntryPoint: ... + def parse(cls, src: str, dist: Distribution | None = None) -> Self: ... @classmethod - def parse_group(cls, group: str, lines: str | Sequence[str], dist: Distribution | None = None) -> dict[str, EntryPoint]: ... + def parse_group(cls, group: str, lines: _NestedStr, dist: Distribution | None = None) -> dict[str, Self]: ... @classmethod def parse_map( cls, data: dict[str, str | Sequence[str]] | str | Sequence[str], dist: Distribution | None = None - ) -> dict[str, EntryPoint]: ... + ) -> dict[str, dict[str, Self]]: ... def find_distributions(path_item: str, only: bool = False) -> Generator[Distribution, None, None]: ... @overload @@ -163,7 +169,7 @@ class ResourceManager: def resource_stream(self, package_or_requirement: _PkgReqType, resource_name: str) -> IO[bytes]: ... def resource_string(self, package_or_requirement: _PkgReqType, resource_name: str) -> bytes: ... def resource_listdir(self, package_or_requirement: _PkgReqType, resource_name: str) -> list[str]: ... - def extraction_error(self) -> None: ... + def extraction_error(self) -> NoReturn: ... def get_cache_path(self, archive_name: str, names: Iterable[str] = ()) -> str: ... def postprocess(self, tempname: str, filename: str) -> None: ... def set_extraction_path(self, path: str) -> None: ... @@ -185,11 +191,11 @@ def get_provider(moduleOrReq: str) -> IResourceProvider: ... def get_provider(moduleOrReq: Requirement) -> Distribution: ... class IMetadataProvider(Protocol): - def has_metadata(self, name: str) -> bool | None: ... + def has_metadata(self, name: str) -> bool: ... + def get_metadata(self, name: str) -> str: ... + def get_metadata_lines(self, name: str) -> Iterator[str]: ... def metadata_isdir(self, name: str) -> bool: ... def metadata_listdir(self, name: str) -> list[str]: ... - def get_metadata(self, name: str) -> str: ... - def get_metadata_lines(self, name: str) -> Generator[str, None, None]: ... def run_script(self, script_name: str, namespace: dict[str, Any]) -> None: ... class ResolutionError(Exception): ... @@ -205,11 +211,11 @@ class DistributionNotFound(ResolutionError): class VersionConflict(ResolutionError): @property - def dist(self) -> Any: ... + def dist(self) -> Distribution: ... @property - def req(self) -> Any: ... + def req(self) -> Requirement: ... def report(self) -> str: ... - def with_context(self, required_by: set[Distribution | str]) -> VersionConflict: ... + def with_context(self, required_by: set[Distribution | str]) -> Self | ContextualVersionConflict: ... class ContextualVersionConflict(VersionConflict): @property @@ -222,11 +228,11 @@ AvailableDistributions = Environment class ExtractionError(Exception): manager: ResourceManager cache_path: str - original_error: Exception + original_error: BaseException | None -def register_finder(importer_type: type, distribution_finder: _DistFinderType) -> None: ... -def register_loader_type(loader_type: type, provider_factory: Callable[[types.ModuleType], IResourceProvider]) -> None: ... -def register_namespace_handler(importer_type: type, namespace_handler: _NSHandlerType) -> None: ... +def register_finder(importer_type: type[_T], distribution_finder: _DistFinderType[_T]) -> None: ... +def register_loader_type(loader_type: type[_ModuleLike], provider_factory: _ProviderFactoryType) -> None: ... +def register_namespace_handler(importer_type: type[_T], namespace_handler: _NSHandlerType[_T]) -> None: ... class IResourceProvider(IMetadataProvider, Protocol): def get_resource_filename(self, manager: ResourceManager, resource_name: str) -> StrPath: ... @@ -250,10 +256,16 @@ class NullProvider: def get_resource_stream(self, manager: ResourceManager, resource_name: str) -> BytesIO: ... def get_resource_string(self, manager: ResourceManager, resource_name: str) -> bytes: ... def has_resource(self, resource_name: str) -> bool: ... - def has_metadata(self, name: str) -> bool | None: ... + # Note: runtime forgets to coerce the return type to boolean, + # but we need to specify bool for NullProvider to respect the IResourceProvider protocol + # TODO: raise and fix in setuptools' repo + def has_metadata(self, name: str) -> bool: ... def get_metadata(self, name: str) -> str: ... - def get_metadata_lines(self, name: str) -> Generator[str, None, None]: ... + def get_metadata_lines(self, name: str) -> chain[str]: ... def resource_isdir(self, resource_name: str) -> bool: ... + # Note: runtime forgets to coerce the return type to boolean, + # but we need to specify bool for NullProvider to respect the IResourceProvider protocol + # TODO: raise and fix in setuptools' repo def metadata_isdir(self, name: str) -> bool: ... def resource_listdir(self, resource_name: str) -> list[str]: ... def metadata_listdir(self, name: str) -> list[str]: ... @@ -263,9 +275,9 @@ class NullProvider: class Distribution(NullProvider): PKG_INFO: ClassVar[str] project_name: str - py_version: str + py_version: str | None platform: str | None - location: str + location: str | None precedence: int def __init__( self, @@ -273,16 +285,14 @@ class Distribution(NullProvider): metadata: _MetadataType = None, project_name: str | None = None, version: str | None = None, - py_version: str = ..., + py_version: str | None = ..., platform: str | None = None, precedence: int = 3, ) -> None: ... @classmethod - def from_location( - cls, location: str, basename: str, metadata: _MetadataType = None, **kw: str | int | None - ) -> Distribution: ... + def from_location(cls, location: str, basename: str, metadata: _MetadataType = None, **kw: int) -> Distribution: ... @property - def hashcmp(self) -> tuple[Incomplete, int, str, Incomplete | None, str, str]: ... + def hashcmp(self) -> tuple[parse_version, int, str, str | None, str, str]: ... def __hash__(self) -> int: ... def __lt__(self, other: Distribution) -> bool: ... def __le__(self, other: Distribution) -> bool: ... @@ -296,19 +306,19 @@ class Distribution(NullProvider): def parsed_version(self) -> packaging_version.Version: ... @property def version(self) -> str: ... - def requires(self, extras: tuple[str, ...] = ()) -> list[Requirement]: ... + def requires(self, extras: Iterable[str] = ()) -> list[Requirement]: ... def activate(self, path: list[str] | None = None, replace: bool = False) -> None: ... def egg_name(self) -> str: ... # type: ignore[override] # supertype's egg_name is a variable, not a method @classmethod def from_filename(cls, filename: str, metadata: _MetadataType = None, **kw: str | int | None) -> Distribution: ... def as_requirement(self) -> Requirement: ... - def load_entry_point(self, group: str, name: str) -> Any: ... + def load_entry_point(self, group: str, name: str) -> types.ModuleType: ... @overload def get_entry_map(self, group: None = None) -> dict[str, dict[str, EntryPoint]]: ... @overload def get_entry_map(self, group: str) -> dict[str, EntryPoint]: ... def get_entry_info(self, group: str, name: str) -> EntryPoint | None: ... - def insert_on(self, path: list[StrPath], loc: Incomplete | None = None, replace: bool = False) -> None: ... + def insert_on(self, path: list[str] | list[PathLike[str]], loc: Incomplete | None = None, replace: bool = False) -> None: ... def check_version_conflict(self) -> None: ... def has_version(self) -> bool: ... def clone(self, **kw: str | int | None) -> Requirement: ... @@ -339,6 +349,7 @@ class MemoizedZipManifests(ZipManifests): class manifest_mod(NamedTuple): manifest: _ZipManifestDict mtime: float + def load(self, path: str) -> _ZipManifestDict: ... # type: ignore[override] class ZipProvider(EggProvider): @@ -375,10 +386,13 @@ def safe_version(version: str) -> str: ... def safe_extra(extra: str) -> str: ... def to_filename(name: str) -> str: ... def get_build_platform() -> str: ... -def get_platform() -> str: ... + +get_platform = get_build_platform + def get_supported_platform() -> str: ... def compatible_platforms(provided: str | None, required: str | None) -> bool: ... def get_default_cache() -> str: ... +def _find_adapter(registry: Mapping[type, _AdapterT], ob: object) -> _AdapterT: ... def ensure_directory(path: str) -> None: ... def normalize_path(filename: str) -> str: ... From 360c2e15f7389ce4c079e112ad69a9f63cefcc91 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 20 Feb 2024 03:05:25 -0500 Subject: [PATCH 05/18] . --- stubs/setuptools/pkg_resources/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 59ca4ce96e95..996305e3b11c 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -145,7 +145,7 @@ class EntryPoint: def parse_group(cls, group: str, lines: _NestedStr, dist: Distribution | None = None) -> dict[str, Self]: ... @classmethod def parse_map( - cls, data: dict[str, str | Sequence[str]] | str | Sequence[str], dist: Distribution | None = None + cls, data: _NestedStr | dict[str, _NestedStr], dist: Distribution | None = None ) -> dict[str, dict[str, Self]]: ... def find_distributions(path_item: str, only: bool = False) -> Generator[Distribution, None, None]: ... From 2fa848f3ffbd27b55895654be3f8d37a05ad8a7c Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 20 Feb 2024 20:30:31 -0500 Subject: [PATCH 06/18] load and resolve, used directly, can actually be anything Add _EntryPoint alias --- stubs/setuptools/pkg_resources/__init__.pyi | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 996305e3b11c..fd333f81f53f 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -23,7 +23,8 @@ _InstallerType: TypeAlias = Callable[[Requirement], Distribution | None] _PkgReqType: TypeAlias = str | Requirement _EPDistType: TypeAlias = Distribution | _PkgReqType _MetadataType: TypeAlias = IResourceProvider | None -_ModuleLike: TypeAlias = object # Optionally has __loader__ or __file__ +_ResolvedEntryPoint: TypeAlias = Any # Can be any atribute in the module +_ModuleLike: TypeAlias = object | types.ModuleType # Optionally has __loader__ or __file__ _ProviderFactoryType: TypeAlias = Callable[[_ModuleLike], IResourceProvider] _DistFinderType: TypeAlias = Callable[[_T, str, bool], Iterator[Distribution]] _NSHandlerType: TypeAlias = Callable[[_T, str, str, types.ModuleType], str | None] @@ -109,7 +110,7 @@ class Requirement(packaging_requirements.Requirement): @staticmethod def parse(s: str | Iterable[str]) -> Requirement: ... -def load_entry_point(dist: _EPDistType, group: str, name: str) -> types.ModuleType: ... +def load_entry_point(dist: _EPDistType, group: str, name: str) -> _ResolvedEntryPoint: ... @overload def get_entry_map(dist: _EPDistType, group: None = None) -> dict[str, dict[str, EntryPoint]]: ... @overload @@ -133,10 +134,10 @@ class EntryPoint: @overload def load( self, require: Literal[True] = True, env: Environment | None = None, installer: _InstallerType | None = None - ) -> types.ModuleType: ... + ) -> _ResolvedEntryPoint: ... @overload - def load(self, require: Literal[False], *args: Unused, **kwargs: Unused) -> types.ModuleType: ... - def resolve(self) -> types.ModuleType: ... + def load(self, require: Literal[False], *args: Unused, **kwargs: Unused) -> _ResolvedEntryPoint: ... + def resolve(self) -> _ResolvedEntryPoint: ... def require(self, env: Environment | None = None, installer: _InstallerType | None = None) -> None: ... pattern: ClassVar[Pattern[str]] @classmethod @@ -312,7 +313,7 @@ class Distribution(NullProvider): @classmethod def from_filename(cls, filename: str, metadata: _MetadataType = None, **kw: str | int | None) -> Distribution: ... def as_requirement(self) -> Requirement: ... - def load_entry_point(self, group: str, name: str) -> types.ModuleType: ... + def load_entry_point(self, group: str, name: str) -> _ResolvedEntryPoint: ... @overload def get_entry_map(self, group: None = None) -> dict[str, dict[str, EntryPoint]]: ... @overload From add0fecc334e84f7a205832ce51b256143031b64 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 27 Feb 2024 20:35:25 -0500 Subject: [PATCH 07/18] Solve pytype issue --- stubs/setuptools/pkg_resources/__init__.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index fd333f81f53f..bbbfe0d9efd1 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -13,6 +13,10 @@ from zipimport import zipimporter from ._vendored_packaging import requirements as packaging_requirements, version as packaging_version +# using `map[Requirement]` directly causes errors with pytype https://github.com/google/pytype/issues/1324 +_RequirementMap: TypeAlias = Iterator[Requirement] + +# Used for a protocol method return type @type_check_only class _ResourceStream(BufferedIOBase, BinaryIO, Protocol): ... # type: ignore[misc] # pyright: ignore @@ -94,7 +98,7 @@ class Environment: def __iadd__(self, other: Distribution | Environment) -> Self: ... def __add__(self, other: Distribution | Environment) -> Self: ... -def parse_requirements(strs: str | Iterable[str]) -> map[Requirement]: ... +def parse_requirements(strs: str | Iterable[str]) -> _RequirementMap: ... class RequirementParseError(packaging_requirements.InvalidRequirement): ... From a5c6f83e8825d0559bb18598c72f9d1a500e677a Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 28 Feb 2024 20:55:23 -0500 Subject: [PATCH 08/18] Completely abstract away map as a return type --- stubs/setuptools/pkg_resources/__init__.pyi | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index bbbfe0d9efd1..59466a417d25 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -13,9 +13,6 @@ from zipimport import zipimporter from ._vendored_packaging import requirements as packaging_requirements, version as packaging_version -# using `map[Requirement]` directly causes errors with pytype https://github.com/google/pytype/issues/1324 -_RequirementMap: TypeAlias = Iterator[Requirement] - # Used for a protocol method return type @type_check_only class _ResourceStream(BufferedIOBase, BinaryIO, Protocol): ... # type: ignore[misc] # pyright: ignore @@ -98,7 +95,7 @@ class Environment: def __iadd__(self, other: Distribution | Environment) -> Self: ... def __add__(self, other: Distribution | Environment) -> Self: ... -def parse_requirements(strs: str | Iterable[str]) -> _RequirementMap: ... +def parse_requirements(strs: str | Iterable[str]) -> Iterator[Requirement]: ... class RequirementParseError(packaging_requirements.InvalidRequirement): ... From bf98b15c93cd4a3501e99239f6e4b2d82171f732 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 16:23:07 -0500 Subject: [PATCH 09/18] Don't add new undocumented names --- stubs/setuptools/pkg_resources/__init__.pyi | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 53d0b40deff8..28e421aa4667 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -1,12 +1,12 @@ import types from _typeshed import Incomplete, StrPath, Unused -from collections.abc import Callable, Generator, Iterable, Iterator, Mapping, Sequence +from collections.abc import Callable, Generator, Iterable, Iterator, Sequence from io import BufferedIOBase, BytesIO from itertools import chain from os import PathLike from pkgutil import get_importer as get_importer from re import Pattern -from typing import IO, Any, BinaryIO, ClassVar, Final, Literal, NamedTuple, NoReturn, Protocol, TypeVar, overload, type_check_only +from typing import IO, Any, BinaryIO, ClassVar, Final, Literal, NoReturn, Protocol, TypeVar, overload, type_check_only from typing_extensions import Self, TypeAlias from zipfile import ZipInfo from zipimport import zipimporter @@ -342,26 +342,13 @@ class PathMetadata(DefaultProvider): module_path: str def __init__(self, path: str, egg_info: str) -> None: ... -class ZipManifests(dict[str, MemoizedZipManifests.manifest_mod]): - @classmethod - def build(cls, path: StrPath | IO[bytes]) -> _ZipManifestDict: ... - # Can't do `load = build` because of https://github.com/python/mypy/issues/6700 - load = ZipManifests.build # noqa: F821 - -class MemoizedZipManifests(ZipManifests): - class manifest_mod(NamedTuple): - manifest: _ZipManifestDict - mtime: float - - def load(self, path: str) -> _ZipManifestDict: ... # type: ignore[override] - class ZipProvider(EggProvider): eagers: list[str] | None zip_pre: str # ZipProvider's loader should always be a zipimporter or equivalent loader: zipimporter @property - def zipinfo(self) -> _ZipManifestDict: ... + def zipinfo(self) -> dict[str, ZipInfo]: ... class EggMetadata(ZipProvider): loader: zipimporter @@ -395,7 +382,6 @@ get_platform = get_build_platform def get_supported_platform() -> str: ... def compatible_platforms(provided: str | None, required: str | None) -> bool: ... def get_default_cache() -> str: ... -def _find_adapter(registry: Mapping[type, _AdapterT], ob: object) -> _AdapterT: ... def ensure_directory(path: str) -> None: ... def normalize_path(filename: str) -> str: ... From dfd3df5bf1d4d493dc1d474581b328b5474f156d Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 16:24:12 -0500 Subject: [PATCH 10/18] Keep zipimport the same --- stubs/setuptools/pkg_resources/__init__.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 28e421aa4667..a04b2545a5ad 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -1,4 +1,5 @@ import types +import zipimport from _typeshed import Incomplete, StrPath, Unused from collections.abc import Callable, Generator, Iterable, Iterator, Sequence from io import BufferedIOBase, BytesIO @@ -9,7 +10,6 @@ from re import Pattern from typing import IO, Any, BinaryIO, ClassVar, Final, Literal, NoReturn, Protocol, TypeVar, overload, type_check_only from typing_extensions import Self, TypeAlias from zipfile import ZipInfo -from zipimport import zipimporter from ._vendored_packaging import requirements as packaging_requirements, version as packaging_version @@ -346,14 +346,14 @@ class ZipProvider(EggProvider): eagers: list[str] | None zip_pre: str # ZipProvider's loader should always be a zipimporter or equivalent - loader: zipimporter + loader: zipimport.zipimporter @property def zipinfo(self) -> dict[str, ZipInfo]: ... class EggMetadata(ZipProvider): - loader: zipimporter + loader: zipimport.zipimporter module_path: str - def __init__(self, importer: zipimporter) -> None: ... + def __init__(self, importer: zipimport.zipimporter) -> None: ... class EmptyProvider(NullProvider): module_path: None From 989e444556bc7560542509d2fc3768c1fb3ee65e Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 16:32:58 -0500 Subject: [PATCH 11/18] Remove unused aliases --- stubs/setuptools/pkg_resources/__init__.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index a04b2545a5ad..061bb3fc48d9 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -24,13 +24,11 @@ _InstallerType: TypeAlias = Callable[[Requirement], Distribution | None] _PkgReqType: TypeAlias = str | Requirement _EPDistType: TypeAlias = Distribution | _PkgReqType _MetadataType: TypeAlias = IResourceProvider | None -_ResolvedEntryPoint: TypeAlias = Any # Can be any atribute in the module +_ResolvedEntryPoint: TypeAlias = Any # Can be any attribute in the module _ModuleLike: TypeAlias = object | types.ModuleType # Optionally has __loader__ or __file__ _ProviderFactoryType: TypeAlias = Callable[[_ModuleLike], IResourceProvider] _DistFinderType: TypeAlias = Callable[[_T, str, bool], Iterator[Distribution]] _NSHandlerType: TypeAlias = Callable[[_T, str, str, types.ModuleType], str | None] -_ZipManifestDict: TypeAlias = dict[str, ZipInfo] -_AdapterT = TypeVar("_AdapterT", _DistFinderType[Any], _ProviderFactoryType, _NSHandlerType[Any]) def declare_namespace(packageName: str) -> None: ... def fixup_namespace_packages(path_item: str, parent: str | None = None) -> None: ... From 6ec11730eb411da64ff37688705c715cff778119 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 16:51:09 -0500 Subject: [PATCH 12/18] Override `pkg_resources.ZipProvider.loader` type --- stubs/setuptools/@tests/stubtest_allowlist.txt | 1 + stubs/setuptools/pkg_resources/__init__.pyi | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/stubs/setuptools/@tests/stubtest_allowlist.txt b/stubs/setuptools/@tests/stubtest_allowlist.txt index 9edb8ef70a0b..bca0ce802a4b 100644 --- a/stubs/setuptools/@tests/stubtest_allowlist.txt +++ b/stubs/setuptools/@tests/stubtest_allowlist.txt @@ -1,6 +1,7 @@ # Is always set in __init__ pkg_resources.PathMetadata.egg_info pkg_resources.EggMetadata.loader +pkg_resources.ZipProvider.loader # 1 used for True as a default value setuptools._distutils.dist.Distribution.get_command_obj diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index d15e1c9a7357..c9d3c1b68fd3 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -8,6 +8,7 @@ from io import BytesIO from re import Pattern from typing import IO, Any, ClassVar, Literal, Protocol, TypeVar, overload, type_check_only from typing_extensions import Self, TypeAlias +from zipfile import ZipInfo from ._vendored_packaging import requirements as packaging_requirements, version as packaging_version @@ -21,6 +22,10 @@ _PkgReqType: TypeAlias = str | Requirement _DistFinderType: TypeAlias = Callable[[_Importer, str, bool], Generator[Distribution, None, None]] _NSHandlerType: TypeAlias = Callable[[_Importer, str, str, types.ModuleType], str] +@type_check_only +class _ZipLoaderModule(Protocol): + __loader__: zipimport.zipimporter + def declare_namespace(packageName: str) -> None: ... def fixup_namespace_packages(path_item: str, parent=None) -> None: ... @@ -317,8 +322,11 @@ class PathMetadata(DefaultProvider): class ZipProvider(EggProvider): eagers: list[str] | None zip_pre: str + # ZipProvider's loader should always be a zipimporter + loader: zipimport.zipimporter + def __init__(self, module: _ZipLoaderModule): ... @property - def zipinfo(self): ... + def zipinfo(self) -> dict[str, ZipInfo]: ... class EggMetadata(ZipProvider): loader: zipimport.zipimporter From 03a9bc5d62018383efdf2e2ba4c853f916d4b9cd Mon Sep 17 00:00:00 2001 From: Avasam Date: Sun, 3 Mar 2024 20:06:32 -0500 Subject: [PATCH 13/18] Add setuptools reference in comments https://github.com/pypa/setuptools/pull/4254 --- stubs/setuptools/pkg_resources/__init__.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 597ce20c3908..8db2487c825b 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -337,14 +337,14 @@ class NullProvider: def has_resource(self, resource_name: str) -> bool: ... # Note: runtime forgets to coerce the return type to boolean, # but we need to specify bool for NullProvider to respect the IResourceProvider protocol - # TODO: raise and fix in setuptools' repo + # https://github.com/pypa/setuptools/pull/4254 def has_metadata(self, name: str) -> bool: ... def get_metadata(self, name: str) -> str: ... def get_metadata_lines(self, name: str) -> chain[str]: ... def resource_isdir(self, resource_name: str) -> bool: ... # Note: runtime forgets to coerce the return type to boolean, # but we need to specify bool for NullProvider to respect the IResourceProvider protocol - # TODO: raise and fix in setuptools' repo + # https://github.com/pypa/setuptools/pull/4254 def metadata_isdir(self, name: str) -> bool: ... def resource_listdir(self, resource_name: str) -> list[str]: ... def metadata_listdir(self, name: str) -> list[str]: ... From 25fc709fd246bb041dab93a3f273c0f4af0346ae Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 5 Mar 2024 17:57:59 -0500 Subject: [PATCH 14/18] https://github.com/pypa/setuptools/pull/4254 merged upstream --- stubs/setuptools/pkg_resources/__init__.pyi | 6 ------ 1 file changed, 6 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 597ce20c3908..d2af2f3ca83c 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -335,16 +335,10 @@ class NullProvider: def get_resource_stream(self, manager: ResourceManager, resource_name: str) -> BytesIO: ... def get_resource_string(self, manager: ResourceManager, resource_name: str) -> bytes: ... def has_resource(self, resource_name: str) -> bool: ... - # Note: runtime forgets to coerce the return type to boolean, - # but we need to specify bool for NullProvider to respect the IResourceProvider protocol - # TODO: raise and fix in setuptools' repo def has_metadata(self, name: str) -> bool: ... def get_metadata(self, name: str) -> str: ... def get_metadata_lines(self, name: str) -> chain[str]: ... def resource_isdir(self, resource_name: str) -> bool: ... - # Note: runtime forgets to coerce the return type to boolean, - # but we need to specify bool for NullProvider to respect the IResourceProvider protocol - # TODO: raise and fix in setuptools' repo def metadata_isdir(self, name: str) -> bool: ... def resource_listdir(self, resource_name: str) -> list[str]: ... def metadata_listdir(self, name: str) -> list[str]: ... From 3c5ba1b9902c37e4eb3fd7f1222c0c4a5c35bc29 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 6 Mar 2024 14:57:39 -0500 Subject: [PATCH 15/18] Improvements from pkg_resources merging upstream --- stubs/setuptools/pkg_resources/__init__.pyi | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index d2af2f3ca83c..9ff7bbba82da 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -4,7 +4,6 @@ from _typeshed import Incomplete, StrPath, Unused from collections.abc import Callable, Generator, Iterable, Iterator, Sequence from io import BufferedIOBase, BytesIO from itertools import chain -from os import PathLike from pkgutil import get_importer as get_importer from re import Pattern from typing import IO, Any, BinaryIO, ClassVar, Final, Literal, NoReturn, Protocol, TypeVar, overload, type_check_only @@ -173,7 +172,7 @@ class Environment: AvailableDistributions = Environment -def parse_requirements(strs: str | Iterable[str]) -> Iterator[Requirement]: ... +def parse_requirements(strs: _NestedStr) -> Iterator[Requirement]: ... class RequirementParseError(packaging_requirements.InvalidRequirement): ... @@ -181,7 +180,8 @@ class Requirement(packaging_requirements.Requirement): unsafe_name: str project_name: str key: str - extras: tuple[str, ...] # type: ignore[assignment] # incompatible override of attribute on base class + # packaging.requirements.Requirement uses a set for its extras. setuptools uses a variable-length tuple + extras: tuple[str, ...] # type: ignore[assignment] specs: list[tuple[str, str]] def __init__(self, requirement_string: str) -> None: ... def __eq__(self, other: object) -> bool: ... @@ -203,12 +203,7 @@ class EntryPoint: extras: tuple[str, ...] dist: Distribution | None def __init__( - self, - name: str, - module_name: str, - attrs: tuple[str, ...] = (), - extras: tuple[str, ...] = (), - dist: Distribution | None = None, + self, name: str, module_name: str, attrs: Iterable[str] = (), extras: Iterable[str] = (), dist: Distribution | None = None ) -> None: ... @overload def load( @@ -225,7 +220,7 @@ class EntryPoint: def parse_group(cls, group: str, lines: _NestedStr, dist: Distribution | None = None) -> dict[str, Self]: ... @classmethod def parse_map( - cls, data: _NestedStr | dict[str, _NestedStr], dist: Distribution | None = None + cls, data: str | Iterable[str] | dict[str, str | Iterable[str]], dist: Distribution | None = None ) -> dict[str, dict[str, Self]]: ... def find_distributions(path_item: str, only: bool = False) -> Generator[Distribution, None, None]: ... @@ -391,10 +386,10 @@ class Distribution(NullProvider): @overload def get_entry_map(self, group: str) -> dict[str, EntryPoint]: ... def get_entry_info(self, group: str, name: str) -> EntryPoint | None: ... - def insert_on(self, path: list[str] | list[PathLike[str]], loc: Incomplete | None = None, replace: bool = False) -> None: ... + def insert_on(self, path: list[str], loc: Incomplete | None = None, replace: bool = False) -> None: ... def check_version_conflict(self) -> None: ... def has_version(self) -> bool: ... - def clone(self, **kw: str | int | None) -> Requirement: ... + def clone(self, **kw: str | int | IResourceProvider | None) -> Requirement: ... @property def extras(self) -> list[str]: ... @@ -403,6 +398,8 @@ class DistInfoDistribution(Distribution): EQEQ: ClassVar[Pattern[str]] class EggProvider(NullProvider): + egg_name: str + egg_info: str egg_root: str class DefaultProvider(EggProvider): ... From dbc2b9d05e6d6183c782020e4c4a811bd9b3033a Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 7 Mar 2024 23:27:29 -0500 Subject: [PATCH 16/18] Revert "Make `pkg_resources` pyright strict" This reverts commit a8cdbb220c5633be86e56a4c72969b17691855c6. --- pyrightconfig.stricter.json | 2 +- stubs/setuptools/pkg_resources/__init__.pyi | 48 +++++++++------------ 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/pyrightconfig.stricter.json b/pyrightconfig.stricter.json index 958d489e9c89..a7b7e0a98363 100644 --- a/pyrightconfig.stricter.json +++ b/pyrightconfig.stricter.json @@ -73,7 +73,7 @@ "stubs/redis", "stubs/requests", "stubs/requests-oauthlib", - "stubs/setuptools/setuptools", + "stubs/setuptools", "stubs/stripe", "stubs/tqdm", "stubs/ttkthemes", diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 306eccb103cf..350f6798607c 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -2,20 +2,16 @@ import types import zipimport from _typeshed import Incomplete, StrPath, Unused from collections.abc import Callable, Generator, Iterable, Iterator, Sequence -from io import BufferedIOBase, BytesIO +from io import BytesIO from itertools import chain from pkgutil import get_importer as get_importer from re import Pattern -from typing import IO, Any, BinaryIO, ClassVar, Final, Literal, NoReturn, Protocol, TypeVar, overload, type_check_only +from typing import IO, Any, ClassVar, Final, Literal, NoReturn, Protocol, TypeVar, overload, type_check_only from typing_extensions import Self, TypeAlias from zipfile import ZipInfo from ._vendored_packaging import requirements as packaging_requirements, version as packaging_version -# Used for a protocol method return type -@type_check_only -class _ResourceStream(BufferedIOBase, BinaryIO, Protocol): ... # type: ignore[misc] # pyright: ignore - _T = TypeVar("_T") _D = TypeVar("_D", bound=Distribution) _NestedStr: TypeAlias = str | Iterable[_NestedStr] @@ -108,7 +104,7 @@ class _ZipLoaderModule(Protocol): __loader__: zipimport.zipimporter def declare_namespace(packageName: str) -> None: ... -def fixup_namespace_packages(path_item: str, parent: str | None = None) -> None: ... +def fixup_namespace_packages(path_item: str, parent=None) -> None: ... class WorkingSet: entries: list[str] @@ -126,14 +122,10 @@ class WorkingSet: env: Environment | None = None, installer: _InstallerType | None = None, replace_conflicting: bool = False, - extras: tuple[str, ...] | None = None, + extras=None, ) -> list[Distribution]: ... def find_plugins( - self, - plugin_env: Environment, - full_env: Environment | None = None, - installer: _InstallerType | None = None, - fallback: bool = True, + self, plugin_env: Environment, full_env: Environment | None = None, installer=None, fallback: bool = True ) -> tuple[list[Distribution], dict[Distribution, Exception]]: ... def require(self, *requirements: _NestedStr) -> Sequence[Distribution]: ... def subscribe(self, callback: Callable[[Distribution], object], existing: bool = True) -> None: ... @@ -312,15 +304,15 @@ def register_loader_type(loader_type: type[_ModuleLike], provider_factory: _Prov def register_namespace_handler(importer_type: type[_T], namespace_handler: _NSHandlerType[_T]) -> None: ... class IResourceProvider(IMetadataProvider, Protocol): - def get_resource_filename(self, manager: ResourceManager, resource_name: str) -> StrPath: ... - def get_resource_stream(self, manager: ResourceManager, resource_name: str) -> _ResourceStream: ... - def get_resource_string(self, manager: ResourceManager, resource_name: str) -> bytes: ... - def has_resource(self, resource_name: str) -> bool: ... - def resource_isdir(self, resource_name: str) -> bool: ... - def resource_listdir(self, resource_name: str) -> list[str]: ... + def get_resource_filename(self, manager: ResourceManager, resource_name): ... + def get_resource_stream(self, manager: ResourceManager, resource_name): ... + def get_resource_string(self, manager: ResourceManager, resource_name): ... + def has_resource(self, resource_name): ... + def resource_isdir(self, resource_name): ... + def resource_listdir(self, resource_name): ... -def invalid_marker(text: str) -> SyntaxError | Literal[False]: ... -def evaluate_marker(text: str, extra: str | None = None) -> bool: ... +def invalid_marker(text) -> SyntaxError | Literal[False]: ... +def evaluate_marker(text, extra: Incomplete | None = None): ... class NullProvider: egg_name: str | None @@ -329,16 +321,16 @@ class NullProvider: module_path: str | None def __init__(self, module: _ModuleLike) -> None: ... - def get_resource_filename(self, manager: ResourceManager, resource_name: str) -> str: ... - def get_resource_stream(self, manager: ResourceManager, resource_name: str) -> BytesIO: ... - def get_resource_string(self, manager: ResourceManager, resource_name: str) -> bytes: ... - def has_resource(self, resource_name: str) -> bool: ... + def get_resource_filename(self, manager: ResourceManager, resource_name) -> str: ... + def get_resource_stream(self, manager: ResourceManager, resource_name) -> BytesIO: ... + def get_resource_string(self, manager: ResourceManager, resource_name): ... + def has_resource(self, resource_name) -> bool: ... def has_metadata(self, name: str) -> bool: ... def get_metadata(self, name: str) -> str: ... def get_metadata_lines(self, name: str) -> chain[str]: ... - def resource_isdir(self, resource_name: str) -> bool: ... + def resource_isdir(self, resource_name) -> bool: ... def metadata_isdir(self, name: str) -> bool: ... - def resource_listdir(self, resource_name: str) -> list[str]: ... + def resource_listdir(self, resource_name) -> list[str]: ... def metadata_listdir(self, name: str) -> list[str]: ... def run_script(self, script_name: str, namespace: dict[str, Any]) -> None: ... @@ -389,7 +381,7 @@ class Distribution(NullProvider): @overload def get_entry_map(self, group: str) -> dict[str, EntryPoint]: ... def get_entry_info(self, group: str, name: str) -> EntryPoint | None: ... - def insert_on(self, path: list[str], loc: Incomplete | None = None, replace: bool = False) -> None: ... + def insert_on(self, path, loc: Incomplete | None = None, replace: bool = False) -> None: ... def check_version_conflict(self) -> None: ... def has_version(self) -> bool: ... def clone(self, **kw: str | int | IResourceProvider | None) -> Requirement: ... From f97b36e6e3b70edd4a1ec0003941f5bb7391c1fa Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 7 Mar 2024 23:50:18 -0500 Subject: [PATCH 17/18] More polish --- stubs/setuptools/pkg_resources/__init__.pyi | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 350f6798607c..ac46f47aab49 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -172,7 +172,7 @@ class Requirement(packaging_requirements.Requirement): unsafe_name: str project_name: str key: str - # packaging.requirements.Requirement uses a set for its extras. setuptools uses a variable-length tuple + # packaging.requirements.Requirement uses a set for its extras. setuptools/pkg_resources uses a variable-length tuple extras: tuple[str, ...] # type: ignore[assignment] specs: list[tuple[str, str]] def __init__(self, requirement_string: str) -> None: ... @@ -353,7 +353,9 @@ class Distribution(NullProvider): precedence: int = 3, ) -> None: ... @classmethod - def from_location(cls, location: str, basename: str, metadata: _MetadataType = None, **kw: int) -> Distribution: ... + def from_location( + cls, location: str, basename: str, metadata: _MetadataType = None, *, precedence: int = 3 + ) -> Distribution: ... @property def hashcmp(self) -> tuple[parse_version, int, str, str | None, str, str]: ... def __hash__(self) -> int: ... @@ -373,7 +375,7 @@ class Distribution(NullProvider): def activate(self, path: list[str] | None = None, replace: bool = False) -> None: ... def egg_name(self) -> str: ... # type: ignore[override] # supertype's egg_name is a variable, not a method @classmethod - def from_filename(cls, filename: str, metadata: _MetadataType = None, **kw: str | int | None) -> Distribution: ... + def from_filename(cls, filename: str, metadata: _MetadataType = None, *, precedence: int = 3) -> Distribution: ... def as_requirement(self) -> Requirement: ... def load_entry_point(self, group: str, name: str) -> _ResolvedEntryPoint: ... @overload @@ -432,7 +434,7 @@ class PEP440Warning(RuntimeWarning): ... parse_version = packaging_version.Version -def yield_lines(iterable: _NestedStr) -> Generator[str, None, None]: ... +def yield_lines(iterable: _NestedStr) -> chain[str]: ... def split_sections(s: _NestedStr) -> Generator[tuple[str | None, list[str]], None, None]: ... def safe_name(name: str) -> str: ... def safe_version(version: str) -> str: ... From 71ea49f9ea6d001ee54e98743aa368a7a17c30a7 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 8 Mar 2024 00:33:11 -0500 Subject: [PATCH 18/18] Update stubs/setuptools/pkg_resources/__init__.pyi --- stubs/setuptools/pkg_resources/__init__.pyi | 2 -- 1 file changed, 2 deletions(-) diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index ac46f47aab49..c67416fc99a2 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -395,8 +395,6 @@ class DistInfoDistribution(Distribution): EQEQ: ClassVar[Pattern[str]] class EggProvider(NullProvider): - egg_name: str - egg_info: str egg_root: str class DefaultProvider(EggProvider): ...