Skip to content

Commit 84c6504

Browse files
authored
fix: tuple stuff (#461)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent ac6ad0e commit 84c6504

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

cyclonedx/model/__init__.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import warnings
1919
from datetime import datetime, timezone
2020
from enum import Enum
21+
from itertools import zip_longest
2122
from typing import Any, Iterable, Optional, Tuple, TypeVar
2223

2324
import serializable
@@ -72,31 +73,27 @@ class ComparableTuple(Tuple[Optional[_T], ...]):
7273
"""
7374

7475
def __lt__(self, other: Any) -> bool:
75-
for s, o in zip(self, other):
76+
for s, o in zip_longest(self, other):
7677
if s == o:
7778
continue
79+
# the idea is to have any consistent order, not necessarily "natural" order.
7880
if s is None:
7981
return False
8082
if o is None:
8183
return True
82-
if s < o:
83-
return True
84-
if s > o:
85-
return False
84+
return True if s < o else False
8685
return False
8786

8887
def __gt__(self, other: Any) -> bool:
89-
for s, o in zip(self, other):
88+
for s, o in zip_longest(self, other):
9089
if s == o:
9190
continue
91+
# the idea is to have any consistent order, not necessarily "natural" order.
9292
if s is None:
9393
return True
9494
if o is None:
9595
return False
96-
if s < o:
97-
return False
98-
if s > o:
99-
return True
96+
return True if s > o else False
10097
return False
10198

10299

cyclonedx/model/vulnerability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class VulnerabilitySeverity(str, Enum):
561561
UNKNOWN = 'unknown'
562562

563563
@staticmethod
564-
def get_from_cvss_scores(scores: Union[Tuple[float], float, None]) -> 'VulnerabilitySeverity':
564+
def get_from_cvss_scores(scores: Union[Tuple[float, ...], float, None]) -> 'VulnerabilitySeverity':
565565
"""
566566
Derives the Severity of a Vulnerability from it's declared CVSS scores.
567567

tests/test_model.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def test_compare_last_item_none(self) -> None:
132132
self.assertNotEqual(tuple1, tuple2)
133133
self.assertNotEqual(tuple2, tuple1)
134134

135+
def test_compare_last_item_missing(self) -> None:
136+
tuple1 = ComparableTuple((1, 2, 3, 4, 5))
137+
tuple2 = ComparableTuple((1, 2, 3, 4))
138+
self.assertLess(tuple1, tuple2)
139+
self.assertGreater(tuple2, tuple1)
140+
self.assertNotEqual(tuple1, tuple2)
141+
self.assertNotEqual(tuple2, tuple1)
142+
135143
def test_compare_enum(self) -> None:
136144
tuple1 = ComparableTuple((DummyStringEnum.FIRST, ))
137145
tuple2 = ComparableTuple((DummyStringEnum.SECOND, ))

tests/test_model_component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def test_sort(self) -> None:
427427
]
428428

429429
# expected sort order: (type, [diff], sorted(resolves))
430-
expected_order = [5, 4, 2, 3, 1, 0]
430+
expected_order = [5, 4, 3, 2, 1, 0]
431431
patches = [
432432
Patch(type=PatchClassification.MONKEY),
433433
Patch(type=PatchClassification.MONKEY, diff=diff_b),

0 commit comments

Comments
 (0)