providers.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from __future__ import annotations
  2. from typing import (
  3. TYPE_CHECKING,
  4. Generic,
  5. Iterable,
  6. Iterator,
  7. Mapping,
  8. Sequence,
  9. )
  10. from .structs import CT, KT, RT, Matches, RequirementInformation
  11. if TYPE_CHECKING:
  12. from typing import Any, Protocol
  13. class Preference(Protocol):
  14. def __lt__(self, __other: Any) -> bool: ...
  15. class AbstractProvider(Generic[RT, CT, KT]):
  16. """Delegate class to provide the required interface for the resolver."""
  17. def identify(self, requirement_or_candidate: RT | CT) -> KT:
  18. """Given a requirement or candidate, return an identifier for it.
  19. This is used to identify, e.g. whether two requirements
  20. should have their specifier parts merged or a candidate matches a
  21. requirement via ``find_matches()``.
  22. """
  23. raise NotImplementedError
  24. def get_preference(
  25. self,
  26. identifier: KT,
  27. resolutions: Mapping[KT, CT],
  28. candidates: Mapping[KT, Iterator[CT]],
  29. information: Mapping[KT, Iterator[RequirementInformation[RT, CT]]],
  30. backtrack_causes: Sequence[RequirementInformation[RT, CT]],
  31. ) -> Preference:
  32. """Produce a sort key for given requirement based on preference.
  33. As this is a sort key it will be called O(n) times per backtrack
  34. step, where n is the number of `identifier`s, if you have a check
  35. which is expensive in some sense. E.g. It needs to make O(n) checks
  36. per call or takes significant wall clock time, consider using
  37. `narrow_requirement_selection` to filter the `identifier`s, which
  38. is applied before this sort key is called.
  39. The preference is defined as "I think this requirement should be
  40. resolved first". The lower the return value is, the more preferred
  41. this group of arguments is.
  42. :param identifier: An identifier as returned by ``identify()``. This
  43. identifies the requirement being considered.
  44. :param resolutions: Mapping of candidates currently pinned by the
  45. resolver. Each key is an identifier, and the value is a candidate.
  46. The candidate may conflict with requirements from ``information``.
  47. :param candidates: Mapping of each dependency's possible candidates.
  48. Each value is an iterator of candidates.
  49. :param information: Mapping of requirement information of each package.
  50. Each value is an iterator of *requirement information*.
  51. :param backtrack_causes: Sequence of *requirement information* that are
  52. the requirements that caused the resolver to most recently
  53. backtrack.
  54. A *requirement information* instance is a named tuple with two members:
  55. * ``requirement`` specifies a requirement contributing to the current
  56. list of candidates.
  57. * ``parent`` specifies the candidate that provides (depended on) the
  58. requirement, or ``None`` to indicate a root requirement.
  59. The preference could depend on various issues, including (not
  60. necessarily in this order):
  61. * Is this package pinned in the current resolution result?
  62. * How relaxed is the requirement? Stricter ones should probably be
  63. worked on first? (I don't know, actually.)
  64. * How many possibilities are there to satisfy this requirement? Those
  65. with few left should likely be worked on first, I guess?
  66. * Are there any known conflicts for this requirement? We should
  67. probably work on those with the most known conflicts.
  68. A sortable value should be returned (this will be used as the ``key``
  69. parameter of the built-in sorting function). The smaller the value is,
  70. the more preferred this requirement is (i.e. the sorting function
  71. is called with ``reverse=False``).
  72. """
  73. raise NotImplementedError
  74. def find_matches(
  75. self,
  76. identifier: KT,
  77. requirements: Mapping[KT, Iterator[RT]],
  78. incompatibilities: Mapping[KT, Iterator[CT]],
  79. ) -> Matches[CT]:
  80. """Find all possible candidates that satisfy the given constraints.
  81. :param identifier: An identifier as returned by ``identify()``. All
  82. candidates returned by this method should produce the same
  83. identifier.
  84. :param requirements: A mapping of requirements that all returned
  85. candidates must satisfy. Each key is an identifier, and the value
  86. an iterator of requirements for that dependency.
  87. :param incompatibilities: A mapping of known incompatibile candidates of
  88. each dependency. Each key is an identifier, and the value an
  89. iterator of incompatibilities known to the resolver. All
  90. incompatibilities *must* be excluded from the return value.
  91. This should try to get candidates based on the requirements' types.
  92. For VCS, local, and archive requirements, the one-and-only match is
  93. returned, and for a "named" requirement, the index(es) should be
  94. consulted to find concrete candidates for this requirement.
  95. The return value should produce candidates ordered by preference; the
  96. most preferred candidate should come first. The return type may be one
  97. of the following:
  98. * A callable that returns an iterator that yields candidates.
  99. * An collection of candidates.
  100. * An iterable of candidates. This will be consumed immediately into a
  101. list of candidates.
  102. """
  103. raise NotImplementedError
  104. def is_satisfied_by(self, requirement: RT, candidate: CT) -> bool:
  105. """Whether the given requirement can be satisfied by a candidate.
  106. The candidate is guaranteed to have been generated from the
  107. requirement.
  108. A boolean should be returned to indicate whether ``candidate`` is a
  109. viable solution to the requirement.
  110. """
  111. raise NotImplementedError
  112. def get_dependencies(self, candidate: CT) -> Iterable[RT]:
  113. """Get dependencies of a candidate.
  114. This should return a collection of requirements that `candidate`
  115. specifies as its dependencies.
  116. """
  117. raise NotImplementedError
  118. def narrow_requirement_selection(
  119. self,
  120. identifiers: Iterable[KT],
  121. resolutions: Mapping[KT, CT],
  122. candidates: Mapping[KT, Iterator[CT]],
  123. information: Mapping[KT, Iterator[RequirementInformation[RT, CT]]],
  124. backtrack_causes: Sequence[RequirementInformation[RT, CT]],
  125. ) -> Iterable[KT]:
  126. """
  127. An optional method to narrow the selection of requirements being
  128. considered during resolution. This method is called O(1) time per
  129. backtrack step.
  130. :param identifiers: An iterable of `identifiers` as returned by
  131. ``identify()``. These identify all requirements currently being
  132. considered.
  133. :param resolutions: A mapping of candidates currently pinned by the
  134. resolver. Each key is an identifier, and the value is a candidate
  135. that may conflict with requirements from ``information``.
  136. :param candidates: A mapping of each dependency's possible candidates.
  137. Each value is an iterator of candidates.
  138. :param information: A mapping of requirement information for each package.
  139. Each value is an iterator of *requirement information*.
  140. :param backtrack_causes: A sequence of *requirement information* that are
  141. the requirements causing the resolver to most recently
  142. backtrack.
  143. A *requirement information* instance is a named tuple with two members:
  144. * ``requirement`` specifies a requirement contributing to the current
  145. list of candidates.
  146. * ``parent`` specifies the candidate that provides (is depended on for)
  147. the requirement, or ``None`` to indicate a root requirement.
  148. Must return a non-empty subset of `identifiers`, with the default
  149. implementation being to return `identifiers` unchanged. Those `identifiers`
  150. will then be passed to the sort key `get_preference` to pick the most
  151. prefered requirement to attempt to pin, unless `narrow_requirement_selection`
  152. returns only 1 requirement, in which case that will be used without
  153. calling the sort key `get_preference`.
  154. This method is designed to be used by the provider to optimize the
  155. dependency resolution, e.g. if a check cost is O(m) and it can be done
  156. against all identifiers at once then filtering the requirement selection
  157. here will cost O(m) but making it part of the sort key in `get_preference`
  158. will cost O(m*n), where n is the number of `identifiers`.
  159. Returns:
  160. Iterable[KT]: A non-empty subset of `identifiers`.
  161. """
  162. return identifiers