From 40ccfddb94773b73102e615a71f59b2a11860470 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Tue, 29 Aug 2023 11:40:27 +0800 Subject: [PATCH] fix typing Signed-off-by: Frost Ming --- src/resolvelib/providers.py | 8 ++++++-- src/resolvelib/resolvers.py | 4 ++-- tests/test_resolvers.py | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/resolvelib/providers.py b/src/resolvelib/providers.py index 237e489..127b4f1 100644 --- a/src/resolvelib/providers.py +++ b/src/resolvelib/providers.py @@ -12,7 +12,11 @@ from .structs import CT, KT, RT, Matches, RequirementInformation if TYPE_CHECKING: - from _typeshed import SupportsRichComparison + from typing import Any, Protocol + + class Preference(Protocol): + def __lt__(self, __other: Any) -> bool: + ... class AbstractProvider(Generic[RT, CT, KT]): @@ -34,7 +38,7 @@ def get_preference( candidates: Mapping[KT, Iterator[CT]], information: Mapping[KT, Iterator[RequirementInformation[RT, CT]]], backtrack_causes: Sequence[RequirementInformation[RT, CT]], - ) -> SupportsRichComparison: + ) -> Preference: """Produce a sort key for given requirement based on preference. The preference is defined as "I think this requirement should be diff --git a/src/resolvelib/resolvers.py b/src/resolvelib/resolvers.py index 595f236..58a48dd 100644 --- a/src/resolvelib/resolvers.py +++ b/src/resolvelib/resolvers.py @@ -29,7 +29,7 @@ ) if TYPE_CHECKING: - from _typeshed import SupportsRichComparison + from .providers import Preference class Result(NamedTuple, Generic[RT, CT, KT]): mapping: Mapping[KT, CT] @@ -197,7 +197,7 @@ def _remove_information_from_criteria( criterion.incompatibilities, ) - def _get_preference(self, name: KT) -> SupportsRichComparison: + def _get_preference(self, name: KT) -> Preference: return self._p.get_preference( identifier=name, resolutions=self.state.mapping, diff --git a/tests/test_resolvers.py b/tests/test_resolvers.py index 709fd9d..46d0f5d 100644 --- a/tests/test_resolvers.py +++ b/tests/test_resolvers.py @@ -203,7 +203,7 @@ def test_pin_conflict_with_self(monkeypatch, reporter): ], } - class Provider(AbstractProvider[str, str, Candidate]): + class Provider(AbstractProvider[str, Candidate, str]): def identify(self, requirement_or_candidate: str | Candidate) -> str: result = ( Requirement(requirement_or_candidate).name @@ -260,7 +260,7 @@ def get_updated_criteria_patch(self, candidate): Resolution, "_get_updated_criteria", get_updated_criteria_patch ) - resolver: Resolver[str, str, Candidate] = Resolver(Provider(), reporter) + resolver: Resolver[str, Candidate, str] = Resolver(Provider(), reporter) result = resolver.resolve(["child", "parent"]) def get_child_versions(