exceptions.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from __future__ import annotations
  2. from typing import TYPE_CHECKING, Collection, Generic
  3. from ..structs import CT, RT, RequirementInformation
  4. if TYPE_CHECKING:
  5. from .criterion import Criterion
  6. class ResolverException(Exception):
  7. """A base class for all exceptions raised by this module.
  8. Exceptions derived by this class should all be handled in this module. Any
  9. bubbling pass the resolver should be treated as a bug.
  10. """
  11. class RequirementsConflicted(ResolverException, Generic[RT, CT]):
  12. def __init__(self, criterion: Criterion[RT, CT]) -> None:
  13. super().__init__(criterion)
  14. self.criterion = criterion
  15. def __str__(self) -> str:
  16. return "Requirements conflict: {}".format(
  17. ", ".join(repr(r) for r in self.criterion.iter_requirement()),
  18. )
  19. class InconsistentCandidate(ResolverException, Generic[RT, CT]):
  20. def __init__(self, candidate: CT, criterion: Criterion[RT, CT]):
  21. super().__init__(candidate, criterion)
  22. self.candidate = candidate
  23. self.criterion = criterion
  24. def __str__(self) -> str:
  25. return "Provided candidate {!r} does not satisfy {}".format(
  26. self.candidate,
  27. ", ".join(repr(r) for r in self.criterion.iter_requirement()),
  28. )
  29. class ResolutionError(ResolverException):
  30. pass
  31. class ResolutionImpossible(ResolutionError, Generic[RT, CT]):
  32. def __init__(self, causes: Collection[RequirementInformation[RT, CT]]):
  33. super().__init__(causes)
  34. # causes is a list of RequirementInformation objects
  35. self.causes = causes
  36. class ResolutionTooDeep(ResolutionError):
  37. def __init__(self, round_count: int) -> None:
  38. super().__init__(round_count)
  39. self.round_count = round_count