Skip to content

Version Sets

VersionSet = Union[VersionEmpty, VersionPoint, VersionRange, VersionUnion] module-attribute

The union of the following types:

VersionItem = Union[VersionPoint, VersionRange] module-attribute

The union of VersionPoint and VersionRange.

VersionSetProtocol

Bases: Specification, Protocol

Source code in versions/version_sets.py
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
@runtime_checkable
class VersionSetProtocol(Specification, Protocol):
    @required
    def is_empty(self) -> bool:
        """Checks if the set is *empty*.

        Returns:
            Whether the set is *empty*.
        """
        raise NotImplementedError

    @required
    def is_universal(self) -> bool:
        """Checks if the set is the *universal*.

        Returns:
            Whether the set is *universal*.
        """
        raise NotImplementedError

    @required
    def includes(self, version_set: VersionSet) -> bool:
        """Checks if the set includes `version_set`.

        Returns:
            Whether the set includes `version_set`.
        """
        raise NotImplementedError

    @required
    def intersects(self, version_set: VersionSet) -> bool:
        """Checks if the set intersects `version_set`.

        Returns:
            Whether the set intersects `version_set`.
        """
        raise NotImplementedError

    @required
    def contains(self, version: Version) -> bool:
        """Checks if the set contains some `version`.

        Returns:
            Whether the `version` is contained within the set.
        """
        raise NotImplementedError

    def accepts(self, version: Version) -> bool:
        """Checks if the set contains some `version`.

        This is an implementation of the [`accepts`][versions.specification.Specification.accepts]
        method of [`Specification`][versions.specification.Specification] protocol, equivalent to
        [`self.contains(version)`][versions.version_sets.VersionSetProtocol.contains].

        Returns:
            Whether the `version` is accepted by the set.
        """
        return self.contains(version)

    @required
    def intersection(self, version_set: VersionSet) -> VersionSet:
        """Computes the *intersection* of `self` and `version_set`.

        Returns:
            The set representing the *intersection* of `self` and `version_set`.
        """
        raise NotImplementedError

    @required
    def union(self, version_set: VersionSet) -> VersionSet:
        """Computes the *union* of `self` and `version_set`.

        Returns:
            The set representing the *union* of `self` and `version_set`.
        """
        raise NotImplementedError

    @required
    def difference(self, version_set: VersionSet) -> VersionSet:
        """Computes the *difference* of `self` and `version_set`.

        Returns:
            The set representing the *difference* of `self` and `version_set`.
        """
        raise NotImplementedError

    @required
    def complement(self) -> VersionSet:
        """Computes the *complement* of `self`.

        Returns:
            The set representing the *complement* of `self`.
        """
        raise NotImplementedError

    def symmetric_difference(self, version_set: VersionSet) -> VersionSet:
        """Computes the *symmetric difference* of `self` and `version_set`.

        Equivalent to [`self.union(version_set).difference(self.intersection(version_set))`]
        [versions.version_sets.VersionSetProtocol.difference].

        Returns:
            The set representing the *symmetric difference* of `self` and `version_set`.
        """
        return self.union(version_set).difference(self.intersection(version_set))

    def __contains__(self, version: Version) -> bool:
        """Checks if the set contains some `version` via the *contains* (`in`) operation.

        Returns:
            Whether the `version` is contained within the set.
        """
        return self.contains(version)

    def __and__(self, version_set: VersionSet) -> VersionSet:
        """Computes the *intersection* of `self` and `version_set` via the *and* (`&`) operation.

        This is equivalent to [`self.intersection(version_set)`]
        [versions.version_sets.VersionSetProtocol.intersection].

        Returns:
            The set representing the *intersection* of `self` and `version_set`.
        """
        return self.intersection(version_set)

    def __iand__(self, version_set: VersionSet) -> VersionSet:
        """Computes the *intersection* of `self` and `version_set` via the *and-assign*
        (`&=`) operation.

        This is equivalent to [`self.intersection(version_set)`]
        [versions.version_sets.VersionSetProtocol.intersection].

        Returns:
            The set representing the *intersection* of `self` and `version_set`.
        """
        return self.intersection(version_set)

    def __or__(self, version_set: VersionSet) -> VersionSet:
        """Computes the *union* of `self` and `version_set` via the *or* (`|`) operation.

        This is equivalent to [`self.union(version_set)`]
        [versions.version_sets.VersionSetProtocol.union].

        Returns:
            The set representing the *union* of `self` and `version_set`.
        """
        return self.union(version_set)

    def __ior__(self, version_set: VersionSet) -> VersionSet:
        """Computes the *union* of `self` and `version_set` via the *or-assign* (`|=`) operation.

        This is equivalent to [`self.union(version_set)`]
        [versions.version_sets.VersionSetProtocol.union].

        Returns:
            The set representing the *union* of `self` and `version_set`.
        """
        return self.union(version_set)

    def __sub__(self, version_set: VersionSet) -> VersionSet:
        """Computes the *difference* of `self` and `version_set` via the *sub* (`-`) operation.

        This is equivalent to [`self.difference(version_set)`]
        [versions.version_sets.VersionSetProtocol.difference].

        Returns:
            The set representing the *difference* of `self` and `version_set`.
        """
        return self.difference(version_set)

    def __isub__(self, version_set: VersionSet) -> VersionSet:
        """Computes the *difference* of `self` and `version_set` via the *sub-assign*
        (`-=`) operation.

        This is equivalent to [`self.difference(version_set)`]
        [versions.version_sets.VersionSetProtocol.difference].

        Returns:
            The set representing the *difference* of `self` and `version_set`.
        """
        return self.difference(version_set)

    def __xor__(self, version_set: VersionSet) -> VersionSet:
        """Computes the *symmetric difference* of `self` and `version_set` via the *xor*
        (`^`) operation.

        This is equivalent to [`self.symmetric_difference(version_set)`]
        [versions.version_sets.VersionSetProtocol.symmetric_difference].

        Returns:
            The set representing the *symmetric difference* of `self` and `version_set`.
        """
        return self.symmetric_difference(version_set)

    def __ixor__(self, version_set: VersionSet) -> VersionSet:
        """Computes the *symmetric difference* of `self` and `version_set` via the *xor-assign*
        (`^=`) operation.

        This is equivalent to [`self.symmetric_difference(version_set)`]
        [versions.version_sets.VersionSetProtocol.symmetric_difference].

        Returns:
            The set representing the *symmetric difference* of `self` and `version_set`.
        """
        return self.symmetric_difference(version_set)

    def __negate__(self) -> VersionSet:
        """Computes the *complement* of `self` via the *negate* (`~`) operation.

        This is equivalent to [`self.complement()`]
        [versions.version_sets.VersionSetProtocol.complement].

        Returns:
            The set representing the *complement* of `self`.
        """
        return self.complement()

is_empty() -> bool

Checks if the set is empty.

Returns:

Type Description
bool

Whether the set is empty.

Source code in versions/version_sets.py
60
61
62
63
64
65
66
67
@required
def is_empty(self) -> bool:
    """Checks if the set is *empty*.

    Returns:
        Whether the set is *empty*.
    """
    raise NotImplementedError

is_universal() -> bool

Checks if the set is the universal.

Returns:

Type Description
bool

Whether the set is universal.

Source code in versions/version_sets.py
69
70
71
72
73
74
75
76
@required
def is_universal(self) -> bool:
    """Checks if the set is the *universal*.

    Returns:
        Whether the set is *universal*.
    """
    raise NotImplementedError

includes(version_set: VersionSet) -> bool

Checks if the set includes version_set.

Returns:

Type Description
bool

Whether the set includes version_set.

Source code in versions/version_sets.py
78
79
80
81
82
83
84
85
@required
def includes(self, version_set: VersionSet) -> bool:
    """Checks if the set includes `version_set`.

    Returns:
        Whether the set includes `version_set`.
    """
    raise NotImplementedError

intersects(version_set: VersionSet) -> bool

Checks if the set intersects version_set.

Returns:

Type Description
bool

Whether the set intersects version_set.

Source code in versions/version_sets.py
87
88
89
90
91
92
93
94
@required
def intersects(self, version_set: VersionSet) -> bool:
    """Checks if the set intersects `version_set`.

    Returns:
        Whether the set intersects `version_set`.
    """
    raise NotImplementedError

contains(version: Version) -> bool

Checks if the set contains some version.

Returns:

Type Description
bool

Whether the version is contained within the set.

Source code in versions/version_sets.py
 96
 97
 98
 99
100
101
102
103
@required
def contains(self, version: Version) -> bool:
    """Checks if the set contains some `version`.

    Returns:
        Whether the `version` is contained within the set.
    """
    raise NotImplementedError

accepts(version: Version) -> bool

Checks if the set contains some version.

This is an implementation of the accepts method of Specification protocol, equivalent to self.contains(version).

Returns:

Type Description
bool

Whether the version is accepted by the set.

Source code in versions/version_sets.py
105
106
107
108
109
110
111
112
113
114
115
def accepts(self, version: Version) -> bool:
    """Checks if the set contains some `version`.

    This is an implementation of the [`accepts`][versions.specification.Specification.accepts]
    method of [`Specification`][versions.specification.Specification] protocol, equivalent to
    [`self.contains(version)`][versions.version_sets.VersionSetProtocol.contains].

    Returns:
        Whether the `version` is accepted by the set.
    """
    return self.contains(version)

intersection(version_set: VersionSet) -> VersionSet

Computes the intersection of self and version_set.

Returns:

Type Description
VersionSet

The set representing the intersection of self and version_set.

Source code in versions/version_sets.py
117
118
119
120
121
122
123
124
@required
def intersection(self, version_set: VersionSet) -> VersionSet:
    """Computes the *intersection* of `self` and `version_set`.

    Returns:
        The set representing the *intersection* of `self` and `version_set`.
    """
    raise NotImplementedError

union(version_set: VersionSet) -> VersionSet

Computes the union of self and version_set.

Returns:

Type Description
VersionSet

The set representing the union of self and version_set.

Source code in versions/version_sets.py
126
127
128
129
130
131
132
133
@required
def union(self, version_set: VersionSet) -> VersionSet:
    """Computes the *union* of `self` and `version_set`.

    Returns:
        The set representing the *union* of `self` and `version_set`.
    """
    raise NotImplementedError

difference(version_set: VersionSet) -> VersionSet

Computes the difference of self and version_set.

Returns:

Type Description
VersionSet

The set representing the difference of self and version_set.

Source code in versions/version_sets.py
135
136
137
138
139
140
141
142
@required
def difference(self, version_set: VersionSet) -> VersionSet:
    """Computes the *difference* of `self` and `version_set`.

    Returns:
        The set representing the *difference* of `self` and `version_set`.
    """
    raise NotImplementedError

complement() -> VersionSet

Computes the complement of self.

Returns:

Type Description
VersionSet

The set representing the complement of self.

Source code in versions/version_sets.py
144
145
146
147
148
149
150
151
@required
def complement(self) -> VersionSet:
    """Computes the *complement* of `self`.

    Returns:
        The set representing the *complement* of `self`.
    """
    raise NotImplementedError

symmetric_difference(version_set: VersionSet) -> VersionSet

Computes the symmetric difference of self and version_set.

Equivalent to self.union(version_set).difference(self.intersection(version_set)).

Returns:

Type Description
VersionSet

The set representing the symmetric difference of self and version_set.

Source code in versions/version_sets.py
153
154
155
156
157
158
159
160
161
162
def symmetric_difference(self, version_set: VersionSet) -> VersionSet:
    """Computes the *symmetric difference* of `self` and `version_set`.

    Equivalent to [`self.union(version_set).difference(self.intersection(version_set))`]
    [versions.version_sets.VersionSetProtocol.difference].

    Returns:
        The set representing the *symmetric difference* of `self` and `version_set`.
    """
    return self.union(version_set).difference(self.intersection(version_set))

__contains__(version: Version) -> bool

Checks if the set contains some version via the contains (in) operation.

Returns:

Type Description
bool

Whether the version is contained within the set.

Source code in versions/version_sets.py
164
165
166
167
168
169
170
def __contains__(self, version: Version) -> bool:
    """Checks if the set contains some `version` via the *contains* (`in`) operation.

    Returns:
        Whether the `version` is contained within the set.
    """
    return self.contains(version)

__and__(version_set: VersionSet) -> VersionSet

Computes the intersection of self and version_set via the and (&) operation.

This is equivalent to self.intersection(version_set).

Returns:

Type Description
VersionSet

The set representing the intersection of self and version_set.

Source code in versions/version_sets.py
172
173
174
175
176
177
178
179
180
181
def __and__(self, version_set: VersionSet) -> VersionSet:
    """Computes the *intersection* of `self` and `version_set` via the *and* (`&`) operation.

    This is equivalent to [`self.intersection(version_set)`]
    [versions.version_sets.VersionSetProtocol.intersection].

    Returns:
        The set representing the *intersection* of `self` and `version_set`.
    """
    return self.intersection(version_set)

__iand__(version_set: VersionSet) -> VersionSet

Computes the intersection of self and version_set via the and-assign (&=) operation.

This is equivalent to self.intersection(version_set).

Returns:

Type Description
VersionSet

The set representing the intersection of self and version_set.

Source code in versions/version_sets.py
183
184
185
186
187
188
189
190
191
192
193
def __iand__(self, version_set: VersionSet) -> VersionSet:
    """Computes the *intersection* of `self` and `version_set` via the *and-assign*
    (`&=`) operation.

    This is equivalent to [`self.intersection(version_set)`]
    [versions.version_sets.VersionSetProtocol.intersection].

    Returns:
        The set representing the *intersection* of `self` and `version_set`.
    """
    return self.intersection(version_set)

__or__(version_set: VersionSet) -> VersionSet

Computes the union of self and version_set via the or (|) operation.

This is equivalent to self.union(version_set).

Returns:

Type Description
VersionSet

The set representing the union of self and version_set.

Source code in versions/version_sets.py
195
196
197
198
199
200
201
202
203
204
def __or__(self, version_set: VersionSet) -> VersionSet:
    """Computes the *union* of `self` and `version_set` via the *or* (`|`) operation.

    This is equivalent to [`self.union(version_set)`]
    [versions.version_sets.VersionSetProtocol.union].

    Returns:
        The set representing the *union* of `self` and `version_set`.
    """
    return self.union(version_set)

__ior__(version_set: VersionSet) -> VersionSet

Computes the union of self and version_set via the or-assign (|=) operation.

This is equivalent to self.union(version_set).

Returns:

Type Description
VersionSet

The set representing the union of self and version_set.

Source code in versions/version_sets.py
206
207
208
209
210
211
212
213
214
215
def __ior__(self, version_set: VersionSet) -> VersionSet:
    """Computes the *union* of `self` and `version_set` via the *or-assign* (`|=`) operation.

    This is equivalent to [`self.union(version_set)`]
    [versions.version_sets.VersionSetProtocol.union].

    Returns:
        The set representing the *union* of `self` and `version_set`.
    """
    return self.union(version_set)

__sub__(version_set: VersionSet) -> VersionSet

Computes the difference of self and version_set via the sub (-) operation.

This is equivalent to self.difference(version_set).

Returns:

Type Description
VersionSet

The set representing the difference of self and version_set.

Source code in versions/version_sets.py
217
218
219
220
221
222
223
224
225
226
def __sub__(self, version_set: VersionSet) -> VersionSet:
    """Computes the *difference* of `self` and `version_set` via the *sub* (`-`) operation.

    This is equivalent to [`self.difference(version_set)`]
    [versions.version_sets.VersionSetProtocol.difference].

    Returns:
        The set representing the *difference* of `self` and `version_set`.
    """
    return self.difference(version_set)

__isub__(version_set: VersionSet) -> VersionSet

Computes the difference of self and version_set via the sub-assign (-=) operation.

This is equivalent to self.difference(version_set).

Returns:

Type Description
VersionSet

The set representing the difference of self and version_set.

Source code in versions/version_sets.py
228
229
230
231
232
233
234
235
236
237
238
def __isub__(self, version_set: VersionSet) -> VersionSet:
    """Computes the *difference* of `self` and `version_set` via the *sub-assign*
    (`-=`) operation.

    This is equivalent to [`self.difference(version_set)`]
    [versions.version_sets.VersionSetProtocol.difference].

    Returns:
        The set representing the *difference* of `self` and `version_set`.
    """
    return self.difference(version_set)

__xor__(version_set: VersionSet) -> VersionSet

Computes the symmetric difference of self and version_set via the xor (^) operation.

This is equivalent to self.symmetric_difference(version_set).

Returns:

Type Description
VersionSet

The set representing the symmetric difference of self and version_set.

Source code in versions/version_sets.py
240
241
242
243
244
245
246
247
248
249
250
def __xor__(self, version_set: VersionSet) -> VersionSet:
    """Computes the *symmetric difference* of `self` and `version_set` via the *xor*
    (`^`) operation.

    This is equivalent to [`self.symmetric_difference(version_set)`]
    [versions.version_sets.VersionSetProtocol.symmetric_difference].

    Returns:
        The set representing the *symmetric difference* of `self` and `version_set`.
    """
    return self.symmetric_difference(version_set)

__ixor__(version_set: VersionSet) -> VersionSet

Computes the symmetric difference of self and version_set via the xor-assign (^=) operation.

This is equivalent to self.symmetric_difference(version_set).

Returns:

Type Description
VersionSet

The set representing the symmetric difference of self and version_set.

Source code in versions/version_sets.py
252
253
254
255
256
257
258
259
260
261
262
def __ixor__(self, version_set: VersionSet) -> VersionSet:
    """Computes the *symmetric difference* of `self` and `version_set` via the *xor-assign*
    (`^=`) operation.

    This is equivalent to [`self.symmetric_difference(version_set)`]
    [versions.version_sets.VersionSetProtocol.symmetric_difference].

    Returns:
        The set representing the *symmetric difference* of `self` and `version_set`.
    """
    return self.symmetric_difference(version_set)

__negate__() -> VersionSet

Computes the complement of self via the negate (~) operation.

This is equivalent to self.complement().

Returns:

Type Description
VersionSet

The set representing the complement of self.

Source code in versions/version_sets.py
264
265
266
267
268
269
270
271
272
273
def __negate__(self) -> VersionSet:
    """Computes the *complement* of `self` via the *negate* (`~`) operation.

    This is equivalent to [`self.complement()`]
    [versions.version_sets.VersionSetProtocol.complement].

    Returns:
        The set representing the *complement* of `self`.
    """
    return self.complement()

VersionEmpty

Bases: Representation, ToString, VersionSetProtocol

Represents empty version sets (0).

Source code in versions/version_sets.py
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
@frozen(repr=False, order=False)
class VersionEmpty(Representation, ToString, VersionSetProtocol):
    """Represents empty version sets (`0`)."""

    def is_empty(self) -> Literal[True]:
        return True

    def is_universal(self) -> Literal[False]:
        return False

    def includes(self, version_set: VersionSet) -> bool:
        return version_set.is_empty()

    def intersects(self, version_set: VersionSet) -> Literal[False]:
        return False

    def contains(self, version: Version) -> Literal[False]:
        return False

    def intersection(self, version_set: VersionSet) -> Self:
        return self

    def union(self, version_set: S) -> S:
        return version_set

    def difference(self, version_set: VersionSet) -> Self:
        return self

    def symmetric_difference(self, version_set: S) -> S:
        return version_set

    def complement(self) -> VersionRange:
        return UNIVERSAL_SET

    def to_string(self) -> str:
        return EMPTY_VERSION

VersionRange

Bases: Representation, ToString, VersionSetProtocol

Represents version ranges ((v, w), (v, w], [v, w) and [v, w]).

Source code in versions/version_sets.py
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
@frozen(repr=False, eq=False, order=False)
class VersionRange(Representation, ToString, VersionSetProtocol):
    """Represents version ranges (`(v, w)`, `(v, w]`, `[v, w)` and `[v, w]`)."""

    min: Optional[Version] = None
    max: Optional[Version] = None
    include_min: bool = False
    include_max: bool = False

    def __attrs_post_init__(self) -> None:
        if self.min is None and self.include_min:
            raise ValueError(CAN_NOT_INCLUDE_INFINITY)

        if self.max is None and self.include_max:
            raise ValueError(CAN_NOT_INCLUDE_INFINITY)

        if self.comparable_min > self.comparable_max:
            raise ValueError(MIN_MAX_CONSTRAINT)

    # range stuff

    @property
    def parameters(self) -> Tuple[Optional[Version], Optional[Version], bool, bool]:
        return (self.min, self.max, self.include_min, self.include_max)

    @property
    def exclude_min(self) -> bool:
        return not self.include_min

    @property
    def exclude_max(self) -> bool:
        return not self.include_max

    @property
    def comparable_min(self) -> Union[Version, NegativeInfinity]:
        min = self.min

        return negative_infinity if min is None else min

    @property
    def comparable_max(self) -> Union[Version, Infinity]:
        max = self.max

        return infinity if max is None else max

    def is_closed(self) -> bool:
        return self.is_left_closed() and self.is_right_closed()

    def is_left_closed(self) -> bool:
        return self.include_min

    def is_right_closed(self) -> bool:
        return self.include_max

    def is_open(self) -> bool:
        return self.is_left_open() and self.is_right_open()

    def is_left_open(self) -> bool:
        return self.exclude_min

    def is_right_open(self) -> bool:
        return self.exclude_max

    def is_unbounded(self) -> bool:
        return self.is_left_unbounded() and self.is_right_unbounded()

    def is_left_unbounded(self) -> bool:
        return self.min is None

    def is_right_unbounded(self) -> bool:
        return self.max is None

    def is_bounded(self) -> bool:
        return self.is_left_bounded() and self.is_right_bounded()

    def is_left_bounded(self) -> bool:
        return self.min is not None

    def is_right_bounded(self) -> bool:
        return self.max is not None

    def is_empty_or_point(self) -> bool:
        return self.comparable_min == self.comparable_max

    def is_lower(self, other: VersionRange) -> bool:
        self_comparable_min = self.comparable_min
        other_comparable_min = other.comparable_min

        if self_comparable_min < other_comparable_min:
            return True

        if self_comparable_min > other_comparable_min:
            return False

        return self.include_min and other.exclude_min

    def is_higher(self, other: VersionRange) -> bool:
        self_comparable_max = self.comparable_max
        other_comparable_max = other.comparable_max

        if self_comparable_max < other_comparable_max:
            return False

        if self_comparable_max > other_comparable_max:
            return True

        return self.include_max and other.exclude_max

    def is_strictly_lower(self, other: VersionRange) -> bool:
        self_comparable_max = self.comparable_max
        other_comparable_min = other.comparable_min

        if self_comparable_max < other_comparable_min:
            return True

        if self_comparable_max > other_comparable_min:
            return False

        return self.exclude_max or other.exclude_min

    def is_strictly_higher(self, other: VersionRange) -> bool:
        self_comparable_min = self.comparable_min
        other_comparable_max = other.comparable_max

        if self_comparable_min > other_comparable_max:
            return True

        if self_comparable_min < other_comparable_max:
            return False

        return self.exclude_min or other.exclude_max

    def is_left_adjacent(self, other: VersionRange) -> bool:
        return (self.max == other.min) and (self.include_max is other.exclude_min)

    def is_right_adjacent(self, other: VersionRange) -> bool:
        return (self.min == other.max) and (self.include_min is other.exclude_max)

    def is_adjacent(self, other: VersionRange) -> bool:
        return self.is_left_adjacent(other) or self.is_right_adjacent(other)

    def __hash__(self) -> int:
        return hash(self.parameters)

    def __eq__(self, other: Any) -> bool:
        return is_version_range(other) and self.parameters == other.parameters

    def __ne__(self, other: Any) -> bool:
        return not self.__eq__(other)

    def __lt__(self, other: VersionRange) -> bool:
        return self.compare(other).is_less()

    def __le__(self, other: VersionRange) -> bool:
        return self.compare(other).is_less_or_equal()

    def __gt__(self, other: VersionRange) -> bool:
        return self.compare(other).is_greater()

    def __ge__(self, other: VersionRange) -> bool:
        return self.compare(other).is_greater_or_equal()

    def compare(self, other: VersionRange) -> Ordering:
        self_comparable_min = self.comparable_min
        other_comparable_min = other.comparable_min

        if self_comparable_min > other_comparable_min:
            return Ordering.GREATER

        if self_comparable_min < other_comparable_min:
            return Ordering.LESS

        include_min = self.include_min

        if include_min == other.exclude_min:
            return Ordering.LESS if include_min else Ordering.GREATER

        self_comparable_max = self.comparable_max
        other_comparable_max = other.comparable_max

        if self_comparable_max > other_comparable_max:
            return Ordering.GREATER

        if self_comparable_max < other_comparable_max:
            return Ordering.LESS

        include_max = self.include_max

        if include_max == other.exclude_max:
            return Ordering.GREATER if include_max else Ordering.LESS

        return Ordering.EQUAL

    # protocol implementation

    def is_empty(self) -> bool:
        return self.is_empty_or_point() and not self.is_closed()

    def is_point(self) -> bool:
        return self.is_empty_or_point() and self.is_closed()

    def is_universal(self) -> bool:
        return self.is_unbounded()

    @property
    def version(self) -> Version:
        if self.is_point():
            return self.version_unchecked

        raise ValueError(RANGE_NOT_POINT)

    @property
    def version_unchecked(self) -> Version:
        version = self.min or self.max

        if version is None:  # we can not violate the type system
            raise ValueError(RANGE_NOT_POINT)

        return version

    def contains(self, version: Version) -> bool:
        comparable_min = self.comparable_min
        comparable_max = self.comparable_max

        if version < comparable_min:
            return False

        if self.exclude_min and version == comparable_min:
            return False

        if version > comparable_max:
            return False

        if self.exclude_max and version == comparable_max:
            return False

        return True

    accepts = contains

    def includes(self, version_set: VersionSet) -> bool:
        if is_version_empty(version_set):
            return True

        if is_version_point(version_set):
            return self.contains(version_set.version)

        if is_version_range(version_set):
            return not version_set.is_lower(self) and not version_set.is_higher(self)

        if is_version_union(version_set):
            return all(self.includes(item) for item in version_set.items)

        raise unexpected_version_set(version_set)

    def intersects(self, version_set: VersionSet) -> bool:
        if is_version_empty(version_set):
            return False

        if is_version_point(version_set):
            return self.contains(version_set.version)

        if is_version_range(version_set):
            return self.intersects_range(version_set)

        if is_version_union(version_set):
            return any(self.intersects(item) for item in version_set.items)

        raise unexpected_version_set(version_set)

    def intersects_range(self, range: VersionRange) -> bool:
        return not range.is_strictly_lower(self) and not range.is_strictly_higher(self)

    def intersection(self, version_set: VersionSet) -> VersionSet:
        if is_version_empty(version_set):
            return EMPTY_SET

        if is_version_point(version_set):
            return version_set.intersection(self)

        if is_version_range(version_set):
            if self.is_lower(version_set):
                if self.is_strictly_lower(version_set):
                    return EMPTY_SET

                intersection_min = version_set.min
                intersection_include_min = version_set.include_min

            else:
                if self.is_strictly_higher(version_set):
                    return EMPTY_SET

                intersection_min = self.min
                intersection_include_min = self.include_min

            if self.is_higher(version_set):
                intersection_max = version_set.max
                intersection_include_max = version_set.include_max

            else:
                intersection_max = self.max
                intersection_include_max = self.include_max

            # if we reached here, there is an actual range
            intersection = VersionRange(
                intersection_min,
                intersection_max,
                intersection_include_min,
                intersection_include_max,
            )

            if intersection.is_point():
                return VersionPoint(intersection.version)

            return intersection

        if is_version_union(version_set):
            return version_set.intersection(self)

        raise unexpected_version_set(version_set)

    def union(self, version_set: VersionSet) -> VersionSet:
        if is_version_empty(version_set):
            return self

        if is_version_point(version_set):
            version = version_set.version

            if self.contains(version):
                return self

            if version == self.min:
                return evolve(self, include_min=True)

            if version == self.max:
                return evolve(self, include_max=True)

            return VersionUnion.of(self, version_set)

        if is_version_range(version_set):
            if not self.is_adjacent(version_set) and not self.intersects(version_set):
                return VersionUnion.of(self, version_set)

            if self.is_lower(version_set):
                union_min = self.min
                union_include_min = self.include_min

            else:
                union_min = version_set.min
                union_include_min = version_set.include_min

            if self.is_higher(version_set):
                union_max = self.max
                union_include_max = self.include_max

            else:
                union_max = version_set.max
                union_include_max = version_set.include_max

            return VersionRange(
                union_min,
                union_max,
                union_include_min,
                union_include_max,
            )

        if is_version_union(version_set):
            return version_set.union(self)

        raise unexpected_version_set(version_set)

    def difference(self, version_set: VersionSet) -> VersionSet:
        if is_version_empty(version_set):
            return self

        if is_version_point(version_set):
            version = version_set.version

            if not self.contains(version):
                return self

            if version == self.min:
                if self.exclude_min:
                    return self

                return evolve(self, include_min=False)

            if version == self.max:
                if self.exclude_max:
                    return self

                return evolve(self, include_max=False)

            return VersionUnion.of(
                evolve(self, max=version, include_max=False),
                evolve(self, min=version, include_min=False),
            )

        if is_version_range(version_set):
            if not self.intersects(version_set):
                return self

            before: Optional[VersionItem]

            if not self.is_lower(version_set):
                before = None

            elif self.min == version_set.min:
                before = VersionPoint(self.min)  # type: ignore

            else:
                before = evolve(self, max=version_set.min, include_max=version_set.exclude_min)

            after: Optional[VersionItem]

            if not self.is_higher(version_set):
                after = None

            elif self.max == version_set.max:
                after = VersionPoint(self.max)  # type: ignore

            else:
                after = evolve(self, min=version_set.max, include_min=version_set.exclude_max)

            if before is None:
                if after is None:
                    return EMPTY_SET

                return after

            if after is None:
                if before is None:
                    return EMPTY_SET

                return before

            return VersionUnion.of(before, after)

        if is_version_union(version_set):
            return VersionUnion.of_iterable(self.difference_iterator(version_set))

        raise unexpected_version_set(version_set)

    def difference_iterator(self, version_union: VersionUnion) -> Iterator[VersionItem]:
        current: VersionItem = self

        for item in version_union.items:
            if item.is_strictly_lower(current):
                continue

            if item.is_strictly_higher(current):
                break

            difference = current.difference(item)

            if is_version_union(difference):
                item, current = difference.items

                yield item

            if is_version_item(difference):
                current = difference

        yield current

    def complement(self) -> VersionSet:
        return UNIVERSAL_SET.difference(self)

    def to_string_iterator(self) -> Iterator[str]:
        if self.is_empty():
            yield EMPTY_VERSION
            return

        if self.is_point():
            yield self.version.to_string()
            return

        if self.is_universal():
            yield UNIVERSE_VERSION
            return

        min = self.min

        if min is not None:
            min_operator = (
                Operator.greater_or_equal(min) if self.include_min else Operator.greater(min)
            )

            yield min_operator.to_string()

        max = self.max

        if max is not None:
            max_operator = Operator.less_or_equal(max) if self.include_max else Operator.less(max)

            yield max_operator.to_string()

    def to_short_string_iterator(self) -> Iterator[str]:
        if self.is_empty():
            yield EMPTY_VERSION
            return

        if self.is_point():
            yield self.version.to_short_string()
            return

        if self.is_universal():
            yield UNIVERSE_VERSION
            return

        min = self.min

        if min is not None:
            min_operator = (
                Operator.greater_or_equal(min) if self.include_min else Operator.greater(min)
            )

            yield min_operator.to_short_string()

        max = self.max

        if max is not None:
            max_operator = Operator.less_or_equal(max) if self.include_max else Operator.less(max)

            yield max_operator.to_short_string()

    def to_string(self) -> str:
        return concat_comma_space(self.to_string_iterator())

    def to_short_string(self) -> str:
        return concat_comma(self.to_short_string_iterator())

VersionPoint

Bases: VersionRange

Represents version points ([v, v] ranges, also known as singleton sets {v}).

Source code in versions/version_sets.py
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
@frozen(repr=False, eq=False, order=False)
class VersionPoint(VersionRange):
    """Represents version points (`[v, v]` ranges, also known as singleton sets `{v}`)."""

    version: Version = field()

    # initialize range fields accordingly

    min: Version = field(init=False)
    max: Version = field(init=False)

    include_min: Literal[True] = field(default=True, init=False)
    include_max: Literal[True] = field(default=True, init=False)

    @min.default
    def default_min(self) -> Version:
        return self.version

    @max.default
    def default_max(self) -> Version:
        return self.version

    def is_empty(self) -> bool:
        return False

    def is_point(self) -> bool:
        return True

    def is_universal(self) -> bool:
        return False

    def contains(self, version: Version) -> bool:
        # version = self.version.weaken(version)
        return self.version == version

    accepts = contains

    def includes(self, version_set: VersionSet) -> bool:
        return version_set.is_empty() or (
            is_version_point(version_set) and self.contains(version_set.version)
        )

    def intersects(self, version_set: VersionSet) -> bool:
        return version_set.contains(self.version)

    def intersection(self, version_set: VersionSet) -> VersionSet:
        return self if version_set.contains(self.version) else EMPTY_SET

    def union(self, version_set: VersionSet) -> VersionSet:
        if is_version_empty(version_set):
            return self

        if is_version_point(version_set):
            if self.contains(version_set.version):
                return self

            return VersionUnion.of(self, version_set)

        if version_set.contains(self.version):
            return version_set

        if is_version_range(version_set) or is_version_union(version_set):
            return VersionUnion.of(self, version_set)

        raise unexpected_version_set(version_set)

    def difference(self, version_set: VersionSet) -> VersionSet:
        return EMPTY_SET if version_set.contains(self.version) else self

    def complement(self) -> VersionSet:
        return UNIVERSAL_SET.difference(self)

    def to_string(self) -> str:
        return self.version.to_string()

    def to_short_string(self) -> str:
        return self.version.to_short_string()

VersionUnion

Bases: Representation, ToString, Specification

Represents version unions.

Source code in versions/version_sets.py
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
@frozen(repr=False, order=False)
class VersionUnion(Representation, ToString, Specification):
    """Represents version unions."""

    items: VersionItems = field()

    @items.validator
    def check_items(self, attribute: Attribute[VersionItems], items: VersionItems) -> None:
        check_items(items)

    @classmethod
    def of_unchecked(cls: Type[U], *items: VersionItem) -> U:
        return cls(items)

    @classmethod
    def of_iterable_unchecked(cls: Type[U], items: Iterable[VersionItem]) -> U:
        return cls(tuple(items))

    @staticmethod
    def extract(version_set: VersionSet) -> Iterator[VersionItem]:
        if is_version_union(version_set):
            yield from version_set.items

        if is_version_item(version_set):
            yield version_set

    @classmethod
    def merge(cls, iterable: Iterable[VersionSet]) -> VersionSet:
        extracted = list(flatten(map(cls.extract, iterable)))

        if not extracted:
            return EMPTY_SET

        if any(item.is_universal() for item in extracted):
            return UNIVERSAL_SET

        extracted.sort()  # since ranges and points are ordered

        merged: List[VersionItem] = []

        for item in extracted:
            if not merged:  # nothing to merge yet
                merged.append(item)

            else:
                last_item = last(merged)

                if last_item.intersects(item) or last_item.is_adjacent(item):
                    result = last_item.union(item)

                    if is_version_item(result):
                        set_last(merged, result)

                    else:
                        raise InternalError(UNEXPECTED_UNION)

                else:
                    merged.append(item)

        if contains_only_item(merged):
            return first(merged)

        return cls.of_iterable_unchecked(merged)

    @classmethod
    def of(cls, *version_sets: VersionSet) -> VersionSet:
        return cls.of_iterable(version_sets)

    @classmethod
    def of_iterable(cls, version_sets: Iterable[VersionSet]) -> VersionSet:
        return cls.merge(version_sets)

    @property
    def exclude_version(self) -> Optional[Version]:
        complement = self.complement()

        return complement.version if is_version_point(complement) else None

    def is_empty(self) -> bool:
        return False

    def is_universal(self) -> bool:
        return False

    def contains(self, version: Version) -> bool:
        return any(item.contains(version) for item in self.items)

    accepts = contains

    def includes(self, version_set: VersionSet) -> bool:
        self_items = iter(self.items)
        items = self.extract(version_set)

        self_item = next_or_none(self_items)
        item = next_or_none(items)

        while self_item and item:
            if self_item.includes(item):
                item = next_or_none(items)

            else:
                self_item = next_or_none(self_items)

        return item is None  # all items are covered

    def intersects(self, version_set: VersionSet) -> bool:
        self_items = iter(self.items)
        items = self.extract(version_set)

        self_item = next_or_none(self_items)
        item = next_or_none(items)

        while self_item and item:
            if self_item.intersects(item):
                return True

            if item.is_higher(self_item):
                self_item = next_or_none(self_items)

            else:
                item = next_or_none(items)

        return False  # none of the items are allowed

    def intersection_iterator(self, version_set: VersionSet) -> Iterator[VersionItem]:
        self_items = iter(self.items)
        items = self.extract(version_set)

        self_item = next_or_none(self_items)
        item = next_or_none(items)

        while self_item and item:
            intersection = self_item.intersection(item)

            if is_version_item(intersection):
                yield intersection

            if item.is_higher(self_item):
                self_item = next_or_none(self_items)

            else:
                item = next_or_none(items)

    def intersection(self, version_set: VersionSet) -> VersionSet:
        return self.of_iterable(self.intersection_iterator(version_set))

    def union(self, version_set: VersionSet) -> VersionSet:
        return self.of(self, version_set)

    def difference(self, version_set: VersionSet) -> VersionSet:
        items_difference = ItemsDifference(iter(self.items), self.extract(version_set))
        return self.of_iterable(items_difference.compute())

    def complement(self) -> VersionSet:
        return UNIVERSAL_SET.difference(self)

    def to_string(self) -> str:
        exclude_version = self.exclude_version

        if exclude_version:
            operator = Operator(OperatorType.NOT_EQUAL, exclude_version)

            return operator.to_string()

        return concat_pipes_spaced(item.to_string() for item in self.items)

    def to_short_string(self) -> str:
        exclude_version = self.exclude_version

        if exclude_version:
            operator = Operator(OperatorType.NOT_EQUAL, exclude_version)

            return operator.to_short_string()

        return concat_pipes(item.to_short_string() for item in self.items)

is_version_empty(item: Any) -> TypeGuard[VersionEmpty]

Checks if an item is an instance of VersionEmpty.

Returns:

Type Description
TypeGuard[VersionEmpty]

Whether the item provided is an instance of VersionEmpty.

Source code in versions/version_sets.py
276
277
278
279
280
281
282
283
def is_version_empty(item: Any) -> TypeGuard[VersionEmpty]:
    """Checks if an `item` is an instance of [`VersionEmpty`][versions.version_sets.VersionEmpty].

    Returns:
        Whether the `item` provided is an instance
            of [`VersionEmpty`][versions.version_sets.VersionEmpty].
    """
    return is_instance(item, VersionEmpty)

is_version_point(item: Any) -> TypeGuard[VersionPoint]

Checks if an item is an instance of VersionPoint.

Returns:

Type Description
TypeGuard[VersionPoint]

Whether the item provided is an instance of VersionPoint.

Source code in versions/version_sets.py
286
287
288
289
290
291
292
293
def is_version_point(item: Any) -> TypeGuard[VersionPoint]:
    """Checks if an `item` is an instance of [`VersionPoint`][versions.version_sets.VersionPoint].

    Returns:
        Whether the `item` provided is an instance
            of [`VersionPoint`][versions.version_sets.VersionPoint].
    """
    return is_instance(item, VersionPoint)

is_version_range(item: Any) -> TypeGuard[VersionRange]

Checks if an item is an instance of VersionRange.

Returns:

Type Description
TypeGuard[VersionRange]

Whether the item provided is an instance of VersionRange.

Source code in versions/version_sets.py
296
297
298
299
300
301
302
303
def is_version_range(item: Any) -> TypeGuard[VersionRange]:
    """Checks if an `item` is an instance of [`VersionRange`][versions.version_sets.VersionRange].

    Returns:
        Whether the `item` provided is an instance
            of [`VersionRange`][versions.version_sets.VersionRange].
    """
    return is_instance(item, VersionRange)

is_version_union(item: Any) -> TypeGuard[VersionUnion]

Checks if an item is an instance of VersionUnion.

Returns:

Type Description
TypeGuard[VersionUnion]

Whether the item provided is an instance of VersionUnion.

Source code in versions/version_sets.py
306
307
308
309
310
311
312
313
def is_version_union(item: Any) -> TypeGuard[VersionUnion]:
    """Checks if an `item` is an instance of [`VersionUnion`][versions.version_sets.VersionUnion].

    Returns:
        Whether the `item` provided is an instance
            of [`VersionUnion`][versions.version_sets.VersionUnion].
    """
    return is_instance(item, VersionUnion)

is_version_item(item: Any) -> TypeGuard[VersionItem]

Checks if an item is an instance of VersionItem.

Returns:

Type Description
TypeGuard[VersionItem]

Whether the item provided is an instance of VersionItem.

Source code in versions/version_sets.py
316
317
318
319
320
321
322
323
def is_version_item(item: Any) -> TypeGuard[VersionItem]:
    """Checks if an `item` is an instance of [`VersionItem`][versions.version_sets.VersionItem].

    Returns:
        Whether the `item` provided is an instance
            of [`VersionItem`][versions.version_sets.VersionItem].
    """
    return is_instance(item, VersionItemTypes)

is_version_set(item: Any) -> TypeGuard[VersionSet]

Checks if an item is an instance of VersionSet.

Returns:

Type Description
TypeGuard[VersionSet]

Whether the item provided is an instance of VersionSet.

Source code in versions/version_sets.py
326
327
328
329
330
331
332
333
def is_version_set(item: Any) -> TypeGuard[VersionSet]:
    """Checks if an `item` is an instance of [`VersionSet`][versions.version_sets.VersionSet].

    Returns:
        Whether the `item` provided is an instance
            of [`VersionSet`][versions.version_sets.VersionSet].
    """
    return is_instance(item, VersionSetTypes)