@@ -174,31 +174,25 @@ def from_versions(cls, sequence):
174174 constraints .append (constraint )
175175 return cls (constraints = constraints )
176176
177+ def is_star (self ):
178+ return len (self .constraints ) == 1 and self .constraints [0 ].is_star ()
179+
177180 def invert (self ):
178181 """
179- Return the inverse of this VersionRange. For example, if this range is
182+ Return the inverse or complement of this VersionRange. For example, if this range is
180183 ">=1.0.0", the inverse is "<1.0.0".
181- >>> VersionRange.from_string("vers:npm/>=1.0.0").invert()
182- NpmVersionRange(constraints=(VersionConstraint(comparator='<', version=SemverVersion(string=' 1.0.0')),))
184+ >>> str( VersionRange.from_string("vers:npm/>=1.0.0").invert() )
185+ 'vers:npm/< 1.0.0'
183186 """
184187 inverted_constraints = []
185188
186- if len ( self .constraints ) == 1 and self . constraints [ 0 ]. comparator == "*" :
189+ if self .is_star () :
187190 # The inverse of "*" is an empty range.
188191 return None
189192
190193 for constraint in self .constraints :
191- if constraint .comparator in INVERTED_COMPARATORS :
192- inverted_comparator = INVERTED_COMPARATORS [constraint .comparator ]
193- else :
194- raise NotImplementedError (
195- f"Cannot invert a range with a { constraint .comparator !r} comparator."
196- )
197- inverted_constraint = VersionConstraint (
198- comparator = inverted_comparator ,
199- version = constraint .version ,
200- )
201- inverted_constraints .append (inverted_constraint )
194+ inverted_constraints .append (constraint .invert ())
195+
202196 return self .__class__ (constraints = inverted_constraints )
203197
204198 def __str__ (self ):
0 commit comments