constraint

Constraint

class versions.constraint.Constraint(operator, version)[source]

A constraint on a package version.

Parameters:
  • operator (Operator) – The constraint operator.
  • version (Version) – The constraint version.
match(version)[source]

Match version with the contraint.

Parameters:version (version string or Version) – Version to match against the constraint.
Return type:True if version satisfies the constraint, False if it doesn’t.
classmethod parse(constraint_string)[source]

Parses a constraint string and returns a Constraint object.

Raises :InvalidConstraint when parsing fails.

Parsing

Constraint has a convenient parse static method to parse constrints strings into Constraint objects.

Constraint strings are composed of a constraint operator, followed by a valid version string.

Valid constraint operators: ==, !=, <, >, <= and >=.

Matching

Examples:

>>> from versions import Constraint, Version
>>> Constraint.parse('>1').match('2')
True
>>> Constraint.parse('<2').match(Version.parse('1'))
True
>>> '1.5' in Constraint.parse('== 1.0')
False
>>> Version(1, 5) in Constraint.parse('> 1.0')
True
>>> Version(1) in Constraint.parse('>= 2.0.0')
False

InvalidConstraint

exception versions.constraint.InvalidConstraint(constraint)[source]

Raised when failing to parse a constraint.

constraint = None

The bogus constraint.

Table Of Contents

Previous topic

version

Next topic

constraints