requirements.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 TYPE_CHECKING
  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 InvalidSpecifier, SpecifierSet
  10. from .utils import canonicalize_name
  11. if TYPE_CHECKING:
  12. from collections.abc import Iterator
  13. __all__ = [
  14. "InvalidRequirement",
  15. "Requirement",
  16. ]
  17. def __dir__() -> list[str]:
  18. return __all__
  19. class InvalidRequirement(ValueError):
  20. """
  21. An invalid requirement was found, users should refer to PEP 508.
  22. .. versionadded:: 16.1
  23. """
  24. class Requirement:
  25. """Parse a requirement.
  26. Parse a given requirement string into its parts, such as name, specifier,
  27. URL, and extras. Raises InvalidRequirement on a badly-formed requirement
  28. string.
  29. .. versionadded:: 16.1
  30. .. versionchanged:: 22.0
  31. Added equality (``__eq__``) and hashing (``__hash__``) so requirements
  32. can be compared and stored in sets / dicts.
  33. .. versionchanged:: 23.2
  34. Equality and hashing began canonicalizing requirement names, so
  35. requirements whose names differ only by normalization (e.g.
  36. ``Requirement("Foo")`` vs ``Requirement("foo")``) now compare and hash
  37. equal.
  38. Instances are safe to serialize with :mod:`pickle`. They use a stable
  39. format so the same pickle can be loaded in future packaging releases.
  40. .. versionchanged:: 26.2
  41. Added a stable pickle format. Pickles created with packaging 26.2+ can
  42. be unpickled with future releases. Backward compatibility with pickles
  43. from packaging < 26.2 is supported but may be removed in a future
  44. release.
  45. .. versionchanged:: 26.3
  46. The dedicated pickle support introduced in 26.2 did not preserve the
  47. specifier's explicit :attr:`~packaging.specifiers.SpecifierSet.prereleases`
  48. override; it is now included again.
  49. Equality and hashing normalize requirement names, extras, and
  50. equivalent specifiers. The string representation still preserves the
  51. parsed name and extras spelling.
  52. """
  53. # TODO: Can we test whether something is contained within a requirement?
  54. # If so how do we do that? Do we need to test against the _name_ of
  55. # the thing as well as the version? What about the markers?
  56. # TODO: Can we normalize the name and extra name?
  57. __slots__ = ("extras", "marker", "name", "specifier", "url")
  58. def __init__(self, requirement_string: str) -> None:
  59. try:
  60. parsed = _parse_requirement(requirement_string)
  61. except ParserSyntaxError as e:
  62. raise InvalidRequirement(str(e)) from e
  63. self.name: str = parsed.name
  64. self.url: str | None = parsed.url or None
  65. self.extras: set[str] = set(parsed.extras)
  66. try:
  67. self.specifier: SpecifierSet = SpecifierSet(parsed.specifier)
  68. except InvalidSpecifier as e:
  69. raise InvalidRequirement(str(e)) from e
  70. self.marker: Marker | None = None
  71. if parsed.marker is not None:
  72. self.marker = Marker.__new__(Marker)
  73. self.marker._markers = _normalize_extra_values(parsed.marker)
  74. def _iter_parts(self, name: str) -> Iterator[str]:
  75. yield name
  76. if self.extras:
  77. formatted_extras = ",".join(sorted(self.extras))
  78. yield f"[{formatted_extras}]"
  79. if self.specifier:
  80. yield str(self.specifier)
  81. if self.url:
  82. yield f" @ {self.url}"
  83. if self.marker:
  84. yield " "
  85. if self.marker:
  86. yield f"; {self.marker}"
  87. def __getstate__(self) -> tuple[str, bool | None]:
  88. # Return the requirement string for compactness and stability, paired
  89. # with the specifier's explicit prereleases override, which is not
  90. # captured by the string form. Re-parsed on load to reconstruct all
  91. # other fields.
  92. return (str(self), self.specifier._prereleases)
  93. def __setstate__(self, state: object) -> None:
  94. if isinstance(state, str):
  95. # Format (26.2): just the requirement string.
  96. requirement_string: str = state
  97. prereleases: bool | None = None
  98. elif (
  99. isinstance(state, tuple)
  100. and len(state) == 2
  101. and isinstance(state[0], str)
  102. and (state[1] is None or isinstance(state[1], bool))
  103. ):
  104. # New format (26.3+): (requirement string, specifier prereleases).
  105. requirement_string, prereleases = state
  106. elif isinstance(state, dict) and state.keys() >= set(self.__slots__):
  107. # Old format (packaging <= 26.1, no __slots__): plain __dict__.
  108. for key in self.__slots__:
  109. setattr(self, key, state[key])
  110. return
  111. else:
  112. raise TypeError(f"Cannot restore Requirement from {state!r}")
  113. try:
  114. tmp = Requirement(requirement_string)
  115. except InvalidRequirement as exc:
  116. raise TypeError(f"Cannot restore Requirement from {state!r}") from exc
  117. self.name = tmp.name
  118. self.url = tmp.url
  119. self.extras = tmp.extras
  120. self.specifier = tmp.specifier
  121. self.specifier._prereleases = prereleases
  122. self.marker = tmp.marker
  123. def __str__(self) -> str:
  124. return "".join(self._iter_parts(self.name))
  125. def __repr__(self) -> str:
  126. return f"<{self.__class__.__name__}({str(self)!r})>"
  127. def __hash__(self) -> int:
  128. # Mirror __eq__ by hashing the canonical specifier object rather than
  129. # its raw string. ``_iter_parts`` yields ``str(self.specifier)``, which
  130. # is non-canonical, so trailing-zero-equivalent requirements such as
  131. # ``foo==1.0.0`` and ``foo==1.0.0.0`` (which compare equal) would
  132. # otherwise hash differently, breaking the hash/__eq__ invariant.
  133. return hash(
  134. (
  135. canonicalize_name(self.name),
  136. frozenset(canonicalize_name(e) for e in self.extras),
  137. self.specifier,
  138. self.url,
  139. self.marker,
  140. )
  141. )
  142. def __eq__(self, other: object) -> bool:
  143. if not isinstance(other, Requirement):
  144. return NotImplemented
  145. # Extras must be normalized before comparison as per PEP 685.
  146. self_extras = frozenset(canonicalize_name(e) for e in self.extras)
  147. other_extras = frozenset(canonicalize_name(e) for e in other.extras)
  148. return (
  149. canonicalize_name(self.name) == canonicalize_name(other.name)
  150. and self_extras == other_extras
  151. and self.specifier == other.specifier
  152. and self.url == other.url
  153. and self.marker == other.marker
  154. )