specifiers.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. """
  5. .. testsetup::
  6. from packaging.specifiers import Specifier, SpecifierSet, InvalidSpecifier
  7. from packaging.version import Version
  8. """
  9. from __future__ import annotations
  10. import abc
  11. import itertools
  12. import re
  13. from typing import Callable, Iterable, Iterator, TypeVar, Union
  14. from .utils import canonicalize_version
  15. from .version import Version
  16. UnparsedVersion = Union[Version, str]
  17. UnparsedVersionVar = TypeVar("UnparsedVersionVar", bound=UnparsedVersion)
  18. CallableOperator = Callable[[Version, str], bool]
  19. def _coerce_version(version: UnparsedVersion) -> Version:
  20. if not isinstance(version, Version):
  21. version = Version(version)
  22. return version
  23. class InvalidSpecifier(ValueError):
  24. """
  25. Raised when attempting to create a :class:`Specifier` with a specifier
  26. string that is invalid.
  27. >>> Specifier("lolwat")
  28. Traceback (most recent call last):
  29. ...
  30. packaging.specifiers.InvalidSpecifier: Invalid specifier: 'lolwat'
  31. """
  32. class BaseSpecifier(metaclass=abc.ABCMeta):
  33. @abc.abstractmethod
  34. def __str__(self) -> str:
  35. """
  36. Returns the str representation of this Specifier-like object. This
  37. should be representative of the Specifier itself.
  38. """
  39. @abc.abstractmethod
  40. def __hash__(self) -> int:
  41. """
  42. Returns a hash value for this Specifier-like object.
  43. """
  44. @abc.abstractmethod
  45. def __eq__(self, other: object) -> bool:
  46. """
  47. Returns a boolean representing whether or not the two Specifier-like
  48. objects are equal.
  49. :param other: The other object to check against.
  50. """
  51. @property
  52. @abc.abstractmethod
  53. def prereleases(self) -> bool | None:
  54. """Whether or not pre-releases as a whole are allowed.
  55. This can be set to either ``True`` or ``False`` to explicitly enable or disable
  56. prereleases or it can be set to ``None`` (the default) to use default semantics.
  57. """
  58. @prereleases.setter
  59. def prereleases(self, value: bool) -> None:
  60. """Setter for :attr:`prereleases`.
  61. :param value: The value to set.
  62. """
  63. @abc.abstractmethod
  64. def contains(self, item: str, prereleases: bool | None = None) -> bool:
  65. """
  66. Determines if the given item is contained within this specifier.
  67. """
  68. @abc.abstractmethod
  69. def filter(
  70. self, iterable: Iterable[UnparsedVersionVar], prereleases: bool | None = None
  71. ) -> Iterator[UnparsedVersionVar]:
  72. """
  73. Takes an iterable of items and filters them so that only items which
  74. are contained within this specifier are allowed in it.
  75. """
  76. class Specifier(BaseSpecifier):
  77. """This class abstracts handling of version specifiers.
  78. .. tip::
  79. It is generally not required to instantiate this manually. You should instead
  80. prefer to work with :class:`SpecifierSet` instead, which can parse
  81. comma-separated version specifiers (which is what package metadata contains).
  82. """
  83. _operator_regex_str = r"""
  84. (?P<operator>(~=|==|!=|<=|>=|<|>|===))
  85. """
  86. _version_regex_str = r"""
  87. (?P<version>
  88. (?:
  89. # The identity operators allow for an escape hatch that will
  90. # do an exact string match of the version you wish to install.
  91. # This will not be parsed by PEP 440 and we cannot determine
  92. # any semantic meaning from it. This operator is discouraged
  93. # but included entirely as an escape hatch.
  94. (?<====) # Only match for the identity operator
  95. \s*
  96. [^\s;)]* # The arbitrary version can be just about anything,
  97. # we match everything except for whitespace, a
  98. # semi-colon for marker support, and a closing paren
  99. # since versions can be enclosed in them.
  100. )
  101. |
  102. (?:
  103. # The (non)equality operators allow for wild card and local
  104. # versions to be specified so we have to define these two
  105. # operators separately to enable that.
  106. (?<===|!=) # Only match for equals and not equals
  107. \s*
  108. v?
  109. (?:[0-9]+!)? # epoch
  110. [0-9]+(?:\.[0-9]+)* # release
  111. # You cannot use a wild card and a pre-release, post-release, a dev or
  112. # local version together so group them with a | and make them optional.
  113. (?:
  114. \.\* # Wild card syntax of .*
  115. |
  116. (?: # pre release
  117. [-_\.]?
  118. (alpha|beta|preview|pre|a|b|c|rc)
  119. [-_\.]?
  120. [0-9]*
  121. )?
  122. (?: # post release
  123. (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
  124. )?
  125. (?:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
  126. (?:\+[a-z0-9]+(?:[-_\.][a-z0-9]+)*)? # local
  127. )?
  128. )
  129. |
  130. (?:
  131. # The compatible operator requires at least two digits in the
  132. # release segment.
  133. (?<=~=) # Only match for the compatible operator
  134. \s*
  135. v?
  136. (?:[0-9]+!)? # epoch
  137. [0-9]+(?:\.[0-9]+)+ # release (We have a + instead of a *)
  138. (?: # pre release
  139. [-_\.]?
  140. (alpha|beta|preview|pre|a|b|c|rc)
  141. [-_\.]?
  142. [0-9]*
  143. )?
  144. (?: # post release
  145. (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
  146. )?
  147. (?:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
  148. )
  149. |
  150. (?:
  151. # All other operators only allow a sub set of what the
  152. # (non)equality operators do. Specifically they do not allow
  153. # local versions to be specified nor do they allow the prefix
  154. # matching wild cards.
  155. (?<!==|!=|~=) # We have special cases for these
  156. # operators so we want to make sure they
  157. # don't match here.
  158. \s*
  159. v?
  160. (?:[0-9]+!)? # epoch
  161. [0-9]+(?:\.[0-9]+)* # release
  162. (?: # pre release
  163. [-_\.]?
  164. (alpha|beta|preview|pre|a|b|c|rc)
  165. [-_\.]?
  166. [0-9]*
  167. )?
  168. (?: # post release
  169. (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
  170. )?
  171. (?:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
  172. )
  173. )
  174. """
  175. _regex = re.compile(
  176. r"^\s*" + _operator_regex_str + _version_regex_str + r"\s*$",
  177. re.VERBOSE | re.IGNORECASE,
  178. )
  179. _operators = {
  180. "~=": "compatible",
  181. "==": "equal",
  182. "!=": "not_equal",
  183. "<=": "less_than_equal",
  184. ">=": "greater_than_equal",
  185. "<": "less_than",
  186. ">": "greater_than",
  187. "===": "arbitrary",
  188. }
  189. def __init__(self, spec: str = "", prereleases: bool | None = None) -> None:
  190. """Initialize a Specifier instance.
  191. :param spec:
  192. The string representation of a specifier which will be parsed and
  193. normalized before use.
  194. :param prereleases:
  195. This tells the specifier if it should accept prerelease versions if
  196. applicable or not. The default of ``None`` will autodetect it from the
  197. given specifiers.
  198. :raises InvalidSpecifier:
  199. If the given specifier is invalid (i.e. bad syntax).
  200. """
  201. match = self._regex.search(spec)
  202. if not match:
  203. raise InvalidSpecifier(f"Invalid specifier: {spec!r}")
  204. self._spec: tuple[str, str] = (
  205. match.group("operator").strip(),
  206. match.group("version").strip(),
  207. )
  208. # Store whether or not this Specifier should accept prereleases
  209. self._prereleases = prereleases
  210. # https://github.com/python/mypy/pull/13475#pullrequestreview-1079784515
  211. @property # type: ignore[override]
  212. def prereleases(self) -> bool:
  213. # If there is an explicit prereleases set for this, then we'll just
  214. # blindly use that.
  215. if self._prereleases is not None:
  216. return self._prereleases
  217. # Look at all of our specifiers and determine if they are inclusive
  218. # operators, and if they are if they are including an explicit
  219. # prerelease.
  220. operator, version = self._spec
  221. if operator in ["==", ">=", "<=", "~=", "===", ">", "<"]:
  222. # The == specifier can include a trailing .*, if it does we
  223. # want to remove before parsing.
  224. if operator == "==" and version.endswith(".*"):
  225. version = version[:-2]
  226. # Parse the version, and if it is a pre-release than this
  227. # specifier allows pre-releases.
  228. if Version(version).is_prerelease:
  229. return True
  230. return False
  231. @prereleases.setter
  232. def prereleases(self, value: bool) -> None:
  233. self._prereleases = value
  234. @property
  235. def operator(self) -> str:
  236. """The operator of this specifier.
  237. >>> Specifier("==1.2.3").operator
  238. '=='
  239. """
  240. return self._spec[0]
  241. @property
  242. def version(self) -> str:
  243. """The version of this specifier.
  244. >>> Specifier("==1.2.3").version
  245. '1.2.3'
  246. """
  247. return self._spec[1]
  248. def __repr__(self) -> str:
  249. """A representation of the Specifier that shows all internal state.
  250. >>> Specifier('>=1.0.0')
  251. <Specifier('>=1.0.0')>
  252. >>> Specifier('>=1.0.0', prereleases=False)
  253. <Specifier('>=1.0.0', prereleases=False)>
  254. >>> Specifier('>=1.0.0', prereleases=True)
  255. <Specifier('>=1.0.0', prereleases=True)>
  256. """
  257. pre = (
  258. f", prereleases={self.prereleases!r}"
  259. if self._prereleases is not None
  260. else ""
  261. )
  262. return f"<{self.__class__.__name__}({str(self)!r}{pre})>"
  263. def __str__(self) -> str:
  264. """A string representation of the Specifier that can be round-tripped.
  265. >>> str(Specifier('>=1.0.0'))
  266. '>=1.0.0'
  267. >>> str(Specifier('>=1.0.0', prereleases=False))
  268. '>=1.0.0'
  269. """
  270. return "{}{}".format(*self._spec)
  271. @property
  272. def _canonical_spec(self) -> tuple[str, str]:
  273. canonical_version = canonicalize_version(
  274. self._spec[1],
  275. strip_trailing_zero=(self._spec[0] != "~="),
  276. )
  277. return self._spec[0], canonical_version
  278. def __hash__(self) -> int:
  279. return hash(self._canonical_spec)
  280. def __eq__(self, other: object) -> bool:
  281. """Whether or not the two Specifier-like objects are equal.
  282. :param other: The other object to check against.
  283. The value of :attr:`prereleases` is ignored.
  284. >>> Specifier("==1.2.3") == Specifier("== 1.2.3.0")
  285. True
  286. >>> (Specifier("==1.2.3", prereleases=False) ==
  287. ... Specifier("==1.2.3", prereleases=True))
  288. True
  289. >>> Specifier("==1.2.3") == "==1.2.3"
  290. True
  291. >>> Specifier("==1.2.3") == Specifier("==1.2.4")
  292. False
  293. >>> Specifier("==1.2.3") == Specifier("~=1.2.3")
  294. False
  295. """
  296. if isinstance(other, str):
  297. try:
  298. other = self.__class__(str(other))
  299. except InvalidSpecifier:
  300. return NotImplemented
  301. elif not isinstance(other, self.__class__):
  302. return NotImplemented
  303. return self._canonical_spec == other._canonical_spec
  304. def _get_operator(self, op: str) -> CallableOperator:
  305. operator_callable: CallableOperator = getattr(
  306. self, f"_compare_{self._operators[op]}"
  307. )
  308. return operator_callable
  309. def _compare_compatible(self, prospective: Version, spec: str) -> bool:
  310. # Compatible releases have an equivalent combination of >= and ==. That
  311. # is that ~=2.2 is equivalent to >=2.2,==2.*. This allows us to
  312. # implement this in terms of the other specifiers instead of
  313. # implementing it ourselves. The only thing we need to do is construct
  314. # the other specifiers.
  315. # We want everything but the last item in the version, but we want to
  316. # ignore suffix segments.
  317. prefix = _version_join(
  318. list(itertools.takewhile(_is_not_suffix, _version_split(spec)))[:-1]
  319. )
  320. # Add the prefix notation to the end of our string
  321. prefix += ".*"
  322. return self._get_operator(">=")(prospective, spec) and self._get_operator("==")(
  323. prospective, prefix
  324. )
  325. def _compare_equal(self, prospective: Version, spec: str) -> bool:
  326. # We need special logic to handle prefix matching
  327. if spec.endswith(".*"):
  328. # In the case of prefix matching we want to ignore local segment.
  329. normalized_prospective = canonicalize_version(
  330. prospective.public, strip_trailing_zero=False
  331. )
  332. # Get the normalized version string ignoring the trailing .*
  333. normalized_spec = canonicalize_version(spec[:-2], strip_trailing_zero=False)
  334. # Split the spec out by bangs and dots, and pretend that there is
  335. # an implicit dot in between a release segment and a pre-release segment.
  336. split_spec = _version_split(normalized_spec)
  337. # Split the prospective version out by bangs and dots, and pretend
  338. # that there is an implicit dot in between a release segment and
  339. # a pre-release segment.
  340. split_prospective = _version_split(normalized_prospective)
  341. # 0-pad the prospective version before shortening it to get the correct
  342. # shortened version.
  343. padded_prospective, _ = _pad_version(split_prospective, split_spec)
  344. # Shorten the prospective version to be the same length as the spec
  345. # so that we can determine if the specifier is a prefix of the
  346. # prospective version or not.
  347. shortened_prospective = padded_prospective[: len(split_spec)]
  348. return shortened_prospective == split_spec
  349. else:
  350. # Convert our spec string into a Version
  351. spec_version = Version(spec)
  352. # If the specifier does not have a local segment, then we want to
  353. # act as if the prospective version also does not have a local
  354. # segment.
  355. if not spec_version.local:
  356. prospective = Version(prospective.public)
  357. return prospective == spec_version
  358. def _compare_not_equal(self, prospective: Version, spec: str) -> bool:
  359. return not self._compare_equal(prospective, spec)
  360. def _compare_less_than_equal(self, prospective: Version, spec: str) -> bool:
  361. # NB: Local version identifiers are NOT permitted in the version
  362. # specifier, so local version labels can be universally removed from
  363. # the prospective version.
  364. return Version(prospective.public) <= Version(spec)
  365. def _compare_greater_than_equal(self, prospective: Version, spec: str) -> bool:
  366. # NB: Local version identifiers are NOT permitted in the version
  367. # specifier, so local version labels can be universally removed from
  368. # the prospective version.
  369. return Version(prospective.public) >= Version(spec)
  370. def _compare_less_than(self, prospective: Version, spec_str: str) -> bool:
  371. # Convert our spec to a Version instance, since we'll want to work with
  372. # it as a version.
  373. spec = Version(spec_str)
  374. # Check to see if the prospective version is less than the spec
  375. # version. If it's not we can short circuit and just return False now
  376. # instead of doing extra unneeded work.
  377. if not prospective < spec:
  378. return False
  379. # This special case is here so that, unless the specifier itself
  380. # includes is a pre-release version, that we do not accept pre-release
  381. # versions for the version mentioned in the specifier (e.g. <3.1 should
  382. # not match 3.1.dev0, but should match 3.0.dev0).
  383. if not spec.is_prerelease and prospective.is_prerelease:
  384. if Version(prospective.base_version) == Version(spec.base_version):
  385. return False
  386. # If we've gotten to here, it means that prospective version is both
  387. # less than the spec version *and* it's not a pre-release of the same
  388. # version in the spec.
  389. return True
  390. def _compare_greater_than(self, prospective: Version, spec_str: str) -> bool:
  391. # Convert our spec to a Version instance, since we'll want to work with
  392. # it as a version.
  393. spec = Version(spec_str)
  394. # Check to see if the prospective version is greater than the spec
  395. # version. If it's not we can short circuit and just return False now
  396. # instead of doing extra unneeded work.
  397. if not prospective > spec:
  398. return False
  399. # This special case is here so that, unless the specifier itself
  400. # includes is a post-release version, that we do not accept
  401. # post-release versions for the version mentioned in the specifier
  402. # (e.g. >3.1 should not match 3.0.post0, but should match 3.2.post0).
  403. if not spec.is_postrelease and prospective.is_postrelease:
  404. if Version(prospective.base_version) == Version(spec.base_version):
  405. return False
  406. # Ensure that we do not allow a local version of the version mentioned
  407. # in the specifier, which is technically greater than, to match.
  408. if prospective.local is not None:
  409. if Version(prospective.base_version) == Version(spec.base_version):
  410. return False
  411. # If we've gotten to here, it means that prospective version is both
  412. # greater than the spec version *and* it's not a pre-release of the
  413. # same version in the spec.
  414. return True
  415. def _compare_arbitrary(self, prospective: Version, spec: str) -> bool:
  416. return str(prospective).lower() == str(spec).lower()
  417. def __contains__(self, item: str | Version) -> bool:
  418. """Return whether or not the item is contained in this specifier.
  419. :param item: The item to check for.
  420. This is used for the ``in`` operator and behaves the same as
  421. :meth:`contains` with no ``prereleases`` argument passed.
  422. >>> "1.2.3" in Specifier(">=1.2.3")
  423. True
  424. >>> Version("1.2.3") in Specifier(">=1.2.3")
  425. True
  426. >>> "1.0.0" in Specifier(">=1.2.3")
  427. False
  428. >>> "1.3.0a1" in Specifier(">=1.2.3")
  429. False
  430. >>> "1.3.0a1" in Specifier(">=1.2.3", prereleases=True)
  431. True
  432. """
  433. return self.contains(item)
  434. def contains(self, item: UnparsedVersion, prereleases: bool | None = None) -> bool:
  435. """Return whether or not the item is contained in this specifier.
  436. :param item:
  437. The item to check for, which can be a version string or a
  438. :class:`Version` instance.
  439. :param prereleases:
  440. Whether or not to match prereleases with this Specifier. If set to
  441. ``None`` (the default), it uses :attr:`prereleases` to determine
  442. whether or not prereleases are allowed.
  443. >>> Specifier(">=1.2.3").contains("1.2.3")
  444. True
  445. >>> Specifier(">=1.2.3").contains(Version("1.2.3"))
  446. True
  447. >>> Specifier(">=1.2.3").contains("1.0.0")
  448. False
  449. >>> Specifier(">=1.2.3").contains("1.3.0a1")
  450. False
  451. >>> Specifier(">=1.2.3", prereleases=True).contains("1.3.0a1")
  452. True
  453. >>> Specifier(">=1.2.3").contains("1.3.0a1", prereleases=True)
  454. True
  455. """
  456. # Determine if prereleases are to be allowed or not.
  457. if prereleases is None:
  458. prereleases = self.prereleases
  459. # Normalize item to a Version, this allows us to have a shortcut for
  460. # "2.0" in Specifier(">=2")
  461. normalized_item = _coerce_version(item)
  462. # Determine if we should be supporting prereleases in this specifier
  463. # or not, if we do not support prereleases than we can short circuit
  464. # logic if this version is a prereleases.
  465. if normalized_item.is_prerelease and not prereleases:
  466. return False
  467. # Actually do the comparison to determine if this item is contained
  468. # within this Specifier or not.
  469. operator_callable: CallableOperator = self._get_operator(self.operator)
  470. return operator_callable(normalized_item, self.version)
  471. def filter(
  472. self, iterable: Iterable[UnparsedVersionVar], prereleases: bool | None = None
  473. ) -> Iterator[UnparsedVersionVar]:
  474. """Filter items in the given iterable, that match the specifier.
  475. :param iterable:
  476. An iterable that can contain version strings and :class:`Version` instances.
  477. The items in the iterable will be filtered according to the specifier.
  478. :param prereleases:
  479. Whether or not to allow prereleases in the returned iterator. If set to
  480. ``None`` (the default), it will be intelligently decide whether to allow
  481. prereleases or not (based on the :attr:`prereleases` attribute, and
  482. whether the only versions matching are prereleases).
  483. This method is smarter than just ``filter(Specifier().contains, [...])``
  484. because it implements the rule from :pep:`440` that a prerelease item
  485. SHOULD be accepted if no other versions match the given specifier.
  486. >>> list(Specifier(">=1.2.3").filter(["1.2", "1.3", "1.5a1"]))
  487. ['1.3']
  488. >>> list(Specifier(">=1.2.3").filter(["1.2", "1.2.3", "1.3", Version("1.4")]))
  489. ['1.2.3', '1.3', <Version('1.4')>]
  490. >>> list(Specifier(">=1.2.3").filter(["1.2", "1.5a1"]))
  491. ['1.5a1']
  492. >>> list(Specifier(">=1.2.3").filter(["1.3", "1.5a1"], prereleases=True))
  493. ['1.3', '1.5a1']
  494. >>> list(Specifier(">=1.2.3", prereleases=True).filter(["1.3", "1.5a1"]))
  495. ['1.3', '1.5a1']
  496. """
  497. yielded = False
  498. found_prereleases = []
  499. kw = {"prereleases": prereleases if prereleases is not None else True}
  500. # Attempt to iterate over all the values in the iterable and if any of
  501. # them match, yield them.
  502. for version in iterable:
  503. parsed_version = _coerce_version(version)
  504. if self.contains(parsed_version, **kw):
  505. # If our version is a prerelease, and we were not set to allow
  506. # prereleases, then we'll store it for later in case nothing
  507. # else matches this specifier.
  508. if parsed_version.is_prerelease and not (
  509. prereleases or self.prereleases
  510. ):
  511. found_prereleases.append(version)
  512. # Either this is not a prerelease, or we should have been
  513. # accepting prereleases from the beginning.
  514. else:
  515. yielded = True
  516. yield version
  517. # Now that we've iterated over everything, determine if we've yielded
  518. # any values, and if we have not and we have any prereleases stored up
  519. # then we will go ahead and yield the prereleases.
  520. if not yielded and found_prereleases:
  521. for version in found_prereleases:
  522. yield version
  523. _prefix_regex = re.compile(r"^([0-9]+)((?:a|b|c|rc)[0-9]+)$")
  524. def _version_split(version: str) -> list[str]:
  525. """Split version into components.
  526. The split components are intended for version comparison. The logic does
  527. not attempt to retain the original version string, so joining the
  528. components back with :func:`_version_join` may not produce the original
  529. version string.
  530. """
  531. result: list[str] = []
  532. epoch, _, rest = version.rpartition("!")
  533. result.append(epoch or "0")
  534. for item in rest.split("."):
  535. match = _prefix_regex.search(item)
  536. if match:
  537. result.extend(match.groups())
  538. else:
  539. result.append(item)
  540. return result
  541. def _version_join(components: list[str]) -> str:
  542. """Join split version components into a version string.
  543. This function assumes the input came from :func:`_version_split`, where the
  544. first component must be the epoch (either empty or numeric), and all other
  545. components numeric.
  546. """
  547. epoch, *rest = components
  548. return f"{epoch}!{'.'.join(rest)}"
  549. def _is_not_suffix(segment: str) -> bool:
  550. return not any(
  551. segment.startswith(prefix) for prefix in ("dev", "a", "b", "rc", "post")
  552. )
  553. def _pad_version(left: list[str], right: list[str]) -> tuple[list[str], list[str]]:
  554. left_split, right_split = [], []
  555. # Get the release segment of our versions
  556. left_split.append(list(itertools.takewhile(lambda x: x.isdigit(), left)))
  557. right_split.append(list(itertools.takewhile(lambda x: x.isdigit(), right)))
  558. # Get the rest of our versions
  559. left_split.append(left[len(left_split[0]) :])
  560. right_split.append(right[len(right_split[0]) :])
  561. # Insert our padding
  562. left_split.insert(1, ["0"] * max(0, len(right_split[0]) - len(left_split[0])))
  563. right_split.insert(1, ["0"] * max(0, len(left_split[0]) - len(right_split[0])))
  564. return (
  565. list(itertools.chain.from_iterable(left_split)),
  566. list(itertools.chain.from_iterable(right_split)),
  567. )
  568. class SpecifierSet(BaseSpecifier):
  569. """This class abstracts handling of a set of version specifiers.
  570. It can be passed a single specifier (``>=3.0``), a comma-separated list of
  571. specifiers (``>=3.0,!=3.1``), or no specifier at all.
  572. """
  573. def __init__(
  574. self,
  575. specifiers: str | Iterable[Specifier] = "",
  576. prereleases: bool | None = None,
  577. ) -> None:
  578. """Initialize a SpecifierSet instance.
  579. :param specifiers:
  580. The string representation of a specifier or a comma-separated list of
  581. specifiers which will be parsed and normalized before use.
  582. May also be an iterable of ``Specifier`` instances, which will be used
  583. as is.
  584. :param prereleases:
  585. This tells the SpecifierSet if it should accept prerelease versions if
  586. applicable or not. The default of ``None`` will autodetect it from the
  587. given specifiers.
  588. :raises InvalidSpecifier:
  589. If the given ``specifiers`` are not parseable than this exception will be
  590. raised.
  591. """
  592. if isinstance(specifiers, str):
  593. # Split on `,` to break each individual specifier into its own item, and
  594. # strip each item to remove leading/trailing whitespace.
  595. split_specifiers = [s.strip() for s in specifiers.split(",") if s.strip()]
  596. # Make each individual specifier a Specifier and save in a frozen set
  597. # for later.
  598. self._specs = frozenset(map(Specifier, split_specifiers))
  599. else:
  600. # Save the supplied specifiers in a frozen set.
  601. self._specs = frozenset(specifiers)
  602. # Store our prereleases value so we can use it later to determine if
  603. # we accept prereleases or not.
  604. self._prereleases = prereleases
  605. @property
  606. def prereleases(self) -> bool | None:
  607. # If we have been given an explicit prerelease modifier, then we'll
  608. # pass that through here.
  609. if self._prereleases is not None:
  610. return self._prereleases
  611. # If we don't have any specifiers, and we don't have a forced value,
  612. # then we'll just return None since we don't know if this should have
  613. # pre-releases or not.
  614. if not self._specs:
  615. return None
  616. # Otherwise we'll see if any of the given specifiers accept
  617. # prereleases, if any of them do we'll return True, otherwise False.
  618. return any(s.prereleases for s in self._specs)
  619. @prereleases.setter
  620. def prereleases(self, value: bool) -> None:
  621. self._prereleases = value
  622. def __repr__(self) -> str:
  623. """A representation of the specifier set that shows all internal state.
  624. Note that the ordering of the individual specifiers within the set may not
  625. match the input string.
  626. >>> SpecifierSet('>=1.0.0,!=2.0.0')
  627. <SpecifierSet('!=2.0.0,>=1.0.0')>
  628. >>> SpecifierSet('>=1.0.0,!=2.0.0', prereleases=False)
  629. <SpecifierSet('!=2.0.0,>=1.0.0', prereleases=False)>
  630. >>> SpecifierSet('>=1.0.0,!=2.0.0', prereleases=True)
  631. <SpecifierSet('!=2.0.0,>=1.0.0', prereleases=True)>
  632. """
  633. pre = (
  634. f", prereleases={self.prereleases!r}"
  635. if self._prereleases is not None
  636. else ""
  637. )
  638. return f"<SpecifierSet({str(self)!r}{pre})>"
  639. def __str__(self) -> str:
  640. """A string representation of the specifier set that can be round-tripped.
  641. Note that the ordering of the individual specifiers within the set may not
  642. match the input string.
  643. >>> str(SpecifierSet(">=1.0.0,!=1.0.1"))
  644. '!=1.0.1,>=1.0.0'
  645. >>> str(SpecifierSet(">=1.0.0,!=1.0.1", prereleases=False))
  646. '!=1.0.1,>=1.0.0'
  647. """
  648. return ",".join(sorted(str(s) for s in self._specs))
  649. def __hash__(self) -> int:
  650. return hash(self._specs)
  651. def __and__(self, other: SpecifierSet | str) -> SpecifierSet:
  652. """Return a SpecifierSet which is a combination of the two sets.
  653. :param other: The other object to combine with.
  654. >>> SpecifierSet(">=1.0.0,!=1.0.1") & '<=2.0.0,!=2.0.1'
  655. <SpecifierSet('!=1.0.1,!=2.0.1,<=2.0.0,>=1.0.0')>
  656. >>> SpecifierSet(">=1.0.0,!=1.0.1") & SpecifierSet('<=2.0.0,!=2.0.1')
  657. <SpecifierSet('!=1.0.1,!=2.0.1,<=2.0.0,>=1.0.0')>
  658. """
  659. if isinstance(other, str):
  660. other = SpecifierSet(other)
  661. elif not isinstance(other, SpecifierSet):
  662. return NotImplemented
  663. specifier = SpecifierSet()
  664. specifier._specs = frozenset(self._specs | other._specs)
  665. if self._prereleases is None and other._prereleases is not None:
  666. specifier._prereleases = other._prereleases
  667. elif self._prereleases is not None and other._prereleases is None:
  668. specifier._prereleases = self._prereleases
  669. elif self._prereleases == other._prereleases:
  670. specifier._prereleases = self._prereleases
  671. else:
  672. raise ValueError(
  673. "Cannot combine SpecifierSets with True and False prerelease "
  674. "overrides."
  675. )
  676. return specifier
  677. def __eq__(self, other: object) -> bool:
  678. """Whether or not the two SpecifierSet-like objects are equal.
  679. :param other: The other object to check against.
  680. The value of :attr:`prereleases` is ignored.
  681. >>> SpecifierSet(">=1.0.0,!=1.0.1") == SpecifierSet(">=1.0.0,!=1.0.1")
  682. True
  683. >>> (SpecifierSet(">=1.0.0,!=1.0.1", prereleases=False) ==
  684. ... SpecifierSet(">=1.0.0,!=1.0.1", prereleases=True))
  685. True
  686. >>> SpecifierSet(">=1.0.0,!=1.0.1") == ">=1.0.0,!=1.0.1"
  687. True
  688. >>> SpecifierSet(">=1.0.0,!=1.0.1") == SpecifierSet(">=1.0.0")
  689. False
  690. >>> SpecifierSet(">=1.0.0,!=1.0.1") == SpecifierSet(">=1.0.0,!=1.0.2")
  691. False
  692. """
  693. if isinstance(other, (str, Specifier)):
  694. other = SpecifierSet(str(other))
  695. elif not isinstance(other, SpecifierSet):
  696. return NotImplemented
  697. return self._specs == other._specs
  698. def __len__(self) -> int:
  699. """Returns the number of specifiers in this specifier set."""
  700. return len(self._specs)
  701. def __iter__(self) -> Iterator[Specifier]:
  702. """
  703. Returns an iterator over all the underlying :class:`Specifier` instances
  704. in this specifier set.
  705. >>> sorted(SpecifierSet(">=1.0.0,!=1.0.1"), key=str)
  706. [<Specifier('!=1.0.1')>, <Specifier('>=1.0.0')>]
  707. """
  708. return iter(self._specs)
  709. def __contains__(self, item: UnparsedVersion) -> bool:
  710. """Return whether or not the item is contained in this specifier.
  711. :param item: The item to check for.
  712. This is used for the ``in`` operator and behaves the same as
  713. :meth:`contains` with no ``prereleases`` argument passed.
  714. >>> "1.2.3" in SpecifierSet(">=1.0.0,!=1.0.1")
  715. True
  716. >>> Version("1.2.3") in SpecifierSet(">=1.0.0,!=1.0.1")
  717. True
  718. >>> "1.0.1" in SpecifierSet(">=1.0.0,!=1.0.1")
  719. False
  720. >>> "1.3.0a1" in SpecifierSet(">=1.0.0,!=1.0.1")
  721. False
  722. >>> "1.3.0a1" in SpecifierSet(">=1.0.0,!=1.0.1", prereleases=True)
  723. True
  724. """
  725. return self.contains(item)
  726. def contains(
  727. self,
  728. item: UnparsedVersion,
  729. prereleases: bool | None = None,
  730. installed: bool | None = None,
  731. ) -> bool:
  732. """Return whether or not the item is contained in this SpecifierSet.
  733. :param item:
  734. The item to check for, which can be a version string or a
  735. :class:`Version` instance.
  736. :param prereleases:
  737. Whether or not to match prereleases with this SpecifierSet. If set to
  738. ``None`` (the default), it uses :attr:`prereleases` to determine
  739. whether or not prereleases are allowed.
  740. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.2.3")
  741. True
  742. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains(Version("1.2.3"))
  743. True
  744. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.0.1")
  745. False
  746. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.3.0a1")
  747. False
  748. >>> SpecifierSet(">=1.0.0,!=1.0.1", prereleases=True).contains("1.3.0a1")
  749. True
  750. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.3.0a1", prereleases=True)
  751. True
  752. """
  753. # Ensure that our item is a Version instance.
  754. if not isinstance(item, Version):
  755. item = Version(item)
  756. # Determine if we're forcing a prerelease or not, if we're not forcing
  757. # one for this particular filter call, then we'll use whatever the
  758. # SpecifierSet thinks for whether or not we should support prereleases.
  759. if prereleases is None:
  760. prereleases = self.prereleases
  761. # We can determine if we're going to allow pre-releases by looking to
  762. # see if any of the underlying items supports them. If none of them do
  763. # and this item is a pre-release then we do not allow it and we can
  764. # short circuit that here.
  765. # Note: This means that 1.0.dev1 would not be contained in something
  766. # like >=1.0.devabc however it would be in >=1.0.debabc,>0.0.dev0
  767. if not prereleases and item.is_prerelease:
  768. return False
  769. if installed and item.is_prerelease:
  770. item = Version(item.base_version)
  771. # We simply dispatch to the underlying specs here to make sure that the
  772. # given version is contained within all of them.
  773. # Note: This use of all() here means that an empty set of specifiers
  774. # will always return True, this is an explicit design decision.
  775. return all(s.contains(item, prereleases=prereleases) for s in self._specs)
  776. def filter(
  777. self, iterable: Iterable[UnparsedVersionVar], prereleases: bool | None = None
  778. ) -> Iterator[UnparsedVersionVar]:
  779. """Filter items in the given iterable, that match the specifiers in this set.
  780. :param iterable:
  781. An iterable that can contain version strings and :class:`Version` instances.
  782. The items in the iterable will be filtered according to the specifier.
  783. :param prereleases:
  784. Whether or not to allow prereleases in the returned iterator. If set to
  785. ``None`` (the default), it will be intelligently decide whether to allow
  786. prereleases or not (based on the :attr:`prereleases` attribute, and
  787. whether the only versions matching are prereleases).
  788. This method is smarter than just ``filter(SpecifierSet(...).contains, [...])``
  789. because it implements the rule from :pep:`440` that a prerelease item
  790. SHOULD be accepted if no other versions match the given specifier.
  791. >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.3", "1.5a1"]))
  792. ['1.3']
  793. >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.3", Version("1.4")]))
  794. ['1.3', <Version('1.4')>]
  795. >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.5a1"]))
  796. []
  797. >>> list(SpecifierSet(">=1.2.3").filter(["1.3", "1.5a1"], prereleases=True))
  798. ['1.3', '1.5a1']
  799. >>> list(SpecifierSet(">=1.2.3", prereleases=True).filter(["1.3", "1.5a1"]))
  800. ['1.3', '1.5a1']
  801. An "empty" SpecifierSet will filter items based on the presence of prerelease
  802. versions in the set.
  803. >>> list(SpecifierSet("").filter(["1.3", "1.5a1"]))
  804. ['1.3']
  805. >>> list(SpecifierSet("").filter(["1.5a1"]))
  806. ['1.5a1']
  807. >>> list(SpecifierSet("", prereleases=True).filter(["1.3", "1.5a1"]))
  808. ['1.3', '1.5a1']
  809. >>> list(SpecifierSet("").filter(["1.3", "1.5a1"], prereleases=True))
  810. ['1.3', '1.5a1']
  811. """
  812. # Determine if we're forcing a prerelease or not, if we're not forcing
  813. # one for this particular filter call, then we'll use whatever the
  814. # SpecifierSet thinks for whether or not we should support prereleases.
  815. if prereleases is None:
  816. prereleases = self.prereleases
  817. # If we have any specifiers, then we want to wrap our iterable in the
  818. # filter method for each one, this will act as a logical AND amongst
  819. # each specifier.
  820. if self._specs:
  821. for spec in self._specs:
  822. iterable = spec.filter(iterable, prereleases=bool(prereleases))
  823. return iter(iterable)
  824. # If we do not have any specifiers, then we need to have a rough filter
  825. # which will filter out any pre-releases, unless there are no final
  826. # releases.
  827. else:
  828. filtered: list[UnparsedVersionVar] = []
  829. found_prereleases: list[UnparsedVersionVar] = []
  830. for item in iterable:
  831. parsed_version = _coerce_version(item)
  832. # Store any item which is a pre-release for later unless we've
  833. # already found a final version or we are accepting prereleases
  834. if parsed_version.is_prerelease and not prereleases:
  835. if not filtered:
  836. found_prereleases.append(item)
  837. else:
  838. filtered.append(item)
  839. # If we've found no items except for pre-releases, then we'll go
  840. # ahead and use the pre-releases
  841. if not filtered and found_prereleases and prereleases is None:
  842. return iter(found_prereleases)
  843. return iter(filtered)