Skip to content

Commit f6b39f1

Browse files
committed
wip
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 712918e commit f6b39f1

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

cyclonedx/model/definition.py

+23-15
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,27 @@ def __init__(
5353
self.owner = owner
5454
self.external_references = external_references or [] # type:ignore[assignment]
5555

56+
@staticmethod
57+
def __comparable_tuple(o: 'Standard') -> _ComparableTuple:
58+
return _ComparableTuple((
59+
o.bom_ref,
60+
o.name, o.version,
61+
o.description, o.owner,
62+
_ComparableTuple(o.external_references)
63+
))
64+
5665
def __lt__(self, other: Any) -> bool:
5766
if isinstance(other, Standard):
58-
return (_ComparableTuple((self.bom_ref, self.name, self.version))
59-
< _ComparableTuple((other.bom_ref, other.name, other.version)))
67+
return self.__comparable_tuple(self) < self.__comparable_tuple(other)
6068
return NotImplemented
6169

6270
def __eq__(self, other: object) -> bool:
6371
if isinstance(other, Standard):
64-
return hash(other) == hash(self)
72+
return self.__comparable_tuple(self) == self.__comparable_tuple(other)
6573
return False
6674

6775
def __hash__(self) -> int:
68-
return hash((
69-
self.bom_ref, self.name, self.version, self.description, self.owner, tuple(self.external_references)
70-
))
76+
return hash(self.__comparable_tuple(self))
7177

7278
def __repr__(self) -> str:
7379
return f'<Standard bom-ref={self.bom_ref}, name={self.name}, version={self.version}, ' \
@@ -212,20 +218,22 @@ def standards(self, standards: Iterable[Standard]) -> None:
212218
def __bool__(self) -> bool:
213219
return len(self._standards) > 0
214220

215-
def __eq__(self, other: object) -> bool:
216-
if not isinstance(other, Definitions):
217-
return False
218-
219-
return self._standards == other._standards
221+
@staticmethod
222+
def __comparable_tuple(o: 'Definitions') -> _ComparableTuple:
223+
return _ComparableTuple(o._standards)
220224

221-
def __hash__(self) -> int:
222-
return hash((tuple(self._standards)))
225+
def __eq__(self, other: object) -> bool:
226+
if isinstance(other, Definitions):
227+
return self.__comparable_tuple(self) == self.__comparable_tuple(other)
228+
return False
223229

224230
def __lt__(self, other: Any) -> bool:
225231
if isinstance(other, Definitions):
226-
return (_ComparableTuple(self._standards)
227-
< _ComparableTuple(other.standards))
232+
return self.__comparable_tuple(self) < self.__comparable_tuple(other)
228233
return NotImplemented
229234

235+
def __hash__(self) -> int:
236+
return hash(self.__comparable_tuple(self))
237+
230238
def __repr__(self) -> str:
231239
return '<Definitions>'

cyclonedx/model/service.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -354,26 +354,28 @@ def release_notes(self) -> Optional[ReleaseNotes]:
354354
def release_notes(self, release_notes: Optional[ReleaseNotes]) -> None:
355355
self._release_notes = release_notes
356356

357+
@staticmethod
358+
def __comparable_tuple(o: 'Service') -> _ComparableTuple:
359+
return _ComparableTuple((
360+
o.bom_ref, # see https://github.com/CycloneDX/cyclonedx-python-lib/issues/753
361+
o.authenticated, _ComparableTuple(o.data), o.description, _ComparableTuple(o.endpoints),
362+
_ComparableTuple(o.external_references), o.group, _ComparableTuple(o.licenses), o.name,
363+
_ComparableTuple(o.properties), o.provider, o.release_notes, _ComparableTuple(o.services), o.version,
364+
o.x_trust_boundary
365+
))
366+
357367
def __eq__(self, other: object) -> bool:
358368
if isinstance(other, Service):
359-
return hash(other) == hash(self)
369+
return self.__comparable_tuple(self) == self.__comparable_tuple(other)
360370
return False
361371

362372
def __lt__(self, other: Any) -> bool:
363373
if isinstance(other, Service):
364-
return _ComparableTuple((
365-
self.group, self.name, self.version
366-
)) < _ComparableTuple((
367-
other.group, other.name, other.version
368-
))
374+
return self.__comparable_tuple(self) < self.__comparable_tuple(other)
369375
return NotImplemented
370376

371377
def __hash__(self) -> int:
372-
return hash((
373-
self.authenticated, tuple(self.data), self.description, tuple(self.endpoints),
374-
tuple(self.external_references), self.group, tuple(self.licenses), self.name, tuple(self.properties),
375-
self.provider, self.release_notes, tuple(self.services), self.version, self.x_trust_boundary
376-
))
378+
return hash(self.__comparable_tuple(self))
377379

378380
def __repr__(self) -> str:
379381
return f'<Service bom-ref={self.bom_ref}, group={self.group}, name={self.name}, version={self.version}>'

cyclonedx/model/vulnerability.py

+9
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,15 @@ def properties(self) -> 'SortedSet[Property]':
13081308
def properties(self, properties: Iterable[Property]) -> None:
13091309
self._properties = SortedSet(properties)
13101310

1311+
@staticmethod
1312+
def __comparable_tuple(o: 'Vulnerability') -> _ComparableTuple:
1313+
return _ComparableTuple((
1314+
o.bom_ref, o.id, o.description,
1315+
o.source, _ComparableTuple(o.references), _ComparableTuple(o.ratings), _ComparableTuple(o.cwes),
1316+
o.detail, o.recommendation, o.workaround, _ComparableTuple(o.advisories), o.created, o.published,
1317+
o.updated, o.credits, o.tools, o.analysis, _ComparableTuple(o.affects), _ComparableTuple(o.properties)
1318+
))
1319+
13111320
def __eq__(self, other: object) -> bool:
13121321
if isinstance(other, Vulnerability):
13131322
return hash(other) == hash(self)

0 commit comments

Comments
 (0)