Skip to content

fix: tuple comparison #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions cyclonedx/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import warnings
from datetime import datetime, timezone
from enum import Enum
from itertools import zip_longest
from typing import Any, Iterable, Optional, Tuple, TypeVar

import serializable
Expand Down Expand Up @@ -74,31 +75,27 @@ class ComparableTuple(Tuple[Optional[_T], ...]):
"""

def __lt__(self, other: Any) -> bool:
for s, o in zip(self, other):
for s, o in zip_longest(self, other):
if s == o:
continue
# the idea is to have any consistent order, not necessarily "natural" order.
if s is None:
return False
if o is None:
return True
if s < o:
return True
if s > o:
return False
return True if s < o else False
return False

def __gt__(self, other: Any) -> bool:
for s, o in zip(self, other):
for s, o in zip_longest(self, other):
if s == o:
continue
# the idea is to have any consistent order, not necessarily "natural" order.
if s is None:
return True
if o is None:
return False
if s < o:
return False
if s > o:
return True
return True if s > o else False
return False


Expand Down
2 changes: 1 addition & 1 deletion cyclonedx/model/vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ class VulnerabilitySeverity(str, Enum):
UNKNOWN = 'unknown'

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

Expand Down
8 changes: 8 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ def test_compare_last_item_none(self) -> None:
self.assertNotEqual(tuple1, tuple2)
self.assertNotEqual(tuple2, tuple1)

def test_compare_last_item_missing(self) -> None:
tuple1 = ComparableTuple((1, 2, 3, 4, 5))
tuple2 = ComparableTuple((1, 2, 3, 4))
self.assertLess(tuple1, tuple2)
self.assertGreater(tuple2, tuple1)
self.assertNotEqual(tuple1, tuple2)
self.assertNotEqual(tuple2, tuple1)

def test_compare_enum(self) -> None:
tuple1 = ComparableTuple((DummyStringEnum.FIRST, ))
tuple2 = ComparableTuple((DummyStringEnum.SECOND, ))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_model_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def test_sort(self) -> None:
]

# expected sort order: (type, [diff], sorted(resolves))
expected_order = [5, 4, 2, 3, 1, 0]
expected_order = [5, 4, 3, 2, 1, 0]
patches = [
Patch(type=PatchClassification.MONKEY),
Patch(type=PatchClassification.MONKEY, diff=diff_b),
Expand Down