Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg_resources: Remove stray Anys and use more Self & NoReturn types #11528

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions stubs/setuptools/pkg_resources/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ from collections.abc import Callable, Generator, Iterable, Iterator, Sequence
from io import BytesIO
from pkgutil import get_importer as get_importer
from re import Pattern
from typing import IO, Any, ClassVar, Final, Literal, 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

_T = TypeVar("_T")
_D = TypeVar("_D", bound=Distribution)
_NestedStr: TypeAlias = str | Iterable[str | Iterable[Any]]
_NestedStr: TypeAlias = str | Iterable[str | Iterable[_NestedStr]]
_InstallerType: TypeAlias = Callable[[Requirement], Distribution | None]
_EPDistType: TypeAlias = Distribution | Requirement | str
_MetadataType: TypeAlias = IResourceProvider | None
_PkgReqType: TypeAlias = str | Requirement
_ResolvedEntryPoint: TypeAlias = Any # Can be any attribute in the module
_ModuleLike: TypeAlias = object | types.ModuleType # Any object that optionally has __loader__ or __file__, usually a module
_ProviderFactoryType: TypeAlias = Callable[[_ModuleLike], IResourceProvider]
_DistFinderType: TypeAlias = Callable[[_T, str, bool], Iterable[Distribution]]
Expand Down Expand Up @@ -158,7 +159,7 @@ 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: ...

AvailableDistributions = Environment

Expand All @@ -178,7 +179,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) -> _ResolvedEntryPoint: ...
@overload
def get_entry_map(dist: _EPDistType, group: None = None) -> dict[str, dict[str, EntryPoint]]: ...
@overload
Expand All @@ -200,11 +201,13 @@ 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 load(
self, require: bool = True, env: Environment | None = ..., installer: _InstallerType | None = ...
) -> _ResolvedEntryPoint: ...
def resolve(self) -> _ResolvedEntryPoint: ...
def require(self, env: Environment | None = None, installer: _InstallerType | None = None) -> None: ...
@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]: ...
@classmethod
Expand Down Expand Up @@ -235,7 +238,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: ...
Expand Down Expand Up @@ -267,25 +270,28 @@ class IMetadataProvider(Protocol):
class ResolutionError(Exception): ...

class DistributionNotFound(ResolutionError):
def __init__(self, req: Requirement, requirers: set[str] | None, /, *args: object) -> None: ...
Copy link
Collaborator Author

@Avasam Avasam Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResolutionError subclasses still work with any number of arguments which will show up in the __repr__ string

@property
def req(self) -> Requirement: ...
@property
def requirers(self) -> set[str]: ...
def requirers(self) -> set[str] | None: ...
@property
def requirers_str(self) -> str: ...
def report(self) -> str: ...

class VersionConflict(ResolutionError):
def __init__(self, dist: Distribution, req: Requirement, /, *args: object) -> None: ...
@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[str]) -> Self | ContextualVersionConflict: ...

class ContextualVersionConflict(VersionConflict):
def __init__(self, dist: Distribution, req: Requirement, required_by: set[str], /, *args: object) -> None: ...
@property
def required_by(self) -> set[Distribution | str]: ...
def required_by(self) -> set[str]: ...

class UnknownExtra(ResolutionError): ...

Expand Down Expand Up @@ -372,7 +378,7 @@ class Distribution(NullProvider):
@classmethod
def from_filename(cls, filename: str, metadata: _MetadataType = None, **kw: str | None | int) -> 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) -> _ResolvedEntryPoint: ...
@overload
def get_entry_map(self, group: None = None) -> dict[str, dict[str, EntryPoint]]: ...
@overload
Expand Down
Loading