Skip to content

Commit df0f554

Browse files
Add type hints for SortedSet
Fix use of set/Set. Signed-off-by: Rodney Richardson <[email protected]>
1 parent ec22f68 commit df0f554

File tree

14 files changed

+241
-88
lines changed

14 files changed

+241
-88
lines changed

.mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[mypy]
22

33
files = cyclonedx/
4+
mypy_path = $MYPY_CONFIG_FILE_DIR/typings
45

56
show_error_codes = True
67
pretty = True

cyclonedx/model/__init__.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import warnings
2222
from datetime import datetime
2323
from enum import Enum
24-
from typing import Any, Iterable, Optional, Set
24+
from typing import Any, Iterable, Optional, Set, TypeVar
2525

2626
from sortedcontainers import SortedSet
2727

@@ -496,7 +496,7 @@ def type(self, type_: ExternalReferenceType) -> None:
496496
self._type = type_
497497

498498
@property
499-
def hashes(self) -> Set[HashType]:
499+
def hashes(self) -> SortedSet[HashType]:
500500
"""
501501
The hashes of the external reference (if applicable).
502502
@@ -516,7 +516,8 @@ def __eq__(self, other: object) -> bool:
516516

517517
def __lt__(self, other: Any) -> bool:
518518
if isinstance(other, ExternalReference):
519-
return ComparableTuple((self._type, self._url, self._comment)) < ComparableTuple((other._type, other._url, other._comment))
519+
return ComparableTuple((self._type, self._url, self._comment)) < \
520+
ComparableTuple((other._type, other._url, other._comment))
520521
return NotImplemented
521522

522523
def __hash__(self) -> int:
@@ -831,7 +832,8 @@ def __eq__(self, other: object) -> bool:
831832

832833
def __lt__(self, other: Any) -> bool:
833834
if isinstance(other, NoteText):
834-
return ComparableTuple((self.content, self.content_type, self.encoding)) < ComparableTuple((other.content, other.content_type, other.encoding))
835+
return ComparableTuple((self.content, self.content_type, self.encoding)) < \
836+
ComparableTuple((other.content, other.content_type, other.encoding))
835837
return NotImplemented
836838

837839
def __hash__(self) -> int:
@@ -980,7 +982,8 @@ def __eq__(self, other: object) -> bool:
980982

981983
def __lt__(self, other: Any) -> bool:
982984
if isinstance(other, OrganizationalContact):
983-
return ComparableTuple((self.name, self.email, self.phone)) < ComparableTuple((other.name, other.email, other.phone))
985+
return ComparableTuple((self.name, self.email, self.phone)) < \
986+
ComparableTuple((other.name, other.email, other.phone))
984987
return NotImplemented
985988

986989
def __hash__(self) -> int:
@@ -1024,7 +1027,7 @@ def name(self, name: Optional[str]) -> None:
10241027
self._name = name
10251028

10261029
@property
1027-
def url(self) -> Set[XsUri]:
1030+
def url(self) -> SortedSet[XsUri]:
10281031
"""
10291032
Get a list of URLs of the organization. Multiple URLs are allowed.
10301033
@@ -1038,7 +1041,7 @@ def url(self, urls: Iterable[XsUri]) -> None:
10381041
self._url = SortedSet(urls)
10391042

10401043
@property
1041-
def contact(self) -> Set[OrganizationalContact]:
1044+
def contact(self) -> SortedSet[OrganizationalContact]:
10421045
"""
10431046
Get a list of contact person at the organization. Multiple contacts are allowed.
10441047
@@ -1125,7 +1128,7 @@ def version(self, version: Optional[str]) -> None:
11251128
self._version = version
11261129

11271130
@property
1128-
def hashes(self) -> Set[HashType]:
1131+
def hashes(self) -> SortedSet[HashType]:
11291132
"""
11301133
The hashes of the tool (if applicable).
11311134
@@ -1139,7 +1142,7 @@ def hashes(self, hashes: Iterable[HashType]) -> None:
11391142
self._hashes = SortedSet(hashes)
11401143

11411144
@property
1142-
def external_references(self) -> Set[ExternalReference]:
1145+
def external_references(self) -> SortedSet[ExternalReference]:
11431146
"""
11441147
External References provide a way to document systems, sites, and information that may be relevant but which
11451148
are not included with the BOM.
@@ -1160,7 +1163,8 @@ def __eq__(self, other: object) -> bool:
11601163

11611164
def __lt__(self, other: Any) -> bool:
11621165
if isinstance(other, Tool):
1163-
return ComparableTuple((self.vendor, self.name, self.version)) < ComparableTuple((other.vendor, other.name, other.version))
1166+
return ComparableTuple((self.vendor, self.name, self.version)) < \
1167+
ComparableTuple((other.vendor, other.name, other.version))
11641168
return NotImplemented
11651169

11661170
def __hash__(self) -> int:
@@ -1238,7 +1242,8 @@ def __eq__(self, other: object) -> bool:
12381242

12391243
def __lt__(self, other: Any) -> bool:
12401244
if isinstance(other, IdentifiableAction):
1241-
return ComparableTuple((self.timestamp, self.name, self.email)) < ComparableTuple((other.timestamp, other.name, other.email))
1245+
return ComparableTuple((self.timestamp, self.name, self.email)) < \
1246+
ComparableTuple((other.timestamp, other.name, other.email))
12421247
return NotImplemented
12431248

12441249
def __hash__(self) -> int:

cyclonedx/model/bom.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from typing import Iterable, Optional, Set
2222
from uuid import UUID, uuid4
2323

24+
from sortedcontainers import SortedSet
25+
2426
from ..exception.model import UnknownComponentDependencyException
2527
from ..parser import BaseParser
2628
from . import ExternalReference, LicenseChoice, OrganizationalContact, OrganizationalEntity, Property, ThisTool, Tool
@@ -43,13 +45,13 @@ def __init__(self, *, tools: Optional[Iterable[Tool]] = None,
4345
licenses: Optional[Iterable[LicenseChoice]] = None,
4446
properties: Optional[Iterable[Property]] = None) -> None:
4547
self.timestamp = datetime.now(tz=timezone.utc)
46-
self.tools = set(tools or [])
47-
self.authors = set(authors or [])
48+
self.tools = SortedSet(tools or [])
49+
self.authors = SortedSet(authors or [])
4850
self.component = component
4951
self.manufacture = manufacture
5052
self.supplier = supplier
51-
self.licenses = set(licenses or [])
52-
self.properties = set(properties or [])
53+
self.licenses = SortedSet(licenses or [])
54+
self.properties = SortedSet(properties or [])
5355

5456
if not tools:
5557
self.tools.add(ThisTool)
@@ -69,7 +71,7 @@ def timestamp(self, timestamp: datetime) -> None:
6971
self._timestamp = timestamp
7072

7173
@property
72-
def tools(self) -> Set[Tool]:
74+
def tools(self) -> SortedSet[Tool]:
7375
"""
7476
Tools used to create this BOM.
7577
@@ -80,10 +82,10 @@ def tools(self) -> Set[Tool]:
8082

8183
@tools.setter
8284
def tools(self, tools: Iterable[Tool]) -> None:
83-
self._tools = set(tools)
85+
self._tools = SortedSet(tools)
8486

8587
@property
86-
def authors(self) -> Set[OrganizationalContact]:
88+
def authors(self) -> SortedSet[OrganizationalContact]:
8789
"""
8890
The person(s) who created the BOM.
8991
@@ -98,7 +100,7 @@ def authors(self) -> Set[OrganizationalContact]:
98100

99101
@authors.setter
100102
def authors(self, authors: Iterable[OrganizationalContact]) -> None:
101-
self._authors = set(authors)
103+
self._authors = SortedSet(authors)
102104

103105
@property
104106
def component(self) -> Optional[Component]:
@@ -155,7 +157,7 @@ def supplier(self, supplier: Optional[OrganizationalEntity]) -> None:
155157
self._supplier = supplier
156158

157159
@property
158-
def licenses(self) -> Set[LicenseChoice]:
160+
def licenses(self) -> SortedSet[LicenseChoice]:
159161
"""
160162
A optional list of statements about how this BOM is licensed.
161163
@@ -166,10 +168,10 @@ def licenses(self) -> Set[LicenseChoice]:
166168

167169
@licenses.setter
168170
def licenses(self, licenses: Iterable[LicenseChoice]) -> None:
169-
self._licenses = set(licenses)
171+
self._licenses = SortedSet(licenses)
170172

171173
@property
172-
def properties(self) -> Set[Property]:
174+
def properties(self) -> SortedSet[Property]:
173175
"""
174176
Provides the ability to document properties in a key/value store. This provides flexibility to include data not
175177
officially supported in the standard without having to use additional namespaces or create extensions.
@@ -184,7 +186,7 @@ def properties(self) -> Set[Property]:
184186

185187
@properties.setter
186188
def properties(self, properties: Iterable[Property]) -> None:
187-
self._properties = set(properties)
189+
self._properties = SortedSet(properties)
188190

189191
def __eq__(self, other: object) -> bool:
190192
if isinstance(other, BomMetaData):
@@ -237,9 +239,9 @@ def __init__(self, *, components: Optional[Iterable[Component]] = None,
237239
"""
238240
self.uuid = uuid4()
239241
self.metadata = BomMetaData()
240-
self.components = set(components or [])
241-
self.services = set(services or [])
242-
self.external_references = set(external_references or [])
242+
self.components = SortedSet(components or [])
243+
self.services = SortedSet(services or [])
244+
self.external_references = SortedSet(external_references or [])
243245

244246
@property
245247
def uuid(self) -> UUID:
@@ -273,7 +275,7 @@ def metadata(self, metadata: BomMetaData) -> None:
273275
self._metadata = metadata
274276

275277
@property
276-
def components(self) -> Set[Component]:
278+
def components(self) -> SortedSet[Component]:
277279
"""
278280
Get all the Components currently in this Bom.
279281
@@ -284,7 +286,7 @@ def components(self) -> Set[Component]:
284286

285287
@components.setter
286288
def components(self, components: Iterable[Component]) -> None:
287-
self._components = set(components)
289+
self._components = SortedSet(components)
288290

289291
def get_component_by_purl(self, purl: Optional[str]) -> Optional[Component]:
290292
"""
@@ -327,7 +329,7 @@ def has_component(self, component: Component) -> bool:
327329
return component in self.components
328330

329331
@property
330-
def services(self) -> Set[Service]:
332+
def services(self) -> SortedSet[Service]:
331333
"""
332334
Get all the Services currently in this Bom.
333335
@@ -338,10 +340,10 @@ def services(self) -> Set[Service]:
338340

339341
@services.setter
340342
def services(self, services: Iterable[Service]) -> None:
341-
self._services = set(services)
343+
self._services = SortedSet(services)
342344

343345
@property
344-
def external_references(self) -> Set[ExternalReference]:
346+
def external_references(self) -> SortedSet[ExternalReference]:
345347
"""
346348
Provides the ability to document external references related to the BOM or to the project the BOM describes.
347349
@@ -352,7 +354,7 @@ def external_references(self) -> Set[ExternalReference]:
352354

353355
@external_references.setter
354356
def external_references(self, external_references: Iterable[ExternalReference]) -> None:
355-
self._external_references = set(external_references)
357+
self._external_references = SortedSet(external_references)
356358

357359
def has_vulnerabilities(self) -> bool:
358360
"""

0 commit comments

Comments
 (0)