Skip to content

Commit c47afc4

Browse files
committed
wip
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 43a427d commit c47afc4

File tree

5 files changed

+77
-75
lines changed

5 files changed

+77
-75
lines changed

cyclonedx/model/__init__.py

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,23 @@ def classification(self) -> str:
133133
def classification(self, classification: str) -> None:
134134
self._classification = classification
135135

136+
def __comparable_tuple(self) -> _ComparableTuple:
137+
return _ComparableTuple((
138+
self.flow, self.classification
139+
))
140+
136141
def __eq__(self, other: object) -> bool:
137142
if isinstance(other, DataClassification):
138-
return hash(other) == hash(self)
143+
return self.__comparable_tuple() == other.__comparable_tuple()
139144
return False
140145

141146
def __lt__(self, other: object) -> bool:
142147
if isinstance(other, DataClassification):
143-
return _ComparableTuple((
144-
self.flow, self.classification
145-
)) < _ComparableTuple((
146-
other.flow, other.classification
147-
))
148+
return self.__comparable_tuple() < other.__comparable_tuple()
148149
return NotImplemented
149150

150151
def __hash__(self) -> int:
151-
# TODO
152-
return hash((self.flow, self.classification))
152+
return hash(self.__comparable_tuple())
153153

154154
def __repr__(self) -> str:
155155
return f'<DataClassification flow={self.flow}>'
@@ -237,23 +237,23 @@ def content(self) -> str:
237237
def content(self, content: str) -> None:
238238
self._content = content
239239

240+
def __comparable_tuple(self) -> _ComparableTuple:
241+
return _ComparableTuple((
242+
self.content_type, self.content, self.encoding
243+
))
244+
240245
def __eq__(self, other: object) -> bool:
241246
if isinstance(other, AttachedText):
242-
return hash(other) == hash(self)
247+
return self.__comparable_tuple() == other.__comparable_tuple()
243248
return False
244249

245250
def __lt__(self, other: Any) -> bool:
246251
if isinstance(other, AttachedText):
247-
return _ComparableTuple((
248-
self.content_type, self.content, self.encoding
249-
)) < _ComparableTuple((
250-
other.content_type, other.content, other.encoding
251-
))
252+
return self.__comparable_tuple() < other.__comparable_tuple()
252253
return NotImplemented
253254

254255
def __hash__(self) -> int:
255-
# TODO
256-
return hash((self.content, self.content_type, self.encoding))
256+
return hash(self.__comparable_tuple())
257257

258258
def __repr__(self) -> str:
259259
return f'<AttachedText content-type={self.content_type}, encoding={self.encoding}>'
@@ -517,23 +517,23 @@ def content(self) -> str:
517517
def content(self, content: str) -> None:
518518
self._content = content
519519

520+
def __comparable_tuple(self) -> _ComparableTuple:
521+
return _ComparableTuple((
522+
self.alg, self.content
523+
))
524+
520525
def __eq__(self, other: object) -> bool:
521526
if isinstance(other, HashType):
522-
return hash(other) == hash(self)
527+
return self.__comparable_tuple() == other.__comparable_tuple()
523528
return False
524529

525530
def __lt__(self, other: Any) -> bool:
526531
if isinstance(other, HashType):
527-
return _ComparableTuple((
528-
self.alg, self.content
529-
)) < _ComparableTuple((
530-
other.alg, other.content
531-
))
532+
return self.__comparable_tuple() < other.__comparable_tuple()
532533
return NotImplemented
533534

534535
def __hash__(self) -> int:
535-
# TODO
536-
return hash((self.alg, self.content))
536+
return hash(self.__comparable_tuple())
537537

538538
def __repr__(self) -> str:
539539
return f'<HashType {self.alg.name}:{self.content}>'
@@ -895,26 +895,24 @@ def hashes(self) -> 'SortedSet[HashType]':
895895
def hashes(self, hashes: Iterable[HashType]) -> None:
896896
self._hashes = SortedSet(hashes)
897897

898+
def __comparable_tuple(self) -> _ComparableTuple:
899+
return _ComparableTuple((
900+
self._type, self._url, self._comment,
901+
_ComparableTuple(sorted(self._hashes, key=hash))
902+
))
903+
898904
def __eq__(self, other: object) -> bool:
899905
if isinstance(other, ExternalReference):
900-
return hash(other) == hash(self)
906+
return self.__comparable_tuple() == other.__comparable_tuple()
901907
return False
902908

903909
def __lt__(self, other: Any) -> bool:
904910
if isinstance(other, ExternalReference):
905-
return _ComparableTuple((
906-
self._type, self._url, self._comment
907-
)) < _ComparableTuple((
908-
other._type, other._url, other._comment
909-
))
911+
return self.__comparable_tuple() < other.__comparable_tuple()
910912
return NotImplemented
911913

912914
def __hash__(self) -> int:
913-
# TODO
914-
return hash((
915-
self._type, self._url, self._comment,
916-
tuple(sorted(self._hashes, key=hash))
917-
))
915+
return hash(self.__comparable_tuple())
918916

919917
def __repr__(self) -> str:
920918
return f'<ExternalReference {self.type.name}, {self.url}>'
@@ -973,23 +971,23 @@ def value(self) -> Optional[str]:
973971
def value(self, value: Optional[str]) -> None:
974972
self._value = value
975973

974+
def __comparable_tuple(self) -> _ComparableTuple:
975+
return _ComparableTuple((
976+
self.name, self.value
977+
))
978+
976979
def __eq__(self, other: object) -> bool:
977980
if isinstance(other, Property):
978-
return hash(other) == hash(self)
981+
return self.__comparable_tuple() == other.__comparable_tuple()
979982
return False
980983

981984
def __lt__(self, other: Any) -> bool:
982985
if isinstance(other, Property):
983-
return _ComparableTuple((
984-
self.name, self.value
985-
)) < _ComparableTuple((
986-
other.name, other.value
987-
))
986+
return self.__comparable_tuple() < other.__comparable_tuple()
988987
return NotImplemented
989988

990989
def __hash__(self) -> int:
991-
# TODO
992-
return hash((self.name, self.value))
990+
return hash(self.__comparable_tuple())
993991

994992
def __repr__(self) -> str:
995993
return f'<Property name={self.name}>'
@@ -1065,23 +1063,23 @@ def encoding(self) -> Optional[Encoding]:
10651063
def encoding(self, encoding: Optional[Encoding]) -> None:
10661064
self._encoding = encoding
10671065

1066+
def __comparable_tuple(self) -> _ComparableTuple:
1067+
return _ComparableTuple((
1068+
self.content, self.content_type, self.encoding
1069+
))
1070+
10681071
def __eq__(self, other: object) -> bool:
10691072
if isinstance(other, NoteText):
1070-
return hash(other) == hash(self)
1073+
return self.__comparable_tuple() == other.__comparable_tuple()
10711074
return False
10721075

10731076
def __lt__(self, other: Any) -> bool:
10741077
if isinstance(other, NoteText):
1075-
return _ComparableTuple((
1076-
self.content, self.content_type, self.encoding
1077-
)) < _ComparableTuple((
1078-
other.content, other.content_type, other.encoding
1079-
))
1078+
return self.__comparable_tuple() < other.__comparable_tuple()
10801079
return NotImplemented
10811080

10821081
def __hash__(self) -> int:
1083-
# TODO
1084-
return hash((self.content, self.content_type, self.encoding))
1082+
return hash(self.__comparable_tuple())
10851083

10861084
def __repr__(self) -> str:
10871085
return f'<NoteText content_type={self.content_type}, encoding={self.encoding}>'
@@ -1150,23 +1148,23 @@ def locale(self, locale: Optional[str]) -> None:
11501148
" ISO-3166 (or higher) country code. according to ISO-639 format. Examples include: 'en', 'en-US'."
11511149
)
11521150

1151+
def __comparable_tuple(self) -> _ComparableTuple:
1152+
return _ComparableTuple((
1153+
self.locale, self.text
1154+
))
1155+
11531156
def __eq__(self, other: object) -> bool:
11541157
if isinstance(other, Note):
1155-
return hash(other) == hash(self)
1158+
return self.__comparable_tuple() == other.__comparable_tuple()
11561159
return False
11571160

11581161
def __lt__(self, other: Any) -> bool:
11591162
if isinstance(other, Note):
1160-
return _ComparableTuple((
1161-
self.locale, self.text
1162-
)) < _ComparableTuple((
1163-
other.locale, other.text
1164-
))
1163+
return self.__comparable_tuple() < other.__comparable_tuple()
11651164
return NotImplemented
11661165

11671166
def __hash__(self) -> int:
1168-
# TODO
1169-
return hash((self.text, self.locale))
1167+
return hash(self.__comparable_tuple())
11701168

11711169
def __repr__(self) -> str:
11721170
return f'<Note id={id(self)}, locale={self.locale}>'
@@ -1241,23 +1239,23 @@ def email(self) -> Optional[str]:
12411239
def email(self, email: Optional[str]) -> None:
12421240
self._email = email
12431241

1242+
def __comparable_tuple(self) -> _ComparableTuple:
1243+
return _ComparableTuple((
1244+
self.timestamp, self.name, self.email
1245+
))
1246+
12441247
def __eq__(self, other: object) -> bool:
12451248
if isinstance(other, IdentifiableAction):
1246-
return hash(other) == hash(self)
1249+
return self.__comparable_tuple() == other.__comparable_tuple()
12471250
return False
12481251

12491252
def __lt__(self, other: Any) -> bool:
12501253
if isinstance(other, IdentifiableAction):
1251-
return _ComparableTuple((
1252-
self.timestamp, self.name, self.email
1253-
)) < _ComparableTuple((
1254-
other.timestamp, other.name, other.email
1255-
))
1254+
return self.__comparable_tuple() < other.__comparable_tuple()
12561255
return NotImplemented
12571256

12581257
def __hash__(self) -> int:
1259-
# TODO
1260-
return hash((self.timestamp, self.name, self.email))
1258+
return hash(self.__comparable_tuple())
12611259

12621260
def __repr__(self) -> str:
12631261
return f'<IdentifiableAction name={self.name}, email={self.email}>'

cyclonedx/model/bom.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import serializable
2626
from sortedcontainers import SortedSet
2727

28+
from .._internal.compare import ComparableTuple as _ComparableTuple
2829
from .._internal.time import get_now_utc as _get_now_utc
2930
from ..exception.model import LicenseExpressionAlongWithOthersException, UnknownComponentDependencyException
3031
from ..schema.schema import (
@@ -48,8 +49,6 @@
4849
from .service import Service
4950
from .tool import Tool, ToolRepository, _ToolRepositoryHelper
5051
from .vulnerability import Vulnerability
51-
from .._internal.compare import ComparableTuple as _ComparableTuple
52-
5352

5453
if TYPE_CHECKING: # pragma: no cover
5554
from packageurl import PackageURL
@@ -730,8 +729,10 @@ def validate(self) -> bool:
730729

731730
def __comparable_tuple(self) -> _ComparableTuple:
732731
return _ComparableTuple((
733-
self.serial_number, self.version, self.metadata, _ComparableTuple(self.components), _ComparableTuple(self.services),
734-
_ComparableTuple(self.external_references), _ComparableTuple(self.dependencies), _ComparableTuple(self.properties),
732+
self.serial_number, self.version, self.metadata, _ComparableTuple(
733+
self.components), _ComparableTuple(self.services),
734+
_ComparableTuple(self.external_references), _ComparableTuple(
735+
self.dependencies), _ComparableTuple(self.properties),
735736
_ComparableTuple(self.vulnerabilities),
736737
))
737738

cyclonedx/model/contact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def street_address(self, street_address: Optional[str]) -> None:
162162
self._street_address = street_address
163163

164164
def __comparable_tuple(self) -> _ComparableTuple:
165-
return _ComparableTuple((
165+
return _ComparableTuple((
166166
self.bom_ref,
167167
self.country, self.region, self.locality, self.postal_code,
168168
self.post_office_box_number,

cyclonedx/model/service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ def __comparable_tuple(self) -> _ComparableTuple:
356356
return _ComparableTuple((
357357
self.authenticated, _ComparableTuple(self.data), self.description, _ComparableTuple(self.endpoints),
358358
_ComparableTuple(self.external_references), self.group, _ComparableTuple(self.licenses), self.name,
359-
_ComparableTuple(self.properties), self.provider, self.release_notes, _ComparableTuple(self.services), self.version,
359+
_ComparableTuple(self.properties), self.provider, self.release_notes, _ComparableTuple(
360+
self.services), self.version,
360361
self.x_trust_boundary
361362
))
362363

cyclonedx/model/vulnerability.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,8 +1311,10 @@ def __comparable_tuple(self) -> _ComparableTuple:
13111311
return _ComparableTuple((
13121312
self.id, self.description,
13131313
self.source, _ComparableTuple(self.references), _ComparableTuple(self.ratings), _ComparableTuple(self.cwes),
1314-
self.detail, self.recommendation, self.workaround, _ComparableTuple(self.advisories), self.created, self.published,
1315-
self.updated, self.credits, self.tools, self.analysis, _ComparableTuple(self.affects), _ComparableTuple(self.properties)
1314+
self.detail, self.recommendation, self.workaround, _ComparableTuple(
1315+
self.advisories), self.created, self.published,
1316+
self.updated, self.credits, self.tools, self.analysis, _ComparableTuple(
1317+
self.affects), _ComparableTuple(self.properties)
13161318
))
13171319

13181320
def __eq__(self, other: object) -> bool:

0 commit comments

Comments
 (0)