contains method

bool contains({
  1. required Object? item,
  2. Object? prereleases,
  3. Object? installed,
})

contains

python docstring

Return whether or not the item is contained in this SpecifierSet.

:param item: The item to check for, which can be a version string or a :class:Version instance. :param prereleases: Whether or not to match prereleases with this SpecifierSet. If set to None (the default), it uses :attr:prereleases to determine whether or not prereleases are allowed.

SpecifierSet(">=1.0.0,!=1.0.1").contains("1.2.3") True SpecifierSet(">=1.0.0,!=1.0.1").contains(Version("1.2.3")) True SpecifierSet(">=1.0.0,!=1.0.1").contains("1.0.1") False SpecifierSet(">=1.0.0,!=1.0.1").contains("1.3.0a1") False SpecifierSet(">=1.0.0,!=1.0.1", prereleases=True).contains("1.3.0a1") True SpecifierSet(">=1.0.0,!=1.0.1").contains("1.3.0a1", prereleases=True) True

python source

def contains(
        self,
        item: UnparsedVersion,
        prereleases: Optional[bool] = None,
        installed: Optional[bool] = None,
    ) -> bool:
        """Return whether or not the item is contained in this SpecifierSet.

        :param item:
            The item to check for, which can be a version string or a
            :class:`Version` instance.
        :param prereleases:
            Whether or not to match prereleases with this SpecifierSet. If set to
            ``None`` (the default), it uses :attr:`prereleases` to determine
            whether or not prereleases are allowed.

        >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.2.3")
        True
        >>> SpecifierSet(">=1.0.0,!=1.0.1").contains(Version("1.2.3"))
        True
        >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.0.1")
        False
        >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.3.0a1")
        False
        >>> SpecifierSet(">=1.0.0,!=1.0.1", prereleases=True).contains("1.3.0a1")
        True
        >>> SpecifierSet(">=1.0.0,!=1.0.1").contains("1.3.0a1", prereleases=True)
        True
        """
        # Ensure that our item is a Version instance.
        if not isinstance(item, Version):
            item = Version(item)

        # Determine if we're forcing a prerelease or not, if we're not forcing
        # one for this particular filter call, then we'll use whatever the
        # SpecifierSet thinks for whether or not we should support prereleases.
        if prereleases is None:
            prereleases = self.prereleases

        # We can determine if we're going to allow pre-releases by looking to
        # see if any of the underlying items supports them. If none of them do
        # and this item is a pre-release then we do not allow it and we can
        # short circuit that here.
        # Note: This means that 1.0.dev1 would not be contained in something
        #       like >=1.0.devabc however it would be in >=1.0.debabc,>0.0.dev0
        if not prereleases and item.is_prerelease:
            return False

        if installed and item.is_prerelease:
            item = Version(item.base_version)

        # We simply dispatch to the underlying specs here to make sure that the
        # given version is contained within all of them.
        # Note: This use of all() here means that an empty set of specifiers
        #       will always return True, this is an explicit design decision.
        return all(s.contains(item, prereleases=prereleases) for s in self._specs)

Implementation

bool contains({
  required Object? item,
  Object? prereleases,
  Object? installed,
}) =>
    getFunction("contains").call(
      <Object?>[
        item,
        prereleases,
        installed,
      ],
      kwargs: <String, Object?>{},
    );