From 47fc8362593cf29b160a3b19ddca3c8bdaf1f917 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 15 Jul 2023 21:56:21 +0100 Subject: [PATCH] Add stubs for `pkg_resources._vendor.packaging` (#10423) --- stubs/setuptools/pkg_resources/__init__.pyi | 16 ++--- .../_vendor/packaging/__init__.pyi | 8 +++ .../_vendor/packaging/markers.pyi | 13 ++++ .../_vendor/packaging/requirements.pyi | 14 ++++ .../_vendor/packaging/specifiers.pyi | 66 +++++++++++++++++++ .../_vendor/packaging/version.pyi | 49 ++++++++++++++ 6 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 stubs/setuptools/pkg_resources/_vendor/packaging/__init__.pyi create mode 100644 stubs/setuptools/pkg_resources/_vendor/packaging/markers.pyi create mode 100644 stubs/setuptools/pkg_resources/_vendor/packaging/requirements.pyi create mode 100644 stubs/setuptools/pkg_resources/_vendor/packaging/specifiers.pyi create mode 100644 stubs/setuptools/pkg_resources/_vendor/packaging/version.pyi diff --git a/stubs/setuptools/pkg_resources/__init__.pyi b/stubs/setuptools/pkg_resources/__init__.pyi index 05d970c9f8bb..f32c849b1c61 100644 --- a/stubs/setuptools/pkg_resources/__init__.pyi +++ b/stubs/setuptools/pkg_resources/__init__.pyi @@ -9,7 +9,7 @@ from re import Pattern from typing import IO, Any, ClassVar, Protocol, TypeVar, overload, type_check_only from typing_extensions import Literal, Self, TypeAlias -_Version: TypeAlias = Incomplete # from packaging.version +from ._vendor.packaging import requirements as packaging_requirements, version as packaging_version _T = TypeVar("_T") _D = TypeVar("_D", bound=Distribution) @@ -83,16 +83,14 @@ class Environment: def parse_requirements(strs: str | Iterable[str]) -> Generator[Requirement, None, None]: ... -class Requirement: +class RequirementParseError(packaging_requirements.InvalidRequirement): ... + +class Requirement(packaging_requirements.Requirement): unsafe_name: str project_name: str key: str - extras: tuple[str, ...] + extras: tuple[str, ...] # type: ignore[assignment] # incompatible override of attribute on base class specs: list[tuple[str, str]] - url: str | None - # TODO: change this to packaging.markers.Marker | None once we can import - # packaging.markers - marker: Incomplete | None def __init__(self, requirement_string: str) -> None: ... @staticmethod def parse(s: str | Iterable[str]) -> Requirement: ... @@ -268,7 +266,7 @@ class Distribution(NullProvider): @property def version(self) -> str: ... @property - def parsed_version(self) -> tuple[str, ...]: ... + def parsed_version(self) -> packaging_version.Version: ... py_version: str platform: str | None precedence: int @@ -339,7 +337,7 @@ class FileMetadata(EmptyProvider): class PEP440Warning(RuntimeWarning): ... -parse_version = _Version +parse_version = packaging_version.Version def yield_lines(iterable: _NestedStr) -> Generator[str, None, None]: ... def split_sections(s: _NestedStr) -> Generator[tuple[str | None, list[str]], None, None]: ... diff --git a/stubs/setuptools/pkg_resources/_vendor/packaging/__init__.pyi b/stubs/setuptools/pkg_resources/_vendor/packaging/__init__.pyi new file mode 100644 index 000000000000..1981f2d9b273 --- /dev/null +++ b/stubs/setuptools/pkg_resources/_vendor/packaging/__init__.pyi @@ -0,0 +1,8 @@ +__title__: str +__summary__: str +__uri__: str +__version__: str +__author__: str +__email__: str +__license__: str +__copyright__: str diff --git a/stubs/setuptools/pkg_resources/_vendor/packaging/markers.pyi b/stubs/setuptools/pkg_resources/_vendor/packaging/markers.pyi new file mode 100644 index 000000000000..0a7acbbf3a75 --- /dev/null +++ b/stubs/setuptools/pkg_resources/_vendor/packaging/markers.pyi @@ -0,0 +1,13 @@ +__all__ = ["InvalidMarker", "UndefinedComparison", "UndefinedEnvironmentName", "Marker", "default_environment"] + +class InvalidMarker(ValueError): ... +class UndefinedComparison(ValueError): ... +class UndefinedEnvironmentName(ValueError): ... + +def default_environment() -> dict[str, str]: ... + +class Marker: + def __init__(self, marker: str) -> None: ... + def __hash__(self) -> int: ... + def __eq__(self, other: object) -> bool: ... + def evaluate(self, environment: dict[str, str] | None = None) -> bool: ... diff --git a/stubs/setuptools/pkg_resources/_vendor/packaging/requirements.pyi b/stubs/setuptools/pkg_resources/_vendor/packaging/requirements.pyi new file mode 100644 index 000000000000..4f909fa75a23 --- /dev/null +++ b/stubs/setuptools/pkg_resources/_vendor/packaging/requirements.pyi @@ -0,0 +1,14 @@ +from .markers import Marker +from .specifiers import SpecifierSet + +class InvalidRequirement(ValueError): ... + +class Requirement: + name: str + url: str | None + extras: set[str] + specifier: SpecifierSet + marker: Marker | None + def __init__(self, requirement_str: str) -> None: ... + def __hash__(self) -> int: ... + def __eq__(self, other: object) -> bool: ... diff --git a/stubs/setuptools/pkg_resources/_vendor/packaging/specifiers.pyi b/stubs/setuptools/pkg_resources/_vendor/packaging/specifiers.pyi new file mode 100644 index 000000000000..ec86ea306e74 --- /dev/null +++ b/stubs/setuptools/pkg_resources/_vendor/packaging/specifiers.pyi @@ -0,0 +1,66 @@ +import abc +from collections.abc import Iterable, Iterator +from typing import TypeVar +from typing_extensions import TypeAlias + +from .version import Version + +# These exist at runtime, hence the public names +UnparsedVersion: TypeAlias = Version | str +UnparsedVersionVar = TypeVar("UnparsedVersionVar", bound=UnparsedVersion) # noqa: Y001 + +class InvalidSpecifier(ValueError): ... + +class BaseSpecifier(metaclass=abc.ABCMeta): + @abc.abstractmethod + def __str__(self) -> str: ... + @abc.abstractmethod + def __hash__(self) -> int: ... + @abc.abstractmethod + def __eq__(self, other: object) -> bool: ... + @property + @abc.abstractmethod + def prereleases(self) -> bool | None: ... + @prereleases.setter + def prereleases(self, value: bool) -> None: ... + @abc.abstractmethod + def contains(self, item: str, prereleases: bool | None = None) -> bool: ... + @abc.abstractmethod + def filter(self, iterable: Iterable[UnparsedVersionVar], prereleases: bool | None = None) -> Iterator[UnparsedVersionVar]: ... + +class Specifier(BaseSpecifier): + def __init__(self, spec: str = "", prereleases: bool | None = None) -> None: ... + @property # type: ignore[override] + def prereleases(self) -> bool: ... + @prereleases.setter + def prereleases(self, value: bool) -> None: ... + @property + def operator(self) -> str: ... + @property + def version(self) -> str: ... + def __str__(self) -> str: ... # noqa: Y029 # needed as it's abstract on the superclass + def __hash__(self) -> int: ... + def __eq__(self, other: object) -> bool: ... + def __contains__(self, item: Version | str) -> bool: ... + def contains(self, item: UnparsedVersion, prereleases: bool | None = None) -> bool: ... + def filter(self, iterable: Iterable[UnparsedVersionVar], prereleases: bool | None = None) -> Iterator[UnparsedVersionVar]: ... + +class SpecifierSet(BaseSpecifier): + def __init__(self, spec: str = "", prereleases: bool | None = None) -> None: ... + @property + def prereleases(self) -> bool | None: ... + @prereleases.setter + def prereleases(self, value: bool) -> None: ... + @property + def operator(self) -> str: ... + @property + def version(self) -> str: ... + def __str__(self) -> str: ... # noqa: Y029 # needed as it's abstract on the superclass + def __hash__(self) -> int: ... + def __and__(self, other: SpecifierSet | str) -> SpecifierSet: ... + def __len__(self) -> int: ... + def __iter__(self) -> Iterator[Specifier]: ... + def __eq__(self, other: object) -> bool: ... + def __contains__(self, item: UnparsedVersion) -> bool: ... + def contains(self, item: UnparsedVersion, prereleases: bool | None = None, installed: bool | None = None) -> bool: ... + def filter(self, iterable: Iterable[UnparsedVersionVar], prereleases: bool | None = None) -> Iterator[UnparsedVersionVar]: ... diff --git a/stubs/setuptools/pkg_resources/_vendor/packaging/version.pyi b/stubs/setuptools/pkg_resources/_vendor/packaging/version.pyi new file mode 100644 index 000000000000..e05866730dbe --- /dev/null +++ b/stubs/setuptools/pkg_resources/_vendor/packaging/version.pyi @@ -0,0 +1,49 @@ +from typing_extensions import Final + +__all__ = ["VERSION_PATTERN", "parse", "Version", "InvalidVersion"] + +def parse(version: str) -> Version: ... + +class InvalidVersion(ValueError): ... + +VERSION_PATTERN: Final[str] + +class _BaseVersion: + def __hash__(self) -> int: ... + def __lt__(self, other: _BaseVersion) -> bool: ... + def __le__(self, other: _BaseVersion) -> bool: ... + def __ge__(self, other: _BaseVersion) -> bool: ... + def __gt__(self, other: _BaseVersion) -> bool: ... + def __eq__(self, other: object) -> bool: ... + def __ne__(self, other: object) -> bool: ... + +class Version(_BaseVersion): + def __init__(self, version: str) -> None: ... + @property + def epoch(self) -> int: ... + @property + def release(self) -> tuple[int, ...]: ... + @property + def pre(self) -> tuple[str, int] | None: ... + @property + def post(self) -> int | None: ... + @property + def dev(self) -> int | None: ... + @property + def local(self) -> str | None: ... + @property + def public(self) -> str: ... + @property + def base_version(self) -> str: ... + @property + def is_prerelease(self) -> bool: ... + @property + def is_postrelease(self) -> bool: ... + @property + def is_devrelease(self) -> bool: ... + @property + def major(self) -> int: ... + @property + def minor(self) -> int: ... + @property + def micro(self) -> int: ...