requirements.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import annotations
  5. from typing import Any, Iterator
  6. from ._parser import parse_requirement as _parse_requirement
  7. from ._tokenizer import ParserSyntaxError
  8. from .markers import Marker, _normalize_extra_values
  9. from .specifiers import SpecifierSet
  10. from .utils import canonicalize_name
  11. class InvalidRequirement(ValueError):
  12. """
  13. An invalid requirement was found, users should refer to PEP 508.
  14. """
  15. class Requirement:
  16. """Parse a requirement.
  17. Parse a given requirement string into its parts, such as name, specifier,
  18. URL, and extras. Raises InvalidRequirement on a badly-formed requirement
  19. string.
  20. """
  21. # TODO: Can we test whether something is contained within a requirement?
  22. # If so how do we do that? Do we need to test against the _name_ of
  23. # the thing as well as the version? What about the markers?
  24. # TODO: Can we normalize the name and extra name?
  25. def __init__(self, requirement_string: str) -> None:
  26. try:
  27. parsed = _parse_requirement(requirement_string)
  28. except ParserSyntaxError as e:
  29. raise InvalidRequirement(str(e)) from e
  30. self.name: str = parsed.name
  31. self.url: str | None = parsed.url or None
  32. self.extras: set[str] = set(parsed.extras or [])
  33. self.specifier: SpecifierSet = SpecifierSet(parsed.specifier)
  34. self.marker: Marker | None = None
  35. if parsed.marker is not None:
  36. self.marker = Marker.__new__(Marker)
  37. self.marker._markers = _normalize_extra_values(parsed.marker)
  38. def _iter_parts(self, name: str) -> Iterator[str]:
  39. yield name
  40. if self.extras:
  41. formatted_extras = ",".join(sorted(self.extras))
  42. yield f"[{formatted_extras}]"
  43. if self.specifier:
  44. yield str(self.specifier)
  45. if self.url:
  46. yield f"@ {self.url}"
  47. if self.marker:
  48. yield " "
  49. if self.marker:
  50. yield f"; {self.marker}"
  51. def __str__(self) -> str:
  52. return "".join(self._iter_parts(self.name))
  53. def __repr__(self) -> str:
  54. return f"<Requirement('{self}')>"
  55. def __hash__(self) -> int:
  56. return hash(
  57. (
  58. self.__class__.__name__,
  59. *self._iter_parts(canonicalize_name(self.name)),
  60. )
  61. )
  62. def __eq__(self, other: Any) -> bool:
  63. if not isinstance(other, Requirement):
  64. return NotImplemented
  65. return (
  66. canonicalize_name(self.name) == canonicalize_name(other.name)
  67. and self.extras == other.extras
  68. and self.specifier == other.specifier
  69. and self.url == other.url
  70. and self.marker == other.marker
  71. )