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.
24 from .utils
import canonicalize_version
25 from .version
import LegacyVersion
, Version
, parse
27 ParsedVersion
= Union
[Version
, LegacyVersion
]
28 UnparsedVersion
= Union
[Version
, LegacyVersion
, str]
29 VersionTypeVar
= TypeVar("VersionTypeVar", bound
=UnparsedVersion
)
30 CallableOperator
= Callable
[[ParsedVersion
, str], bool]
33 class InvalidSpecifier(ValueError):
35 An invalid specifier was found, users should refer to PEP 440.
39 class BaseSpecifier(metaclass
=abc
.ABCMeta
):
41 def __str__(self
) -> str:
43 Returns the str representation of this Specifier like object. This
44 should be representative of the Specifier itself.
48 def __hash__(self
) -> int:
50 Returns a hash value for this Specifier like object.
54 def __eq__(self
, other
: object) -> bool:
56 Returns a boolean representing whether or not the two Specifier like
61 def prereleases(self
) -> Optional
[bool]:
63 Returns whether or not pre-releases as a whole are allowed by this
68 def prereleases(self
, value
: bool) -> None:
70 Sets whether or not pre-releases as a whole are allowed by this
75 def contains(self
, item
: str, prereleases
: Optional
[bool] = None) -> bool:
77 Determines if the given item is contained within this specifier.
82 self
, iterable
: Iterable
[VersionTypeVar
], prereleases
: Optional
[bool] = None
83 ) -> Iterable
[VersionTypeVar
]:
85 Takes an iterable of items and filters them so that only items which
86 are contained within this specifier are allowed in it.
90 class _IndividualSpecifier(BaseSpecifier
):
92 _operators
: Dict
[str, str] = {}
95 def __init__(self
, spec
: str = "", prereleases
: Optional
[bool] = None) -> None:
96 match
= self
._regex
.search(spec
)
98 raise InvalidSpecifier(f
"Invalid specifier: '{spec}'")
100 self
._spec
: Tuple
[str, str] = (
101 match
.group("operator").strip(),
102 match
.group("version").strip(),
105 # Store whether or not this Specifier should accept prereleases
106 self
._prereleases
= prereleases
108 def __repr__(self
) -> str:
110 f
", prereleases={self.prereleases!r}"
111 if self
._prereleases
is not None
115 return f
"<{self.__class__.__name__}({str(self)!r}{pre})>"
117 def __str__(self
) -> str:
118 return "{}{}".format(*self
._spec
)
121 def _canonical_spec(self
) -> Tuple
[str, str]:
122 return self
._spec
[0], canonicalize_version(self
._spec
[1])
124 def __hash__(self
) -> int:
125 return hash(self
._canonical
_spec
)
127 def __eq__(self
, other
: object) -> bool:
128 if isinstance(other
, str):
130 other
= self
.__class
__(str(other
))
131 except InvalidSpecifier
:
132 return NotImplemented
133 elif not isinstance(other
, self
.__class
__):
134 return NotImplemented
136 return self
._canonical
_spec
== other
._canonical
_spec
138 def _get_operator(self
, op
: str) -> CallableOperator
:
139 operator_callable
: CallableOperator
= getattr(
140 self
, f
"_compare_{self._operators[op]}"
142 return operator_callable
144 def _coerce_version(self
, version
: UnparsedVersion
) -> ParsedVersion
:
145 if not isinstance(version
, (LegacyVersion
, Version
)):
146 version
= parse(version
)
150 def operator(self
) -> str:
154 def version(self
) -> str:
158 def prereleases(self
) -> Optional
[bool]:
159 return self
._prereleases
162 def prereleases(self
, value
: bool) -> None:
163 self
._prereleases
= value
165 def __contains__(self
, item
: str) -> bool:
166 return self
.contains(item
)
169 self
, item
: UnparsedVersion
, prereleases
: Optional
[bool] = None
172 # Determine if prereleases are to be allowed or not.
173 if prereleases
is None:
174 prereleases
= self
.prereleases
176 # Normalize item to a Version or LegacyVersion, this allows us to have
177 # a shortcut for ``"2.0" in Specifier(">=2")
178 normalized_item
= self
._coerce
_version
(item
)
180 # Determine if we should be supporting prereleases in this specifier
181 # or not, if we do not support prereleases than we can short circuit
182 # logic if this version is a prereleases.
183 if normalized_item
.is_prerelease
and not prereleases
:
186 # Actually do the comparison to determine if this item is contained
187 # within this Specifier or not.
188 operator_callable
: CallableOperator
= self
._get
_operator
(self
.operator
)
189 return operator_callable(normalized_item
, self
.version
)
192 self
, iterable
: Iterable
[VersionTypeVar
], prereleases
: Optional
[bool] = None
193 ) -> Iterable
[VersionTypeVar
]:
196 found_prereleases
= []
198 kw
= {"prereleases": prereleases if prereleases is not None else True}
200 # Attempt to iterate over all the values in the iterable and if any of
201 # them match, yield them.
202 for version
in iterable
:
203 parsed_version
= self
._coerce
_version
(version
)
205 if self
.contains(parsed_version
, **kw
):
206 # If our version is a prerelease, and we were not set to allow
207 # prereleases, then we'll store it for later in case nothing
208 # else matches this specifier.
209 if parsed_version
.is_prerelease
and not (
210 prereleases
or self
.prereleases
212 found_prereleases
.append(version
)
213 # Either this is not a prerelease, or we should have been
214 # accepting prereleases from the beginning.
219 # Now that we've iterated over everything, determine if we've yielded
220 # any values, and if we have not and we have any prereleases stored up
221 # then we will go ahead and yield the prereleases.
222 if not yielded
and found_prereleases
:
223 for version
in found_prereleases
:
227 class LegacySpecifier(_IndividualSpecifier
):
230 (?P<operator>(==|!=|<=|>=|<|>))
233 [^,;\s)]* # Since this is a "legacy" specifier, and the version
234 # string can be just about anything, we match everything
235 # except for whitespace, a semi-colon for marker support,
236 # a closing paren since versions can be enclosed in
237 # them, and a comma since it's a version separator.
241 _regex
= re
.compile(r
"^\s*" + _regex_str
+ r
"\s*$", re
.VERBOSE | re
.IGNORECASE
)
246 "<=": "less_than_equal",
247 ">=": "greater_than_equal",
252 def __init__(self
, spec
: str = "", prereleases
: Optional
[bool] = None) -> None:
253 super().__init
__(spec
, prereleases
)
256 "Creating a LegacyVersion has been deprecated and will be "
257 "removed in the next major release",
261 def _coerce_version(self
, version
: UnparsedVersion
) -> LegacyVersion
:
262 if not isinstance(version
, LegacyVersion
):
263 version
= LegacyVersion(str(version
))
266 def _compare_equal(self
, prospective
: LegacyVersion
, spec
: str) -> bool:
267 return prospective
== self
._coerce
_version
(spec
)
269 def _compare_not_equal(self
, prospective
: LegacyVersion
, spec
: str) -> bool:
270 return prospective
!= self
._coerce
_version
(spec
)
272 def _compare_less_than_equal(self
, prospective
: LegacyVersion
, spec
: str) -> bool:
273 return prospective
<= self
._coerce
_version
(spec
)
275 def _compare_greater_than_equal(
276 self
, prospective
: LegacyVersion
, spec
: str
278 return prospective
>= self
._coerce
_version
(spec
)
280 def _compare_less_than(self
, prospective
: LegacyVersion
, spec
: str) -> bool:
281 return prospective
< self
._coerce
_version
(spec
)
283 def _compare_greater_than(self
, prospective
: LegacyVersion
, spec
: str) -> bool:
284 return prospective
> self
._coerce
_version
(spec
)
287 def _require_version_compare(
288 fn
: Callable
[["Specifier", ParsedVersion
, str], bool]
289 ) -> Callable
[["Specifier", ParsedVersion
, str], bool]:
291 def wrapped(self
: "Specifier", prospective
: ParsedVersion
, spec
: str) -> bool:
292 if not isinstance(prospective
, Version
):
294 return fn(self
, prospective
, spec
)
299 class Specifier(_IndividualSpecifier
):
302 (?P<operator>(~=|==|!=|<=|>=|<|>|===))
305 # The identity operators allow for an escape hatch that will
306 # do an exact string match of the version you wish to install.
307 # This will not be parsed by PEP 440 and we cannot determine
308 # any semantic meaning from it. This operator is discouraged
309 # but included entirely as an escape hatch.
310 (?<====) # Only match for the identity operator
312 [^\s]* # We just match everything, except for whitespace
313 # since we are only testing for strict identity.
317 # The (non)equality operators allow for wild card and local
318 # versions to be specified so we have to define these two
319 # operators separately to enable that.
320 (?<===|!=) # Only match for equals and not equals
325 [0-9]+(?:\.[0-9]+)* # release
328 (a|b|c|rc|alpha|beta|pre|preview)
333 (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
336 # You cannot use a wild card and a dev or local version
337 # together so group them with a | and make them optional.
339 (?:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
340 (?:\+[a-z0-9]+(?:[-_\.][a-z0-9]+)*)? # local
342 \.\* # Wild card syntax of .*
347 # The compatible operator requires at least two digits in the
349 (?<=~=) # Only match for the compatible operator
354 [0-9]+(?:\.[0-9]+)+ # release (We have a + instead of a *)
357 (a|b|c|rc|alpha|beta|pre|preview)
362 (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
364 (?:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
368 # All other operators only allow a sub set of what the
369 # (non)equality operators do. Specifically they do not allow
370 # local versions to be specified nor do they allow the prefix
371 # matching wild cards.
372 (?<!==|!=|~=) # We have special cases for these
373 # operators so we want to make sure they
379 [0-9]+(?:\.[0-9]+)* # release
382 (a|b|c|rc|alpha|beta|pre|preview)
387 (?:-[0-9]+)|(?:[-_\.]?(post|rev|r)[-_\.]?[0-9]*)
389 (?:[-_\.]?dev[-_\.]?[0-9]*)? # dev release
394 _regex
= re
.compile(r
"^\s*" + _regex_str
+ r
"\s*$", re
.VERBOSE | re
.IGNORECASE
)
400 "<=": "less_than_equal",
401 ">=": "greater_than_equal",
407 @_require_version_compare
408 def _compare_compatible(self
, prospective
: ParsedVersion
, spec
: str) -> bool:
410 # Compatible releases have an equivalent combination of >= and ==. That
411 # is that ~=2.2 is equivalent to >=2.2,==2.*. This allows us to
412 # implement this in terms of the other specifiers instead of
413 # implementing it ourselves. The only thing we need to do is construct
414 # the other specifiers.
416 # We want everything but the last item in the version, but we want to
417 # ignore suffix segments.
419 list(itertools
.takewhile(_is_not_suffix
, _version_split(spec
)))[:-1]
422 # Add the prefix notation to the end of our string
425 return self
._get
_operator
(">=")(prospective
, spec
) and self
._get
_operator
("==")(
429 @_require_version_compare
430 def _compare_equal(self
, prospective
: ParsedVersion
, spec
: str) -> bool:
432 # We need special logic to handle prefix matching
433 if spec
.endswith(".*"):
434 # In the case of prefix matching we want to ignore local segment.
435 prospective
= Version(prospective
.public
)
436 # Split the spec out by dots, and pretend that there is an implicit
437 # dot in between a release segment and a pre-release segment.
438 split_spec
= _version_split(spec
[:-2]) # Remove the trailing .*
440 # Split the prospective version out by dots, and pretend that there
441 # is an implicit dot in between a release segment and a pre-release
443 split_prospective
= _version_split(str(prospective
))
445 # Shorten the prospective version to be the same length as the spec
446 # so that we can determine if the specifier is a prefix of the
447 # prospective version or not.
448 shortened_prospective
= split_prospective
[: len(split_spec
)]
450 # Pad out our two sides with zeros so that they both equal the same
452 padded_spec
, padded_prospective
= _pad_version(
453 split_spec
, shortened_prospective
456 return padded_prospective
== padded_spec
458 # Convert our spec string into a Version
459 spec_version
= Version(spec
)
461 # If the specifier does not have a local segment, then we want to
462 # act as if the prospective version also does not have a local
464 if not spec_version
.local
:
465 prospective
= Version(prospective
.public
)
467 return prospective
== spec_version
469 @_require_version_compare
470 def _compare_not_equal(self
, prospective
: ParsedVersion
, spec
: str) -> bool:
471 return not self
._compare
_equal
(prospective
, spec
)
473 @_require_version_compare
474 def _compare_less_than_equal(self
, prospective
: ParsedVersion
, spec
: str) -> bool:
476 # NB: Local version identifiers are NOT permitted in the version
477 # specifier, so local version labels can be universally removed from
478 # the prospective version.
479 return Version(prospective
.public
) <= Version(spec
)
481 @_require_version_compare
482 def _compare_greater_than_equal(
483 self
, prospective
: ParsedVersion
, spec
: str
486 # NB: Local version identifiers are NOT permitted in the version
487 # specifier, so local version labels can be universally removed from
488 # the prospective version.
489 return Version(prospective
.public
) >= Version(spec
)
491 @_require_version_compare
492 def _compare_less_than(self
, prospective
: ParsedVersion
, spec_str
: str) -> bool:
494 # Convert our spec to a Version instance, since we'll want to work with
496 spec
= Version(spec_str
)
498 # Check to see if the prospective version is less than the spec
499 # version. If it's not we can short circuit and just return False now
500 # instead of doing extra unneeded work.
501 if not prospective
< spec
:
504 # This special case is here so that, unless the specifier itself
505 # includes is a pre-release version, that we do not accept pre-release
506 # versions for the version mentioned in the specifier (e.g. <3.1 should
507 # not match 3.1.dev0, but should match 3.0.dev0).
508 if not spec
.is_prerelease
and prospective
.is_prerelease
:
509 if Version(prospective
.base_version
) == Version(spec
.base_version
):
512 # If we've gotten to here, it means that prospective version is both
513 # less than the spec version *and* it's not a pre-release of the same
514 # version in the spec.
517 @_require_version_compare
518 def _compare_greater_than(self
, prospective
: ParsedVersion
, spec_str
: str) -> bool:
520 # Convert our spec to a Version instance, since we'll want to work with
522 spec
= Version(spec_str
)
524 # Check to see if the prospective version is greater than the spec
525 # version. If it's not we can short circuit and just return False now
526 # instead of doing extra unneeded work.
527 if not prospective
> spec
:
530 # This special case is here so that, unless the specifier itself
531 # includes is a post-release version, that we do not accept
532 # post-release versions for the version mentioned in the specifier
533 # (e.g. >3.1 should not match 3.0.post0, but should match 3.2.post0).
534 if not spec
.is_postrelease
and prospective
.is_postrelease
:
535 if Version(prospective
.base_version
) == Version(spec
.base_version
):
538 # Ensure that we do not allow a local version of the version mentioned
539 # in the specifier, which is technically greater than, to match.
540 if prospective
.local
is not None:
541 if Version(prospective
.base_version
) == Version(spec
.base_version
):
544 # If we've gotten to here, it means that prospective version is both
545 # greater than the spec version *and* it's not a pre-release of the
546 # same version in the spec.
549 def _compare_arbitrary(self
, prospective
: Version
, spec
: str) -> bool:
550 return str(prospective
).lower() == str(spec
).lower()
553 def prereleases(self
) -> bool:
555 # If there is an explicit prereleases set for this, then we'll just
557 if self
._prereleases
is not None:
558 return self
._prereleases
560 # Look at all of our specifiers and determine if they are inclusive
561 # operators, and if they are if they are including an explicit
563 operator
, version
= self
._spec
564 if operator
in ["==", ">=", "<=", "~=", "==="]:
565 # The == specifier can include a trailing .*, if it does we
566 # want to remove before parsing.
567 if operator
== "==" and version
.endswith(".*"):
568 version
= version
[:-2]
570 # Parse the version, and if it is a pre-release than this
571 # specifier allows pre-releases.
572 if parse(version
).is_prerelease
:
578 def prereleases(self
, value
: bool) -> None:
579 self
._prereleases
= value
582 _prefix_regex
= re
.compile(r
"^([0-9]+)((?:a|b|c|rc)[0-9]+)$")
585 def _version_split(version
: str) -> List
[str]:
586 result
: List
[str] = []
587 for item
in version
.split("."):
588 match
= _prefix_regex
.search(item
)
590 result
.extend(match
.groups())
596 def _is_not_suffix(segment
: str) -> bool:
598 segment
.startswith(prefix
) for prefix
in ("dev", "a", "b", "rc", "post")
602 def _pad_version(left
: List
[str], right
: List
[str]) -> Tuple
[List
[str], List
[str]]:
603 left_split
, right_split
= [], []
605 # Get the release segment of our versions
606 left_split
.append(list(itertools
.takewhile(lambda x
: x
.isdigit(), left
)))
607 right_split
.append(list(itertools
.takewhile(lambda x
: x
.isdigit(), right
)))
609 # Get the rest of our versions
610 left_split
.append(left
[len(left_split
[0]) :])
611 right_split
.append(right
[len(right_split
[0]) :])
614 left_split
.insert(1, ["0"] * max(0, len(right_split
[0]) - len(left_split
[0])))
615 right_split
.insert(1, ["0"] * max(0, len(left_split
[0]) - len(right_split
[0])))
617 return (list(itertools
.chain(*left_split
)), list(itertools
.chain(*right_split
)))
620 class SpecifierSet(BaseSpecifier
):
622 self
, specifiers
: str = "", prereleases
: Optional
[bool] = None
625 # Split on , to break each individual specifier into it's own item, and
626 # strip each item to remove leading/trailing whitespace.
627 split_specifiers
= [s
.strip() for s
in specifiers
.split(",") if s
.strip()]
629 # Parsed each individual specifier, attempting first to make it a
630 # Specifier and falling back to a LegacySpecifier.
631 parsed
: Set
[_IndividualSpecifier
] = set()
632 for specifier
in split_specifiers
:
634 parsed
.add(Specifier(specifier
))
635 except InvalidSpecifier
:
636 parsed
.add(LegacySpecifier(specifier
))
638 # Turn our parsed specifiers into a frozen set and save them for later.
639 self
._specs
= frozenset(parsed
)
641 # Store our prereleases value so we can use it later to determine if
642 # we accept prereleases or not.
643 self
._prereleases
= prereleases
645 def __repr__(self
) -> str:
647 f
", prereleases={self.prereleases!r}"
648 if self
._prereleases
is not None
652 return f
"<SpecifierSet({str(self)!r}{pre})>"
654 def __str__(self
) -> str:
655 return ",".join(sorted(str(s
) for s
in self
._specs
))
657 def __hash__(self
) -> int:
658 return hash(self
._specs
)
660 def __and__(self
, other
: Union
["SpecifierSet", str]) -> "SpecifierSet":
661 if isinstance(other
, str):
662 other
= SpecifierSet(other
)
663 elif not isinstance(other
, SpecifierSet
):
664 return NotImplemented
666 specifier
= SpecifierSet()
667 specifier
._specs
= frozenset(self
._specs | other
._specs
)
669 if self
._prereleases
is None and other
._prereleases
is not None:
670 specifier
._prereleases
= other
._prereleases
671 elif self
._prereleases
is not None and other
._prereleases
is None:
672 specifier
._prereleases
= self
._prereleases
673 elif self
._prereleases
== other
._prereleases
:
674 specifier
._prereleases
= self
._prereleases
677 "Cannot combine SpecifierSets with True and False prerelease "
683 def __eq__(self
, other
: object) -> bool:
684 if isinstance(other
, (str, _IndividualSpecifier
)):
685 other
= SpecifierSet(str(other
))
686 elif not isinstance(other
, SpecifierSet
):
687 return NotImplemented
689 return self
._specs
== other
._specs
691 def __len__(self
) -> int:
692 return len(self
._specs
)
694 def __iter__(self
) -> Iterator
[_IndividualSpecifier
]:
695 return iter(self
._specs
)
698 def prereleases(self
) -> Optional
[bool]:
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
705 # If we don't have any specifiers, and we don't have a forced value,
706 # then we'll just return None since we don't know if this should have
707 # pre-releases or not.
711 # Otherwise we'll see if any of the given specifiers accept
712 # prereleases, if any of them do we'll return True, otherwise False.
713 return any(s
.prereleases
for s
in self
._specs
)
716 def prereleases(self
, value
: bool) -> None:
717 self
._prereleases
= value
719 def __contains__(self
, item
: UnparsedVersion
) -> bool:
720 return self
.contains(item
)
723 self
, item
: UnparsedVersion
, prereleases
: Optional
[bool] = None
726 # Ensure that our item is a Version or LegacyVersion instance.
727 if not isinstance(item
, (LegacyVersion
, Version
)):
730 # Determine if we're forcing a prerelease or not, if we're not forcing
731 # one for this particular filter call, then we'll use whatever the
732 # SpecifierSet thinks for whether or not we should support prereleases.
733 if prereleases
is None:
734 prereleases
= self
.prereleases
736 # We can determine if we're going to allow pre-releases by looking to
737 # see if any of the underlying items supports them. If none of them do
738 # and this item is a pre-release then we do not allow it and we can
739 # short circuit that here.
740 # Note: This means that 1.0.dev1 would not be contained in something
741 # like >=1.0.devabc however it would be in >=1.0.debabc,>0.0.dev0
742 if not prereleases
and item
.is_prerelease
:
745 # We simply dispatch to the underlying specs here to make sure that the
746 # given version is contained within all of them.
747 # Note: This use of all() here means that an empty set of specifiers
748 # will always return True, this is an explicit design decision.
749 return all(s
.contains(item
, prereleases
=prereleases
) for s
in self
._specs
)
752 self
, iterable
: Iterable
[VersionTypeVar
], prereleases
: Optional
[bool] = None
753 ) -> Iterable
[VersionTypeVar
]:
755 # Determine if we're forcing a prerelease or not, if we're not forcing
756 # one for this particular filter call, then we'll use whatever the
757 # SpecifierSet thinks for whether or not we should support prereleases.
758 if prereleases
is None:
759 prereleases
= self
.prereleases
761 # If we have any specifiers, then we want to wrap our iterable in the
762 # filter method for each one, this will act as a logical AND amongst
765 for spec
in self
._specs
:
766 iterable
= spec
.filter(iterable
, prereleases
=bool(prereleases
))
768 # If we do not have any specifiers, then we need to have a rough filter
769 # which will filter out any pre-releases, unless there are no final
770 # releases, and which will filter out LegacyVersion in general.
772 filtered
: List
[VersionTypeVar
] = []
773 found_prereleases
: List
[VersionTypeVar
] = []
775 item
: UnparsedVersion
776 parsed_version
: Union
[Version
, LegacyVersion
]
778 for item
in iterable
:
779 # Ensure that we some kind of Version class for this item.
780 if not isinstance(item
, (LegacyVersion
, Version
)):
781 parsed_version
= parse(item
)
783 parsed_version
= item
785 # Filter out any item which is parsed as a LegacyVersion
786 if isinstance(parsed_version
, LegacyVersion
):
789 # Store any item which is a pre-release for later unless we've
790 # already found a final version or we are accepting prereleases
791 if parsed_version
.is_prerelease
and not prereleases
:
793 found_prereleases
.append(item
)
795 filtered
.append(item
)
797 # If we've found no items except for pre-releases, then we'll go
798 # ahead and use the pre-releases
799 if not filtered
and found_prereleases
and prereleases
is None:
800 return found_prereleases