specifiers.py 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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 re
  12. import typing
  13. from typing import (
  14. TYPE_CHECKING,
  15. Any,
  16. Callable,
  17. Final,
  18. TypeVar,
  19. Union,
  20. )
  21. from ._ranges import (
  22. FULL_RANGE,
  23. bounds_for_spec,
  24. coerce_version,
  25. filter_by_ranges,
  26. intersect_specifier_bounds,
  27. matches_bounds_only,
  28. ranges_are_prerelease_only,
  29. resolve_prereleases,
  30. trim_release,
  31. )
  32. from .utils import canonicalize_version
  33. from .version import Version
  34. if TYPE_CHECKING:
  35. import sys
  36. from collections.abc import Iterable, Iterator, Sequence
  37. if sys.version_info >= (3, 10):
  38. from typing import TypeGuard
  39. else:
  40. from typing_extensions import TypeGuard
  41. from . import ranges
  42. from ._ranges import Interval
  43. __all__ = [
  44. "BaseSpecifier",
  45. "InvalidSpecifier",
  46. "Specifier",
  47. "SpecifierSet",
  48. ]
  49. def __dir__() -> list[str]:
  50. return __all__
  51. def _validate_spec(spec: object, /) -> TypeGuard[tuple[str, str]]:
  52. return (
  53. isinstance(spec, tuple)
  54. and len(spec) == 2
  55. and isinstance(spec[0], str)
  56. and isinstance(spec[1], str)
  57. )
  58. def _validate_pre(pre: object, /) -> TypeGuard[bool | None]:
  59. return pre is None or isinstance(pre, bool)
  60. T = TypeVar("T")
  61. UnparsedVersion = Union[Version, str]
  62. UnparsedVersionVar = TypeVar("UnparsedVersionVar", bound=UnparsedVersion)
  63. # Operators whose result is just a direct Version comparison, given a parsed
  64. # item with no local. ``<=``/``==``/``!=`` need that no-local guard because
  65. # PEP 440 strips locals on those; ``>=`` works regardless.
  66. _DIRECT_COMPARE_OPS: dict[str, Callable[[Version, Version], bool]] = {
  67. ">=": Version.__ge__,
  68. "<=": Version.__le__,
  69. "==": Version.__eq__,
  70. "!=": Version.__ne__,
  71. }
  72. def _fast_match(specifier: Specifier, parsed: Version) -> bool | None:
  73. """Match ``parsed`` against ``specifier`` without building a range.
  74. Handles ``>=``, ``<=``, ``==``, ``!=``, ``<``, ``>`` when the spec is
  75. not a wildcard and ``parsed`` has no local. Returns ``None`` when the
  76. range path must be used. Pre-release policy is left to the caller.
  77. """
  78. op_str, ver_str = specifier._spec
  79. if ver_str.endswith(".*") or parsed.local is not None:
  80. return None
  81. direct_compare = _DIRECT_COMPARE_OPS.get(op_str)
  82. if direct_compare is not None:
  83. return direct_compare(parsed, specifier._require_spec_version(ver_str))
  84. if op_str in ("<", ">"):
  85. spec_v = specifier._require_spec_version(ver_str)
  86. # ``<V``/``>V`` carve out V's family (pre/dev/post); that only
  87. # matters when parsed shares V's epoch and trimmed release.
  88. # Otherwise a direct cmpkey comparison is correct.
  89. if parsed.epoch != spec_v.epoch or trim_release(parsed.release) != trim_release(
  90. spec_v.release
  91. ):
  92. return parsed < spec_v if op_str == "<" else parsed > spec_v
  93. return None
  94. return None
  95. class InvalidSpecifier(ValueError):
  96. """
  97. Raised when attempting to create a :class:`Specifier` with a specifier
  98. string that is invalid.
  99. >>> Specifier("lolwat")
  100. Traceback (most recent call last):
  101. ...
  102. packaging.specifiers.InvalidSpecifier: Invalid specifier: 'lolwat'
  103. """
  104. class BaseSpecifier(metaclass=abc.ABCMeta):
  105. """
  106. Abstract base class for :class:`Specifier` and :class:`SpecifierSet`.
  107. """
  108. __slots__ = ()
  109. __match_args__ = ("_str",)
  110. @property
  111. def _str(self) -> str:
  112. """Internal property for match_args"""
  113. return str(self)
  114. @abc.abstractmethod
  115. def __str__(self) -> str:
  116. """
  117. Returns the str representation of this Specifier-like object. This
  118. should be representative of the Specifier itself.
  119. """
  120. @abc.abstractmethod
  121. def __hash__(self) -> int:
  122. """
  123. Returns a hash value for this Specifier-like object.
  124. """
  125. @abc.abstractmethod
  126. def __eq__(self, other: object) -> bool:
  127. """
  128. Returns a boolean representing whether or not the two Specifier-like
  129. objects are equal.
  130. :param other: The other object to check against.
  131. """
  132. @property
  133. @abc.abstractmethod
  134. def prereleases(self) -> bool | None:
  135. """Whether or not pre-releases as a whole are allowed.
  136. This can be set to either ``True`` or ``False`` to explicitly enable or disable
  137. prereleases or it can be set to ``None`` (the default) to use default semantics.
  138. """
  139. @prereleases.setter # noqa: B027
  140. def prereleases(self, value: bool) -> None:
  141. """Setter for :attr:`prereleases`.
  142. :param value: The value to set.
  143. """
  144. @abc.abstractmethod
  145. def contains(self, item: str, prereleases: bool | None = None) -> bool:
  146. """
  147. Determines if the given item is contained within this specifier.
  148. """
  149. @typing.overload
  150. def filter(
  151. self,
  152. iterable: Iterable[UnparsedVersionVar],
  153. prereleases: bool | None = None,
  154. key: None = ...,
  155. ) -> Iterator[UnparsedVersionVar]: ...
  156. @typing.overload
  157. def filter(
  158. self,
  159. iterable: Iterable[T],
  160. prereleases: bool | None = None,
  161. key: Callable[[T], UnparsedVersion] = ...,
  162. ) -> Iterator[T]: ...
  163. @abc.abstractmethod
  164. def filter(
  165. self,
  166. iterable: Iterable[Any],
  167. prereleases: bool | None = None,
  168. key: Callable[[Any], UnparsedVersion] | None = None,
  169. ) -> Iterator[Any]:
  170. """
  171. Takes an iterable of items and filters them so that only items which
  172. are contained within this specifier are allowed in it.
  173. """
  174. class Specifier(BaseSpecifier):
  175. """This class abstracts handling of version specifiers.
  176. .. tip::
  177. It is generally not required to instantiate this manually. You should instead
  178. prefer to work with :class:`SpecifierSet` instead, which can parse
  179. comma-separated version specifiers (which is what package metadata contains).
  180. Instances are safe to serialize with :mod:`pickle`. They use a stable
  181. format so the same pickle can be loaded in future packaging releases.
  182. .. versionchanged:: 26.2
  183. Added a stable pickle format. Pickles created with packaging 26.2+ can
  184. be unpickled with future releases. Backward compatibility with pickles
  185. from packaging < 26.2 is supported but may be removed in a future
  186. release.
  187. """
  188. __slots__ = (
  189. "_prereleases",
  190. "_ranges",
  191. "_spec",
  192. "_spec_version",
  193. )
  194. _specifier_regex_str = r"""
  195. (?:
  196. (?:
  197. # The identity operators allow for an escape hatch that will
  198. # do an exact string match of the version you wish to install.
  199. # This will not be parsed by PEP 440 and we cannot determine
  200. # any semantic meaning from it. This operator is discouraged
  201. # but included entirely as an escape hatch.
  202. === # Only match for the identity operator
  203. \s*
  204. [^\s;)]* # The arbitrary version can be just about anything,
  205. # we match everything except for whitespace, a
  206. # semi-colon for marker support, and a closing paren
  207. # since versions can be enclosed in them.
  208. )
  209. |
  210. (?:
  211. # The (non)equality operators allow for wild card and local
  212. # versions to be specified so we have to define these two
  213. # operators separately to enable that.
  214. (?:==|!=) # Only match for equals and not equals
  215. \s*
  216. v?
  217. (?:[0-9]+!)? # epoch
  218. [0-9]+(?:\.[0-9]+)* # release
  219. # You cannot use a wild card and a pre-release, post-release, a dev or
  220. # local version together so group them with a | and make them optional.
  221. (?:
  222. \.\* # Wild card syntax of .*
  223. |
  224. (?a: # pre release
  225. [-_\.]?
  226. (alpha|beta|preview|pre|a|b|c|rc)
  227. [-_\.]?
  228. [0-9]*
  229. )?
  230. (?a: # post release
  231. (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
  232. )?
  233. (?a:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
  234. (?a:\+[a-z0-9]+(?:[-_\.][a-z0-9]+)*)? # local
  235. )?
  236. )
  237. |
  238. (?:
  239. # The compatible operator requires at least two digits in the
  240. # release segment.
  241. (?:~=) # Only match for the compatible operator
  242. \s*
  243. v?
  244. (?:[0-9]+!)? # epoch
  245. [0-9]+(?:\.[0-9]+)+ # release (We have a + instead of a *)
  246. (?: # pre release
  247. [-_\.]?
  248. (alpha|beta|preview|pre|a|b|c|rc)
  249. [-_\.]?
  250. [0-9]*
  251. )?
  252. (?: # post release
  253. (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
  254. )?
  255. (?:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
  256. )
  257. |
  258. (?:
  259. # All other operators only allow a sub set of what the
  260. # (non)equality operators do. Specifically they do not allow
  261. # local versions to be specified nor do they allow the prefix
  262. # matching wild cards.
  263. (?:<=|>=|<|>)
  264. \s*
  265. v?
  266. (?:[0-9]+!)? # epoch
  267. [0-9]+(?:\.[0-9]+)* # release
  268. (?a: # pre release
  269. [-_\.]?
  270. (alpha|beta|preview|pre|a|b|c|rc)
  271. [-_\.]?
  272. [0-9]*
  273. )?
  274. (?a: # post release
  275. (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
  276. )?
  277. (?a:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
  278. )
  279. )
  280. """
  281. _regex = re.compile(
  282. r"\s*" + _specifier_regex_str + r"\s*", re.VERBOSE | re.IGNORECASE
  283. )
  284. # Legacy unused attribute, kept for backward compatibility
  285. _operators: Final = {
  286. "~=": "compatible",
  287. "==": "equal",
  288. "!=": "not_equal",
  289. "<=": "less_than_equal",
  290. ">=": "greater_than_equal",
  291. "<": "less_than",
  292. ">": "greater_than",
  293. "===": "arbitrary",
  294. }
  295. def __init__(self, spec: str = "", prereleases: bool | None = None) -> None:
  296. """Initialize a Specifier instance.
  297. :param spec:
  298. The string representation of a specifier which will be parsed and
  299. normalized before use.
  300. :param prereleases:
  301. This tells the specifier if it should accept prerelease versions if
  302. applicable or not. The default of ``None`` will autodetect it from the
  303. given specifiers.
  304. :raises InvalidSpecifier:
  305. If the given specifier is invalid (i.e. bad syntax).
  306. """
  307. if not self._regex.fullmatch(spec):
  308. raise InvalidSpecifier(f"Invalid specifier: {spec!r}")
  309. spec = spec.strip()
  310. if spec.startswith("==="):
  311. operator, version = spec[:3], spec[3:].strip()
  312. elif spec.startswith(("~=", "==", "!=", "<=", ">=")):
  313. operator, version = spec[:2], spec[2:].strip()
  314. else:
  315. operator, version = spec[:1], spec[1:].strip()
  316. self._spec: tuple[str, str] = (operator, version)
  317. # Store whether or not this Specifier should accept prereleases
  318. self._prereleases = prereleases
  319. # Specifier version cache
  320. self._spec_version: tuple[str, Version] | None = None
  321. # Version range cache (populated by _to_ranges)
  322. self._ranges: Sequence[Interval] | None = None
  323. def _get_spec_version(self, version: str) -> Version | None:
  324. """One element cache, as only one spec Version is needed per Specifier."""
  325. if self._spec_version is not None and self._spec_version[0] == version:
  326. return self._spec_version[1]
  327. version_specifier = coerce_version(version)
  328. if version_specifier is None:
  329. return None
  330. self._spec_version = (version, version_specifier)
  331. return version_specifier
  332. def _require_spec_version(self, version: str) -> Version:
  333. """Get spec version, asserting it's valid (not for === operator).
  334. This method should only be called for operators where version
  335. strings are guaranteed to be valid PEP 440 versions (not ===).
  336. """
  337. spec_version = self._get_spec_version(version)
  338. assert spec_version is not None
  339. return spec_version
  340. def _to_ranges(self) -> Sequence[Interval]:
  341. """Convert this specifier to sorted, non-overlapping version ranges.
  342. Each standard operator maps to one or two ranges. ``===`` is
  343. modeled as full range (actual check done separately). Cached.
  344. """
  345. if self._ranges is not None:
  346. return self._ranges
  347. op = self.operator
  348. ver_str = self.version
  349. if op == "===":
  350. result: Sequence[Interval] = FULL_RANGE
  351. else:
  352. version = self._require_spec_version(ver_str.removesuffix(".*"))
  353. result = bounds_for_spec(op, ver_str, version)
  354. self._ranges = result
  355. return result
  356. @property
  357. def prereleases(self) -> bool | None:
  358. # If there is an explicit prereleases set for this, then we'll just
  359. # blindly use that.
  360. if self._prereleases is not None:
  361. return self._prereleases
  362. # Only the "!=" operator does not imply prereleases when
  363. # the version in the specifier is a prerelease.
  364. operator, version_str = self._spec
  365. if operator == "!=":
  366. return False
  367. # The == specifier with trailing .* cannot include prereleases
  368. # e.g. "==1.0a1.*" is not valid.
  369. if operator == "==" and version_str.endswith(".*"):
  370. return False
  371. # "===" can have arbitrary string versions, so we cannot parse
  372. # those, we take prereleases as unknown (None) for those.
  373. version = self._get_spec_version(version_str)
  374. if version is None:
  375. return None
  376. # For all other operators, use the check if spec Version
  377. # object implies pre-releases.
  378. return version.is_prerelease
  379. @prereleases.setter
  380. def prereleases(self, value: bool | None) -> None:
  381. self._prereleases = value
  382. def __getstate__(self) -> tuple[tuple[str, str], bool | None]:
  383. # Return state as a 2-item tuple for compactness:
  384. # ((operator, version), prereleases)
  385. # Cache members are excluded and will be recomputed on demand.
  386. return (self._spec, self._prereleases)
  387. def __setstate__(self, state: object) -> None:
  388. # Always discard cached values - they will be recomputed on demand.
  389. self._spec_version = None
  390. self._ranges = None
  391. if isinstance(state, tuple):
  392. if len(state) == 2:
  393. # New format (26.2+): ((operator, version), prereleases)
  394. spec, prereleases = state
  395. if _validate_spec(spec) and _validate_pre(prereleases):
  396. self._spec = spec
  397. self._prereleases = prereleases
  398. return
  399. if len(state) == 2 and isinstance(state[1], dict):
  400. # Format (packaging 26.0-26.1): (None, {slot: value}).
  401. _, slot_dict = state
  402. spec = slot_dict.get("_spec")
  403. prereleases = slot_dict.get("_prereleases", "invalid")
  404. if _validate_spec(spec) and _validate_pre(prereleases):
  405. self._spec = spec
  406. self._prereleases = prereleases
  407. return
  408. if isinstance(state, dict):
  409. # Old format (packaging <= 25.x, no __slots__): state is a plain dict.
  410. spec = state.get("_spec")
  411. prereleases = state.get("_prereleases", "invalid")
  412. if _validate_spec(spec) and _validate_pre(prereleases):
  413. self._spec = spec
  414. self._prereleases = prereleases
  415. return
  416. raise TypeError(f"Cannot restore Specifier from {state!r}")
  417. @property
  418. def operator(self) -> str:
  419. """The operator of this specifier.
  420. >>> Specifier("==1.2.3").operator
  421. '=='
  422. """
  423. return self._spec[0]
  424. @property
  425. def version(self) -> str:
  426. """The version of this specifier.
  427. >>> Specifier("==1.2.3").version
  428. '1.2.3'
  429. """
  430. return self._spec[1]
  431. def __repr__(self) -> str:
  432. """A representation of the Specifier that shows all internal state.
  433. >>> Specifier('>=1.0.0')
  434. <Specifier('>=1.0.0')>
  435. >>> Specifier('>=1.0.0', prereleases=False)
  436. <Specifier('>=1.0.0', prereleases=False)>
  437. >>> Specifier('>=1.0.0', prereleases=True)
  438. <Specifier('>=1.0.0', prereleases=True)>
  439. """
  440. pre = (
  441. f", prereleases={self.prereleases!r}"
  442. if self._prereleases is not None
  443. else ""
  444. )
  445. return f"<{self.__class__.__name__}({str(self)!r}{pre})>"
  446. def __str__(self) -> str:
  447. """A string representation of the Specifier that can be round-tripped.
  448. >>> str(Specifier('>=1.0.0'))
  449. '>=1.0.0'
  450. >>> str(Specifier('>=1.0.0', prereleases=False))
  451. '>=1.0.0'
  452. """
  453. return "{}{}".format(*self._spec)
  454. @property
  455. def _canonical_spec(self) -> tuple[str, str]:
  456. operator, version = self._spec
  457. if operator == "===" or version.endswith(".*"):
  458. return operator, version
  459. spec_version = self._require_spec_version(version)
  460. canonical_version = canonicalize_version(
  461. spec_version, strip_trailing_zero=(operator != "~=")
  462. )
  463. return operator, canonical_version
  464. def __hash__(self) -> int:
  465. return hash(self._canonical_spec)
  466. def __eq__(self, other: object) -> bool:
  467. """Whether or not the two Specifier-like objects are equal.
  468. :param other: The other object to check against.
  469. The value of :attr:`prereleases` is ignored.
  470. >>> Specifier("==1.2.3") == Specifier("== 1.2.3.0")
  471. True
  472. >>> (Specifier("==1.2.3", prereleases=False) ==
  473. ... Specifier("==1.2.3", prereleases=True))
  474. True
  475. >>> Specifier("==1.2.3") == "==1.2.3"
  476. True
  477. >>> Specifier("==1.2.3") == Specifier("==1.2.4")
  478. False
  479. >>> Specifier("==1.2.3") == Specifier("~=1.2.3")
  480. False
  481. """
  482. if isinstance(other, str):
  483. try:
  484. other = self.__class__(str(other))
  485. except InvalidSpecifier:
  486. return NotImplemented
  487. elif not isinstance(other, self.__class__):
  488. return NotImplemented
  489. return self._canonical_spec == other._canonical_spec
  490. def __contains__(self, item: str | Version) -> bool:
  491. """Return whether or not the item is contained in this specifier.
  492. :param item: The item to check for.
  493. This is used for the ``in`` operator and behaves the same as
  494. :meth:`contains` with no ``prereleases`` argument passed.
  495. >>> "1.2.3" in Specifier(">=1.2.3")
  496. True
  497. >>> Version("1.2.3") in Specifier(">=1.2.3")
  498. True
  499. >>> "1.0.0" in Specifier(">=1.2.3")
  500. False
  501. >>> "1.3.0a1" in Specifier(">=1.2.3")
  502. True
  503. >>> "1.3.0a1" in Specifier(">=1.2.3", prereleases=True)
  504. True
  505. """
  506. return self.contains(item)
  507. def contains(self, item: UnparsedVersion, prereleases: bool | None = None) -> bool:
  508. """Return whether or not the item is contained in this specifier.
  509. :param item:
  510. The item to check for, which can be a version string or a
  511. :class:`~packaging.version.Version` instance.
  512. :param prereleases:
  513. Whether or not to match prereleases with this Specifier. If set to
  514. ``None`` (the default), it will follow the recommendation from
  515. :pep:`440` and match prereleases, as there are no other versions.
  516. >>> Specifier(">=1.2.3").contains("1.2.3")
  517. True
  518. >>> Specifier(">=1.2.3").contains(Version("1.2.3"))
  519. True
  520. >>> Specifier(">=1.2.3").contains("1.0.0")
  521. False
  522. >>> Specifier(">=1.2.3").contains("1.3.0a1")
  523. True
  524. >>> Specifier(">=1.2.3", prereleases=False).contains("1.3.0a1")
  525. False
  526. >>> Specifier(">=1.2.3").contains("1.3.0a1")
  527. True
  528. .. versionchanged:: 26.0
  529. With ``prereleases=None``, a prerelease now matches. A single
  530. version has no alternatives, so the :pep:`440` rule to accept
  531. prereleases when nothing else satisfies the specifier applies.
  532. Earlier versions rejected it. An unparsable version now returns
  533. ``False`` instead of raising :exc:`~packaging.version.InvalidVersion`.
  534. """
  535. # ``===`` compares the raw string, so a Version parse here would
  536. # be wasted.
  537. if self._spec[0] == "===":
  538. return bool(list(self.filter([item], prereleases=prereleases)))
  539. parsed = coerce_version(item)
  540. if parsed is None:
  541. # Standard operators never match an unparsable input.
  542. return False
  543. if prereleases is None:
  544. prereleases = resolve_prereleases(self._prereleases, self.prereleases)
  545. if prereleases is False and parsed.is_prerelease:
  546. return False
  547. # ``_fast_match`` answers the simple operators without building a
  548. # range; otherwise fall back to the engine's bounds membership.
  549. match = _fast_match(self, parsed)
  550. if match is not None:
  551. return match
  552. return matches_bounds_only(self._to_ranges(), parsed)
  553. @typing.overload
  554. def filter(
  555. self,
  556. iterable: Iterable[UnparsedVersionVar],
  557. prereleases: bool | None = None,
  558. key: None = ...,
  559. ) -> Iterator[UnparsedVersionVar]: ...
  560. @typing.overload
  561. def filter(
  562. self,
  563. iterable: Iterable[T],
  564. prereleases: bool | None = None,
  565. key: Callable[[T], UnparsedVersion] = ...,
  566. ) -> Iterator[T]: ...
  567. def filter(
  568. self,
  569. iterable: Iterable[Any],
  570. prereleases: bool | None = None,
  571. key: Callable[[Any], UnparsedVersion] | None = None,
  572. ) -> Iterator[Any]:
  573. """Filter items in the given iterable, that match the specifier.
  574. :param iterable:
  575. An iterable that can contain version strings and
  576. :class:`~packaging.version.Version` instances. The items in the
  577. iterable will be filtered according to the specifier.
  578. :param prereleases:
  579. Whether or not to allow prereleases in the returned iterator. If set to
  580. ``None`` (the default), it will follow the recommendation from :pep:`440`
  581. and match prereleases if there are no other versions.
  582. :param key:
  583. A callable that takes a single argument (an item from the iterable) and
  584. returns a version string or :class:`~packaging.version.Version`
  585. instance to be used for filtering.
  586. >>> list(Specifier(">=1.2.3").filter(["1.2", "1.3", "1.5a1"]))
  587. ['1.3']
  588. >>> list(Specifier(">=1.2.3").filter(["1.2", "1.2.3", "1.3", Version("1.4")]))
  589. ['1.2.3', '1.3', <Version('1.4')>]
  590. >>> list(Specifier(">=1.2.3").filter(["1.2", "1.5a1"]))
  591. ['1.5a1']
  592. >>> list(Specifier(">=1.2.3").filter(["1.3", "1.5a1"], prereleases=True))
  593. ['1.3', '1.5a1']
  594. >>> list(Specifier(">=1.2.3", prereleases=True).filter(["1.3", "1.5a1"]))
  595. ['1.3', '1.5a1']
  596. >>> list(Specifier(">=1.2.3").filter(
  597. ... [{"ver": "1.2"}, {"ver": "1.3"}],
  598. ... key=lambda x: x["ver"]))
  599. [{'ver': '1.3'}]
  600. .. versionchanged:: 26.1
  601. Added the ``key`` parameter.
  602. """
  603. if prereleases is None:
  604. prereleases = resolve_prereleases(self._prereleases, self.prereleases)
  605. if self.operator == "===":
  606. spec_lower = self.version.lower()
  607. matches = (
  608. item
  609. for item in iterable
  610. if str(item if key is None else key(item)).lower() == spec_lower
  611. )
  612. return _apply_prereleases_filter(matches, key, prereleases)
  613. return filter_by_ranges(self._to_ranges(), iterable, key, prereleases)
  614. def _apply_prereleases_filter(
  615. matches: Iterable[Any],
  616. key: Callable[[Any], UnparsedVersion] | None,
  617. prereleases: bool | None,
  618. ) -> Iterator[Any]:
  619. """Apply ``prereleases=`` handling to an already-matched iterable.
  620. ``None`` means PEP 440 default (buffer pre-releases until a final
  621. appears); ``True`` yields everything; ``False`` drops pre-releases.
  622. """
  623. if prereleases is None:
  624. return _pep440_filter_prereleases(matches, key)
  625. if prereleases:
  626. return iter(matches)
  627. return (
  628. item
  629. for item in matches
  630. if (parsed := coerce_version(item if key is None else key(item))) is None
  631. or not parsed.is_prerelease
  632. )
  633. class SpecifierSet(BaseSpecifier):
  634. """This class abstracts handling of a set of version specifiers.
  635. It can be passed a single specifier (``>=3.0``), a comma-separated list of
  636. specifiers (``>=3.0,!=3.1``), or no specifier at all.
  637. Instances are safe to serialize with :mod:`pickle`. They use a stable
  638. format so the same pickle can be loaded in future packaging
  639. releases.
  640. .. versionchanged:: 26.2
  641. Added a stable pickle format. Pickles created with
  642. packaging 26.2+ can be unpickled with future releases.
  643. Backward compatibility with pickles from
  644. packaging < 26.2 is supported but may be removed in a future
  645. release.
  646. """
  647. __slots__ = (
  648. "_canonicalized",
  649. "_has_arbitrary",
  650. "_is_unsatisfiable",
  651. "_prereleases",
  652. "_ranges",
  653. "_specs",
  654. )
  655. def __init__(
  656. self,
  657. specifiers: str | Iterable[Specifier] = "",
  658. prereleases: bool | None = None,
  659. ) -> None:
  660. """Initialize a SpecifierSet instance.
  661. :param specifiers:
  662. The string representation of a specifier or a comma-separated list of
  663. specifiers which will be parsed and normalized before use.
  664. May also be an iterable of ``Specifier`` instances, which will be used
  665. as is.
  666. :param prereleases:
  667. This tells the SpecifierSet if it should accept prerelease versions if
  668. applicable or not. The default of ``None`` will autodetect it from the
  669. given specifiers.
  670. :raises InvalidSpecifier:
  671. If the given ``specifiers`` are not parseable than this exception will be
  672. raised.
  673. """
  674. if isinstance(specifiers, str):
  675. # Split on `,` to break each individual specifier into its own item, and
  676. # strip each item to remove leading/trailing whitespace.
  677. split_specifiers = [s.strip() for s in specifiers.split(",") if s.strip()]
  678. self._specs: tuple[Specifier, ...] = tuple(map(Specifier, split_specifiers))
  679. # Fast substring check; avoids iterating parsed specs.
  680. self._has_arbitrary = "===" in specifiers
  681. else:
  682. self._specs = tuple(specifiers)
  683. # Substring check works for both Specifier objects and plain
  684. # strings (setuptools passes lists of strings).
  685. self._has_arbitrary = any("===" in str(s) for s in self._specs)
  686. self._canonicalized = len(self._specs) <= 1
  687. self._is_unsatisfiable: bool | None = None
  688. self._ranges: Sequence[Interval] | None = None
  689. # Store our prereleases value so we can use it later to determine if
  690. # we accept prereleases or not.
  691. self._prereleases = prereleases
  692. def _canonical_specs(self) -> tuple[Specifier, ...]:
  693. """Deduplicate, sort, and cache specs for order-sensitive operations."""
  694. if not self._canonicalized:
  695. self._specs = tuple(dict.fromkeys(sorted(self._specs, key=str)))
  696. self._canonicalized = True
  697. return self._specs
  698. @property
  699. def prereleases(self) -> bool | None:
  700. # If we have been given an explicit prerelease modifier, then we'll
  701. # pass that through here.
  702. if self._prereleases is not None:
  703. return self._prereleases
  704. # If we don't have any specifiers, and we don't have a forced value,
  705. # then we'll just return None since we don't know if this should have
  706. # pre-releases or not.
  707. if not self._specs:
  708. return None
  709. # Otherwise we'll see if any of the given specifiers accept
  710. # prereleases, if any of them do we'll return True, otherwise False.
  711. if any(s.prereleases for s in self._specs):
  712. return True
  713. return None
  714. @prereleases.setter
  715. def prereleases(self, value: bool | None) -> None:
  716. self._prereleases = value
  717. self._is_unsatisfiable = None
  718. def __getstate__(self) -> tuple[tuple[Specifier, ...], bool | None]:
  719. # Return state as a 2-item tuple for compactness:
  720. # (specs, prereleases)
  721. # Cache members are excluded and will be recomputed on demand.
  722. return (self._specs, self._prereleases)
  723. def __setstate__(self, state: object) -> None:
  724. # Always discard cached values - they will be recomputed on demand.
  725. self._ranges = None
  726. self._is_unsatisfiable = None
  727. if isinstance(state, tuple):
  728. if len(state) == 2:
  729. # New format (26.2+): (specs, prereleases)
  730. specs, prereleases = state
  731. if (
  732. isinstance(specs, tuple)
  733. and all(isinstance(s, Specifier) for s in specs)
  734. and _validate_pre(prereleases)
  735. ):
  736. self._specs = specs
  737. self._prereleases = prereleases
  738. self._canonicalized = len(specs) <= 1
  739. self._has_arbitrary = any("===" in str(s) for s in specs)
  740. return
  741. if len(state) == 2 and isinstance(state[1], dict):
  742. # Format (packaging 26.0-26.1): (None, {slot: value}).
  743. _, slot_dict = state
  744. specs = slot_dict.get("_specs", ())
  745. prereleases = slot_dict.get("_prereleases")
  746. # Convert frozenset to tuple (26.0 stored as frozenset)
  747. if isinstance(specs, frozenset):
  748. specs = tuple(sorted(specs, key=str))
  749. if (
  750. isinstance(specs, tuple)
  751. and all(isinstance(s, Specifier) for s in specs)
  752. and _validate_pre(prereleases)
  753. ):
  754. self._specs = specs
  755. self._prereleases = prereleases
  756. self._canonicalized = len(self._specs) <= 1
  757. self._has_arbitrary = any("===" in str(s) for s in self._specs)
  758. return
  759. if isinstance(state, dict):
  760. # Old format (packaging <= 25.x, no __slots__): state is a plain dict.
  761. specs = state.get("_specs", ())
  762. prereleases = state.get("_prereleases")
  763. # Convert frozenset to tuple (26.0 stored as frozenset)
  764. if isinstance(specs, frozenset):
  765. specs = tuple(sorted(specs, key=str))
  766. if (
  767. isinstance(specs, tuple)
  768. and all(isinstance(s, Specifier) for s in specs)
  769. and _validate_pre(prereleases)
  770. ):
  771. self._specs = specs
  772. self._prereleases = prereleases
  773. self._canonicalized = len(self._specs) <= 1
  774. self._has_arbitrary = any("===" in str(s) for s in self._specs)
  775. return
  776. raise TypeError(f"Cannot restore SpecifierSet from {state!r}")
  777. def __repr__(self) -> str:
  778. """A representation of the specifier set that shows all internal state.
  779. Note that the ordering of the individual specifiers within the set may not
  780. match the input string.
  781. >>> SpecifierSet('>=1.0.0,!=2.0.0')
  782. <SpecifierSet('!=2.0.0,>=1.0.0')>
  783. >>> SpecifierSet('>=1.0.0,!=2.0.0', prereleases=False)
  784. <SpecifierSet('!=2.0.0,>=1.0.0', prereleases=False)>
  785. >>> SpecifierSet('>=1.0.0,!=2.0.0', prereleases=True)
  786. <SpecifierSet('!=2.0.0,>=1.0.0', prereleases=True)>
  787. """
  788. pre = (
  789. f", prereleases={self.prereleases!r}"
  790. if self._prereleases is not None
  791. else ""
  792. )
  793. return f"<{self.__class__.__name__}({str(self)!r}{pre})>"
  794. def __str__(self) -> str:
  795. """A string representation of the specifier set that can be round-tripped.
  796. Note that the ordering of the individual specifiers within the set may not
  797. match the input string.
  798. >>> str(SpecifierSet(">=1.0.0,!=1.0.1"))
  799. '!=1.0.1,>=1.0.0'
  800. >>> str(SpecifierSet(">=1.0.0,!=1.0.1", prereleases=False))
  801. '!=1.0.1,>=1.0.0'
  802. """
  803. return ",".join(str(s) for s in self._canonical_specs())
  804. def __hash__(self) -> int:
  805. return hash(self._canonical_specs())
  806. def __and__(self, other: SpecifierSet | str) -> SpecifierSet:
  807. """Return a SpecifierSet which is a combination of the two sets.
  808. :param other: The other object to combine with.
  809. >>> SpecifierSet(">=1.0.0,!=1.0.1") & '<=2.0.0,!=2.0.1'
  810. <SpecifierSet('!=1.0.1,!=2.0.1,<=2.0.0,>=1.0.0')>
  811. >>> SpecifierSet(">=1.0.0,!=1.0.1") & SpecifierSet('<=2.0.0,!=2.0.1')
  812. <SpecifierSet('!=1.0.1,!=2.0.1,<=2.0.0,>=1.0.0')>
  813. """
  814. if isinstance(other, str):
  815. other = SpecifierSet(other)
  816. elif not isinstance(other, SpecifierSet):
  817. return NotImplemented
  818. specifier = SpecifierSet()
  819. specifier._specs = self._specs + other._specs
  820. specifier._canonicalized = len(specifier._specs) <= 1
  821. specifier._has_arbitrary = self._has_arbitrary or other._has_arbitrary
  822. # Combine prerelease settings: use common or non-None value
  823. if self._prereleases is None or self._prereleases == other._prereleases:
  824. specifier._prereleases = other._prereleases
  825. elif other._prereleases is None:
  826. specifier._prereleases = self._prereleases
  827. else:
  828. raise ValueError(
  829. "Cannot combine SpecifierSets with True and False prerelease overrides."
  830. )
  831. return specifier
  832. def __eq__(self, other: object) -> bool:
  833. """Whether or not the two SpecifierSet-like objects are equal.
  834. :param other: The other object to check against.
  835. The value of :attr:`prereleases` is ignored.
  836. >>> SpecifierSet(">=1.0.0,!=1.0.1") == SpecifierSet(">=1.0.0,!=1.0.1")
  837. True
  838. >>> (SpecifierSet(">=1.0.0,!=1.0.1", prereleases=False) ==
  839. ... SpecifierSet(">=1.0.0,!=1.0.1", prereleases=True))
  840. True
  841. >>> SpecifierSet(">=1.0.0,!=1.0.1") == ">=1.0.0,!=1.0.1"
  842. True
  843. >>> SpecifierSet(">=1.0.0,!=1.0.1") == SpecifierSet(">=1.0.0")
  844. False
  845. >>> SpecifierSet(">=1.0.0,!=1.0.1") == SpecifierSet(">=1.0.0,!=1.0.2")
  846. False
  847. """
  848. if isinstance(other, (str, Specifier)):
  849. other = SpecifierSet(str(other))
  850. elif not isinstance(other, SpecifierSet):
  851. return NotImplemented
  852. return self._canonical_specs() == other._canonical_specs()
  853. def __len__(self) -> int:
  854. """Returns the number of specifiers in this specifier set."""
  855. return len(self._specs)
  856. def __iter__(self) -> Iterator[Specifier]:
  857. """
  858. Returns an iterator over all the underlying :class:`Specifier` instances
  859. in this specifier set.
  860. >>> sorted(SpecifierSet(">=1.0.0,!=1.0.1"), key=str)
  861. [<Specifier('!=1.0.1')>, <Specifier('>=1.0.0')>]
  862. """
  863. return iter(self._specs)
  864. def _get_ranges(self) -> Sequence[Interval]:
  865. """Intersect all specifiers into a single sequence of version ranges.
  866. Empty when unsatisfiable. Callers must ensure ``self._specs``
  867. is non-empty.
  868. """
  869. if self._ranges is not None:
  870. return self._ranges
  871. self._ranges = intersect_specifier_bounds(s._to_ranges() for s in self._specs)
  872. return self._ranges
  873. def is_unsatisfiable(self) -> bool:
  874. """Check whether this specifier set can never be satisfied.
  875. Returns True if no version can satisfy all specifiers simultaneously.
  876. >>> SpecifierSet(">=2.0,<1.0").is_unsatisfiable()
  877. True
  878. >>> SpecifierSet(">=1.0,<2.0").is_unsatisfiable()
  879. False
  880. >>> SpecifierSet("").is_unsatisfiable()
  881. False
  882. >>> SpecifierSet("==1.0,!=1.0").is_unsatisfiable()
  883. True
  884. .. versionadded:: 26.1
  885. """
  886. cached = self._is_unsatisfiable
  887. if cached is not None:
  888. return cached
  889. if not self._specs:
  890. self._is_unsatisfiable = False
  891. return False
  892. result = not self._get_ranges()
  893. if not result:
  894. result = self._check_arbitrary_unsatisfiable()
  895. if not result and self.prereleases is False:
  896. result = ranges_are_prerelease_only(self._get_ranges())
  897. self._is_unsatisfiable = result
  898. return result
  899. def _check_arbitrary_unsatisfiable(self) -> bool:
  900. """Check === (arbitrary equality) specs for unsatisfiability.
  901. === uses case-insensitive string comparison, so the only candidate
  902. that can match ``===V`` is the literal string V. This method
  903. checks whether that candidate is excluded by other specifiers.
  904. """
  905. arbitrary = [s for s in self._specs if s.operator == "==="]
  906. if not arbitrary:
  907. return False
  908. # Multiple === must agree on the same string (case-insensitive).
  909. first = arbitrary[0].version.lower()
  910. if any(s.version.lower() != first for s in arbitrary[1:]):
  911. return True
  912. # The sole candidate is the === version string. Check whether
  913. # it can satisfy every standard spec.
  914. candidate = coerce_version(arbitrary[0].version)
  915. # With prereleases=False, a prerelease candidate is excluded
  916. # by contains() before the === string check even runs.
  917. if (
  918. self.prereleases is False
  919. and candidate is not None
  920. and candidate.is_prerelease
  921. ):
  922. return True
  923. standard = [s for s in self._specs if s.operator != "==="]
  924. if not standard:
  925. return False
  926. if candidate is None:
  927. # Unparsable string cannot satisfy any standard spec.
  928. return True
  929. return not all(s.contains(candidate) for s in standard)
  930. def to_range(self) -> ranges.VersionRange:
  931. """Return the :class:`~packaging.ranges.VersionRange` this set accepts.
  932. An empty set yields the full range; an unsatisfiable set yields the
  933. empty range. ``===`` specifiers contribute literal-string admission.
  934. >>> SpecifierSet(">=1.0,<2.0").to_range()
  935. <VersionRange '[1.0, 2.0.dev0)'>
  936. .. versionadded:: 26.3
  937. """
  938. from .ranges import VersionRange # noqa: PLC0415
  939. return VersionRange._from_specifier_set(self)
  940. def _check_relation_operand(self, other: object) -> None:
  941. if not isinstance(other, SpecifierSet):
  942. raise TypeError("expected a SpecifierSet")
  943. if self._has_arbitrary or other._has_arbitrary:
  944. raise ValueError("set relations do not support === specifiers")
  945. def is_subset(self, other: SpecifierSet) -> bool:
  946. """Return whether every version matching this set also matches other.
  947. :raises ValueError:
  948. If either set uses ``===`` specifiers, or the two sets were
  949. given different ``prereleases`` arguments (unset on one side
  950. counts as different).
  951. :raises TypeError:
  952. If other is not a :class:`SpecifierSet`.
  953. >>> SpecifierSet(">=3.12,<3.13").is_subset(SpecifierSet(">=3.12"))
  954. True
  955. >>> SpecifierSet(">=3.12").is_subset(SpecifierSet(">=3.12,<3.13"))
  956. False
  957. .. versionadded:: 26.3
  958. """
  959. self._check_relation_operand(other)
  960. return self.to_range().is_subset(other.to_range())
  961. def is_superset(self, other: SpecifierSet) -> bool:
  962. """Return whether every version matching other also matches this set.
  963. :raises ValueError:
  964. If either set uses ``===`` specifiers, or the two sets were
  965. given different ``prereleases`` arguments (unset on one side
  966. counts as different).
  967. :raises TypeError:
  968. If other is not a :class:`SpecifierSet`.
  969. >>> SpecifierSet(">=3.12").is_superset(SpecifierSet(">=3.12,<3.13"))
  970. True
  971. .. versionadded:: 26.3
  972. """
  973. self._check_relation_operand(other)
  974. return self.to_range().is_superset(other.to_range())
  975. def is_disjoint(self, other: SpecifierSet) -> bool:
  976. """Return whether this set and other share no matching versions.
  977. :raises ValueError:
  978. If either set uses ``===`` specifiers, or the two sets were
  979. given different ``prereleases`` arguments (unset on one side
  980. counts as different).
  981. :raises TypeError:
  982. If other is not a :class:`SpecifierSet`.
  983. >>> SpecifierSet("<3.12").is_disjoint(SpecifierSet(">=3.12"))
  984. True
  985. >>> SpecifierSet("<3.12").is_disjoint(SpecifierSet(">=3.11"))
  986. False
  987. .. versionadded:: 26.3
  988. """
  989. self._check_relation_operand(other)
  990. return self.to_range().is_disjoint(other.to_range())
  991. def __contains__(self, item: UnparsedVersion) -> bool:
  992. """Return whether or not the item is contained in this specifier.
  993. :param item: The item to check for.
  994. This is used for the ``in`` operator and behaves the same as
  995. :meth:`contains` with no ``prereleases`` argument passed.
  996. >>> "1.2.3" in SpecifierSet(">=1.0.0,!=1.0.1")
  997. True
  998. >>> Version("1.2.3") in SpecifierSet(">=1.0.0,!=1.0.1")
  999. True
  1000. >>> "1.0.1" in SpecifierSet(">=1.0.0,!=1.0.1")
  1001. False
  1002. >>> "1.3.0a1" in SpecifierSet(">=1.0.0,!=1.0.1")
  1003. True
  1004. >>> "1.3.0a1" in SpecifierSet(">=1.0.0,!=1.0.1", prereleases=True)
  1005. True
  1006. """
  1007. return self.contains(item)
  1008. def contains(
  1009. self,
  1010. item: UnparsedVersion,
  1011. prereleases: bool | None = None,
  1012. installed: bool | None = None,
  1013. ) -> bool:
  1014. """Return whether or not the item is contained in this SpecifierSet.
  1015. :param item:
  1016. The item to check for, which can be a version string or a
  1017. :class:`~packaging.version.Version` instance.
  1018. :param prereleases:
  1019. Whether or not to match prereleases with this SpecifierSet. If set to
  1020. ``None`` (the default), it will follow the recommendation from :pep:`440`
  1021. and match prereleases, as there are no other versions.
  1022. :param installed:
  1023. Whether or not the item is installed. If set to ``True``, it will
  1024. accept prerelease versions even if the specifier does not allow them.
  1025. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.2.3")
  1026. True
  1027. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains(Version("1.2.3"))
  1028. True
  1029. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.0.1")
  1030. False
  1031. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.3.0a1")
  1032. True
  1033. >>> SpecifierSet(">=1.0.0,!=1.0.1", prereleases=False).contains("1.3.0a1")
  1034. False
  1035. >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.3.0a1", prereleases=True)
  1036. True
  1037. .. versionchanged:: 26.0
  1038. With ``prereleases=None``, a prerelease now matches. A single
  1039. version has no alternatives, so the :pep:`440` rule to accept
  1040. prereleases when nothing else satisfies the specifiers applies.
  1041. Earlier versions rejected it. An unparsable version now returns
  1042. ``False`` instead of raising :exc:`~packaging.version.InvalidVersion`.
  1043. """
  1044. version = coerce_version(item)
  1045. if version is not None and installed and version.is_prerelease:
  1046. prereleases = True
  1047. # When item is a string and === is involved, keep it as-is
  1048. # so the comparison isn't done against the normalized form.
  1049. if version is None or (self._has_arbitrary and not isinstance(item, Version)):
  1050. check_item = item
  1051. else:
  1052. check_item = version
  1053. # Fast path: a parseable, local-free version against a rangelike set.
  1054. # A local on ``version`` needs PEP 440 stripping that the range path
  1055. # applies.
  1056. if (
  1057. version is not None
  1058. and not self._has_arbitrary
  1059. and version.local is None
  1060. and self._specs
  1061. ):
  1062. if version.is_prerelease and (
  1063. prereleases is False
  1064. or (prereleases is None and self._prereleases is False)
  1065. ):
  1066. return False
  1067. bounds = self._ranges
  1068. if bounds is None:
  1069. # Per-spec ``_fast_match`` answers a set of simple specifiers
  1070. # without folding anything. If a spec needs the range path,
  1071. # fold the intersected bounds once and cache them so repeated
  1072. # checks on the same set stay cheap.
  1073. for spec in self._specs:
  1074. match = _fast_match(spec, version)
  1075. if match is None:
  1076. break
  1077. if not match:
  1078. return False
  1079. else:
  1080. return True
  1081. bounds = self._ranges = self._get_ranges()
  1082. return matches_bounds_only(bounds, version)
  1083. return bool(list(self.filter([check_item], prereleases=prereleases)))
  1084. @typing.overload
  1085. def filter(
  1086. self,
  1087. iterable: Iterable[UnparsedVersionVar],
  1088. prereleases: bool | None = None,
  1089. key: None = ...,
  1090. ) -> Iterator[UnparsedVersionVar]: ...
  1091. @typing.overload
  1092. def filter(
  1093. self,
  1094. iterable: Iterable[T],
  1095. prereleases: bool | None = None,
  1096. key: Callable[[T], UnparsedVersion] = ...,
  1097. ) -> Iterator[T]: ...
  1098. def filter(
  1099. self,
  1100. iterable: Iterable[Any],
  1101. prereleases: bool | None = None,
  1102. key: Callable[[Any], UnparsedVersion] | None = None,
  1103. ) -> Iterator[Any]:
  1104. """Filter items in the given iterable, that match the specifiers in this set.
  1105. :param iterable:
  1106. An iterable that can contain version strings and
  1107. :class:`~packaging.version.Version` instances. The items in the
  1108. iterable will be filtered according to the specifier.
  1109. :param prereleases:
  1110. Whether or not to allow prereleases in the returned iterator. If set to
  1111. ``None`` (the default), it will follow the recommendation from :pep:`440`
  1112. and match prereleases if there are no other versions.
  1113. :param key:
  1114. A callable that takes a single argument (an item from the iterable) and
  1115. returns a version string or :class:`~packaging.version.Version`
  1116. instance to be used for filtering.
  1117. >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.3", "1.5a1"]))
  1118. ['1.3']
  1119. >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.3", Version("1.4")]))
  1120. ['1.3', <Version('1.4')>]
  1121. >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.5a1"]))
  1122. ['1.5a1']
  1123. >>> list(SpecifierSet(">=1.2.3").filter(["1.3", "1.5a1"], prereleases=True))
  1124. ['1.3', '1.5a1']
  1125. >>> list(SpecifierSet(">=1.2.3", prereleases=True).filter(["1.3", "1.5a1"]))
  1126. ['1.3', '1.5a1']
  1127. >>> list(SpecifierSet(">=1.2.3").filter(
  1128. ... [{"ver": "1.2"}, {"ver": "1.3"}],
  1129. ... key=lambda x: x["ver"]))
  1130. [{'ver': '1.3'}]
  1131. An "empty" SpecifierSet will filter items based on the presence of prerelease
  1132. versions in the set.
  1133. >>> list(SpecifierSet("").filter(["1.3", "1.5a1"]))
  1134. ['1.3']
  1135. >>> list(SpecifierSet("").filter(["1.5a1"]))
  1136. ['1.5a1']
  1137. >>> list(SpecifierSet("", prereleases=True).filter(["1.3", "1.5a1"]))
  1138. ['1.3', '1.5a1']
  1139. >>> list(SpecifierSet("").filter(["1.3", "1.5a1"], prereleases=True))
  1140. ['1.3', '1.5a1']
  1141. .. versionchanged:: 26.0
  1142. Prerelease filtering now follows the PEP 440 recommendation of
  1143. yielding prereleases only when no final release is present.
  1144. .. versionchanged:: 26.1
  1145. Added the ``key`` parameter.
  1146. """
  1147. # Determine if we're forcing a prerelease or not, if we're not forcing
  1148. # one for this particular filter call, then we'll use whatever the
  1149. # SpecifierSet thinks for whether or not we should support prereleases.
  1150. if prereleases is None and self.prereleases is not None:
  1151. prereleases = self.prereleases
  1152. if self._specs:
  1153. if self._has_arbitrary:
  1154. # Slow path for ===
  1155. specs = self._specs
  1156. matches = (
  1157. item
  1158. for item in iterable
  1159. if all(
  1160. s.contains(item if key is None else key(item), prereleases=True)
  1161. for s in specs
  1162. )
  1163. )
  1164. return _apply_prereleases_filter(matches, key, prereleases)
  1165. ranges = self._ranges
  1166. if ranges is None:
  1167. ranges = self._get_ranges()
  1168. return filter_by_ranges(ranges, iterable, key, prereleases)
  1169. # Empty SpecifierSet.
  1170. return _apply_prereleases_filter(iterable, key, prereleases)
  1171. def _pep440_filter_prereleases(
  1172. iterable: Iterable[Any], key: Callable[[Any], UnparsedVersion] | None
  1173. ) -> Iterator[Any]:
  1174. """Filter per PEP 440: exclude prereleases unless no finals exist."""
  1175. # Two lists used:
  1176. # * all_nonfinal to preserve order if no finals exist
  1177. # * arbitrary_strings for streaming when first final found
  1178. all_nonfinal: list[Any] = []
  1179. arbitrary_strings: list[Any] = []
  1180. found_final = False
  1181. for item in iterable:
  1182. parsed = coerce_version(item if key is None else key(item))
  1183. if parsed is None:
  1184. # Arbitrary strings are always included as it is not
  1185. # possible to determine if they are prereleases,
  1186. # and they have already passed all specifiers.
  1187. if found_final:
  1188. yield item
  1189. else:
  1190. arbitrary_strings.append(item)
  1191. all_nonfinal.append(item)
  1192. continue
  1193. if not parsed.is_prerelease:
  1194. # Final release found - flush arbitrary strings, then yield
  1195. if not found_final:
  1196. yield from arbitrary_strings
  1197. found_final = True
  1198. yield item
  1199. continue
  1200. # Prerelease - buffer if no finals yet, otherwise skip
  1201. if not found_final:
  1202. all_nonfinal.append(item)
  1203. # No finals found - yield all buffered items
  1204. if not found_final:
  1205. yield from all_nonfinal