Skip to content

Commit 735c05e

Browse files
committed
fix type hint for get_component_by_purl
Signed-off-by: gruebel <[email protected]>
1 parent 6c0c174 commit 735c05e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

cyclonedx/model/bom.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Copyright (c) OWASP Foundation. All Rights Reserved.
1919
import warnings
2020
from datetime import datetime, timezone
21-
from typing import Iterable, Optional, Set
21+
from typing import TYPE_CHECKING, Iterable, Optional, Set
2222
from uuid import UUID, uuid4
2323

2424
from sortedcontainers import SortedSet
@@ -29,6 +29,9 @@
2929
from .component import Component
3030
from .service import Service
3131

32+
if TYPE_CHECKING:
33+
from packageurl import PackageURL # type:ignore[import]
34+
3235

3336
class BomMetaData:
3437
"""
@@ -288,19 +291,19 @@ def components(self) -> "SortedSet[Component]":
288291
def components(self, components: Iterable[Component]) -> None:
289292
self._components = SortedSet(components)
290293

291-
def get_component_by_purl(self, purl: Optional[str]) -> Optional[Component]:
294+
def get_component_by_purl(self, purl: Optional["PackageURL"]) -> Optional[Component]:
292295
"""
293296
Get a Component already in the Bom by its PURL
294297
295298
Args:
296299
purl:
297-
Package URL as a `str` to look and find `Component`
300+
An instance of `packageurl.PackageURL` to look and find `Component`.
298301
299302
Returns:
300303
`Component` or `None`
301304
"""
302305
if purl:
303-
found = list(filter(lambda x: x.purl == purl, self.components))
306+
found = [x for x in self.components if x.purl == purl]
304307
if len(found) == 1:
305308
return found[0]
306309

tests/test_component.py

+10
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ def test_has_component_1(self) -> None:
8585
self.assertEqual(len(bom.components), 2)
8686
self.assertTrue(bom.has_component(component=get_component_setuptools_simple_no_version()))
8787
self.assertIsNot(get_component_setuptools_simple(), get_component_setuptools_simple_no_version())
88+
89+
def test_get_component_by_purl(self) -> None:
90+
bom = Bom()
91+
setuptools_simple = get_component_setuptools_simple()
92+
bom.components.add(get_component_setuptools_simple())
93+
94+
result = bom.get_component_by_purl(get_component_setuptools_simple().purl)
95+
96+
self.assertEqual(result, setuptools_simple)
97+
self.assertIsNone(bom.get_component_by_purl(get_component_setuptools_simple_no_version().purl))

0 commit comments

Comments
 (0)