Skip to content

Commit a3ea870

Browse files
committed
fix: tuple stuff
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 5d7b86c commit a3ea870

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

cyclonedx/model/__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import warnings
2121
from datetime import datetime, timezone
2222
from enum import Enum
23+
from itertools import zip_longest
2324
from typing import Any, Iterable, Optional, Tuple, TypeVar
2425

2526
import serializable
@@ -74,31 +75,31 @@ class ComparableTuple(Tuple[Optional[_T], ...]):
7475
"""
7576

7677
def __lt__(self, other: Any) -> bool:
77-
for s, o in zip(self, other):
78+
if not isinstance(other, tuple):
79+
return NotImplemented
80+
for s, o in zip_longest(self, other):
7881
if s == o:
7982
continue
83+
# the idea is to have any consistent comparability, not natural order...
8084
if s is None:
8185
return False
8286
if o is None:
8387
return True
84-
if s < o:
85-
return True
86-
if s > o:
87-
return False
88+
return s < o
8889
return False
8990

9091
def __gt__(self, other: Any) -> bool:
91-
for s, o in zip(self, other):
92+
if not isinstance(other, tuple):
93+
return NotImplemented
94+
for s, o in zip_longest(self, other):
9295
if s == o:
9396
continue
97+
# the idea is to have any consistent comparability, not natural order...
9498
if s is None:
9599
return True
96100
if o is None:
97101
return False
98-
if s < o:
99-
return False
100-
if s > o:
101-
return True
102+
return s > o
102103
return False
103104

104105

cyclonedx/model/vulnerability.py

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

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

tests/test_model.py

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

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

0 commit comments

Comments
 (0)