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 2 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
40 changes: 23 additions & 17 deletions stubs/setuptools/pkg_resources/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import types
import zipimport
from _typeshed import Incomplete
from collections.abc import Callable, Generator, Iterable, Iterator, Sequence
from collections.abc import Callable, Generator, Iterable, Iterator, Mapping, 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 @@
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 @@ -176,9 +177,9 @@
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 parse(s: str | Iterable[str]) -> Self: ...

Check failure on line 180 in stubs/setuptools/pkg_resources/__init__.pyi

View workflow job for this annotation

GitHub Actions / Test typeshed with pyright (Linux, 3.11)

"Self" is not valid in this context (reportGeneralTypeIssues)

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,8 +201,10 @@
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: ...
Expand Down Expand Up @@ -235,7 +238,7 @@
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 All @@ -262,30 +265,33 @@
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: ...
def run_script(self, script_name: str, namespace: Mapping[str, object]) -> None: ...

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 @@ -327,7 +333,7 @@
def metadata_isdir(self, name: str) -> bool: ...
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: ...
def run_script(self, script_name: str, namespace: Mapping[str, object]) -> None: ...

# Doesn't actually extend NullProvider, solves a typing issue in pytype_test.py
class Distribution(NullProvider):
Expand Down Expand Up @@ -372,7 +378,7 @@
@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