1import importlib.abc
2import types
3import zipimport
4from abc import ABCMeta
5from typing import IO, Any, Callable, Dict, Generator, Iterable, List, Optional, Sequence, Set, Tuple, TypeVar, Union, overload
6
7LegacyVersion = Any  # from packaging.version
8Version = Any  # from packaging.version
9
10_T = TypeVar("_T")
11_NestedStr = Union[str, Iterable[Union[str, Iterable[Any]]]]
12_InstallerType = Callable[[Requirement], Optional[Distribution]]
13_EPDistType = Union[Distribution, Requirement, str]
14_MetadataType = Optional[IResourceProvider]
15_PkgReqType = Union[str, Requirement]
16_DistFinderType = Callable[[_Importer, str, bool], Generator[Distribution, None, None]]
17_NSHandlerType = Callable[[_Importer, str, str, types.ModuleType], str]
18
19def declare_namespace(name: str) -> None: ...
20def fixup_namespace_packages(path_item: str) -> None: ...
21
22class WorkingSet:
23    entries: List[str]
24    def __init__(self, entries: Optional[Iterable[str]] = ...) -> None: ...
25    def require(self, *requirements: _NestedStr) -> Sequence[Distribution]: ...
26    def run_script(self, requires: str, script_name: str) -> None: ...
27    def iter_entry_points(self, group: str, name: Optional[str] = ...) -> Generator[EntryPoint, None, None]: ...
28    def add_entry(self, entry: str) -> None: ...
29    def __contains__(self, dist: Distribution) -> bool: ...
30    def __iter__(self) -> Generator[Distribution, None, None]: ...
31    def find(self, req: Requirement) -> Optional[Distribution]: ...
32    def resolve(
33        self, requirements: Iterable[Requirement], env: Optional[Environment] = ..., installer: Optional[_InstallerType] = ...
34    ) -> List[Distribution]: ...
35    def add(self, dist: Distribution, entry: Optional[str] = ..., insert: bool = ..., replace: bool = ...) -> None: ...
36    def subscribe(self, callback: Callable[[Distribution], None]) -> None: ...
37    def find_plugins(
38        self, plugin_env: Environment, full_env: Optional[Environment] = ..., fallback: bool = ...
39    ) -> Tuple[List[Distribution], Dict[Distribution, Exception]]: ...
40
41working_set: WorkingSet = ...
42
43require = working_set.require
44run_script = working_set.run_script
45iter_entry_points = working_set.iter_entry_points
46add_activation_listener = working_set.subscribe
47
48class Environment:
49    def __init__(
50        self, search_path: Optional[Sequence[str]] = ..., platform: Optional[str] = ..., python: Optional[str] = ...
51    ) -> None: ...
52    def __getitem__(self, project_name: str) -> List[Distribution]: ...
53    def __iter__(self) -> Generator[str, None, None]: ...
54    def add(self, dist: Distribution) -> None: ...
55    def remove(self, dist: Distribution) -> None: ...
56    def can_add(self, dist: Distribution) -> bool: ...
57    def __add__(self, other: Union[Distribution, Environment]) -> Environment: ...
58    def __iadd__(self, other: Union[Distribution, Environment]) -> Environment: ...
59    @overload
60    def best_match(self, req: Requirement, working_set: WorkingSet) -> Distribution: ...
61    @overload
62    def best_match(self, req: Requirement, working_set: WorkingSet, installer: Callable[[Requirement], _T] = ...) -> _T: ...
63    @overload
64    def obtain(self, requirement: Requirement) -> None: ...
65    @overload
66    def obtain(self, requirement: Requirement, installer: Callable[[Requirement], _T] = ...) -> _T: ...
67    def scan(self, search_path: Optional[Sequence[str]] = ...) -> None: ...
68
69def parse_requirements(strs: Union[str, Iterable[str]]) -> Generator[Requirement, None, None]: ...
70
71class Requirement:
72    unsafe_name: str
73    project_name: str
74    key: str
75    extras: Tuple[str, ...]
76    specs: List[Tuple[str, str]]
77    # TODO: change this to Optional[packaging.markers.Marker] once we can import
78    #       packaging.markers
79    marker: Optional[Any]
80    @staticmethod
81    def parse(s: Union[str, Iterable[str]]) -> Requirement: ...
82    def __contains__(self, item: Union[Distribution, str, Tuple[str, ...]]) -> bool: ...
83    def __eq__(self, other_requirement: Any) -> bool: ...
84
85def load_entry_point(dist: _EPDistType, group: str, name: str) -> Any: ...
86def get_entry_info(dist: _EPDistType, group: str, name: str) -> Optional[EntryPoint]: ...
87@overload
88def get_entry_map(dist: _EPDistType) -> Dict[str, Dict[str, EntryPoint]]: ...
89@overload
90def get_entry_map(dist: _EPDistType, group: str) -> Dict[str, EntryPoint]: ...
91
92class EntryPoint:
93    name: str
94    module_name: str
95    attrs: Tuple[str, ...]
96    extras: Tuple[str, ...]
97    dist: Optional[Distribution]
98    def __init__(
99        self,
100        name: str,
101        module_name: str,
102        attrs: Tuple[str, ...] = ...,
103        extras: Tuple[str, ...] = ...,
104        dist: Optional[Distribution] = ...,
105    ) -> None: ...
106    @classmethod
107    def parse(cls, src: str, dist: Optional[Distribution] = ...) -> EntryPoint: ...
108    @classmethod
109    def parse_group(
110        cls, group: str, lines: Union[str, Sequence[str]], dist: Optional[Distribution] = ...
111    ) -> Dict[str, EntryPoint]: ...
112    @classmethod
113    def parse_map(
114        cls, data: Union[Dict[str, Union[str, Sequence[str]]], str, Sequence[str]], dist: Optional[Distribution] = ...
115    ) -> Dict[str, EntryPoint]: ...
116    def load(self, require: bool = ..., env: Optional[Environment] = ..., installer: Optional[_InstallerType] = ...) -> Any: ...
117    def require(self, env: Optional[Environment] = ..., installer: Optional[_InstallerType] = ...) -> None: ...
118    def resolve(self) -> Any: ...
119
120def find_distributions(path_item: str, only: bool = ...) -> Generator[Distribution, None, None]: ...
121def get_distribution(dist: Union[Requirement, str, Distribution]) -> Distribution: ...
122
123class Distribution(IResourceProvider, IMetadataProvider):
124    PKG_INFO: str
125    location: str
126    project_name: str
127    key: str
128    extras: List[str]
129    version: str
130    parsed_version: Tuple[str, ...]
131    py_version: str
132    platform: Optional[str]
133    precedence: int
134    def __init__(
135        self,
136        location: Optional[str] = ...,
137        metadata: _MetadataType = ...,
138        project_name: Optional[str] = ...,
139        version: Optional[str] = ...,
140        py_version: str = ...,
141        platform: Optional[str] = ...,
142        precedence: int = ...,
143    ) -> None: ...
144    @classmethod
145    def from_location(
146        cls, location: str, basename: str, metadata: _MetadataType = ..., **kw: Union[str, None, int]
147    ) -> Distribution: ...
148    @classmethod
149    def from_filename(cls, filename: str, metadata: _MetadataType = ..., **kw: Union[str, None, int]) -> Distribution: ...
150    def activate(self, path: Optional[List[str]] = ...) -> None: ...
151    def as_requirement(self) -> Requirement: ...
152    def requires(self, extras: Tuple[str, ...] = ...) -> List[Requirement]: ...
153    def clone(self, **kw: Union[str, int, None]) -> Requirement: ...
154    def egg_name(self) -> str: ...
155    def __cmp__(self, other: Any) -> bool: ...
156    def get_entry_info(self, group: str, name: str) -> Optional[EntryPoint]: ...
157    @overload
158    def get_entry_map(self) -> Dict[str, Dict[str, EntryPoint]]: ...
159    @overload
160    def get_entry_map(self, group: str) -> Dict[str, EntryPoint]: ...
161    def load_entry_point(self, group: str, name: str) -> Any: ...
162
163EGG_DIST: int
164BINARY_DIST: int
165SOURCE_DIST: int
166CHECKOUT_DIST: int
167DEVELOP_DIST: int
168
169def resource_exists(package_or_requirement: _PkgReqType, resource_name: str) -> bool: ...
170def resource_stream(package_or_requirement: _PkgReqType, resource_name: str) -> IO[bytes]: ...
171def resource_string(package_or_requirement: _PkgReqType, resource_name: str) -> bytes: ...
172def resource_isdir(package_or_requirement: _PkgReqType, resource_name: str) -> bool: ...
173def resource_listdir(package_or_requirement: _PkgReqType, resource_name: str) -> List[str]: ...
174def resource_filename(package_or_requirement: _PkgReqType, resource_name: str) -> str: ...
175def set_extraction_path(path: str) -> None: ...
176def cleanup_resources(force: bool = ...) -> List[str]: ...
177
178class IResourceManager:
179    def resource_exists(self, package_or_requirement: _PkgReqType, resource_name: str) -> bool: ...
180    def resource_stream(self, package_or_requirement: _PkgReqType, resource_name: str) -> IO[bytes]: ...
181    def resource_string(self, package_or_requirement: _PkgReqType, resource_name: str) -> bytes: ...
182    def resource_isdir(self, package_or_requirement: _PkgReqType, resource_name: str) -> bool: ...
183    def resource_listdir(self, package_or_requirement: _PkgReqType, resource_name: str) -> List[str]: ...
184    def resource_filename(self, package_or_requirement: _PkgReqType, resource_name: str) -> str: ...
185    def set_extraction_path(self, path: str) -> None: ...
186    def cleanup_resources(self, force: bool = ...) -> List[str]: ...
187    def get_cache_path(self, archive_name: str, names: Iterable[str] = ...) -> str: ...
188    def extraction_error(self) -> None: ...
189    def postprocess(self, tempname: str, filename: str) -> None: ...
190
191@overload
192def get_provider(package_or_requirement: str) -> IResourceProvider: ...
193@overload
194def get_provider(package_or_requirement: Requirement) -> Distribution: ...
195
196class IMetadataProvider:
197    def has_metadata(self, name: str) -> bool: ...
198    def metadata_isdir(self, name: str) -> bool: ...
199    def metadata_listdir(self, name: str) -> List[str]: ...
200    def get_metadata(self, name: str) -> str: ...
201    def get_metadata_lines(self, name: str) -> Generator[str, None, None]: ...
202    def run_script(self, script_name: str, namespace: Dict[str, Any]) -> None: ...
203
204class ResolutionError(Exception): ...
205
206class DistributionNotFound(ResolutionError):
207    @property
208    def req(self) -> Requirement: ...
209    @property
210    def requirers(self) -> Set[str]: ...
211    @property
212    def requirers_str(self) -> str: ...
213    def report(self) -> str: ...
214
215class VersionConflict(ResolutionError):
216    @property
217    def dist(self) -> Any: ...
218    @property
219    def req(self) -> Any: ...
220    def report(self) -> str: ...
221    def with_context(self, required_by: Set[Union[Distribution, str]]) -> VersionConflict: ...
222
223class ContextualVersionConflict(VersionConflict):
224    @property
225    def required_by(self) -> Set[Union[Distribution, str]]: ...
226
227class UnknownExtra(ResolutionError): ...
228
229class ExtractionError(Exception):
230    manager: IResourceManager
231    cache_path: str
232    original_error: Exception
233
234class _Importer(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader, metaclass=ABCMeta): ...
235
236def register_finder(importer_type: type, distribution_finder: _DistFinderType) -> None: ...
237def register_loader_type(loader_type: type, provider_factory: Callable[[types.ModuleType], IResourceProvider]) -> None: ...
238def register_namespace_handler(importer_type: type, namespace_handler: _NSHandlerType) -> None: ...
239
240class IResourceProvider(IMetadataProvider): ...
241class NullProvider: ...
242class EggProvider(NullProvider): ...
243class DefaultProvider(EggProvider): ...
244
245class PathMetadata(DefaultProvider, IResourceProvider):
246    def __init__(self, path: str, egg_info: str) -> None: ...
247
248class ZipProvider(EggProvider): ...
249
250class EggMetadata(ZipProvider, IResourceProvider):
251    def __init__(self, zipimporter: zipimport.zipimporter) -> None: ...
252
253class EmptyProvider(NullProvider): ...
254
255empty_provider: EmptyProvider
256
257class FileMetadata(EmptyProvider, IResourceProvider):
258    def __init__(self, path_to_pkg_info: str) -> None: ...
259
260def parse_version(v: str) -> Union[Version, LegacyVersion]: ...
261def yield_lines(strs: _NestedStr) -> Generator[str, None, None]: ...
262def split_sections(strs: _NestedStr) -> Generator[Tuple[Optional[str], str], None, None]: ...
263def safe_name(name: str) -> str: ...
264def safe_version(version: str) -> str: ...
265def safe_extra(extra: str) -> str: ...
266def to_filename(name_or_version: str) -> str: ...
267def get_build_platform() -> str: ...
268def get_platform() -> str: ...
269def get_supported_platform() -> str: ...
270def compatible_platforms(provided: Optional[str], required: Optional[str]) -> bool: ...
271def get_default_cache() -> str: ...
272def get_importer(path_item: str) -> _Importer: ...
273def ensure_directory(path: str) -> None: ...
274def normalize_path(filename: str) -> str: ...
275