Skip to content

Commit c476ca9

Browse files
committed
Use Protocol
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
1 parent 5eb1641 commit c476ca9

5 files changed

Lines changed: 30 additions & 7 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ explicit_package_bases = true
7070
# they are excluded below so they don't mask issues in the new code.
7171
files = [
7272
"src/spdx_tools/spdx3/formats.py",
73+
"src/spdx_tools/spdx3/object_set.py",
7374
"src/spdx_tools/spdx3/parser",
7475
]
7576

src/spdx_tools/spdx3/object_set.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2026-present SPDX contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
from typing import Any, Iterable, Optional, Protocol, Set, runtime_checkable
4+
5+
6+
@runtime_checkable
7+
class SpdxObjectSet(Protocol):
8+
"""Version-agnostic structural type for a SHACL object set.
9+
10+
Satisfied by ``SHACLObjectSet`` from any ``spdx_python_model.vX_Y_Z``
11+
version module, so callers are not tied to a specific SPDX spec version.
12+
"""
13+
14+
def foreach(self) -> Iterable[Any]: ...
15+
16+
def foreach_type(self, typ: Any, *, match_subclass: bool = True) -> Iterable[Any]: ...
17+
18+
def find_by_id(self, _id: str, default: Optional[Any] = None) -> Optional[Any]: ...
19+
20+
def add(self, obj: Any) -> Any: ...
21+
22+
def link(self) -> Set[str]: ...

src/spdx_tools/spdx3/parser/json_ld/json_ld_parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
# SPDX-License-Identifier: Apache-2.0
33
from spdx_python_model import v3_0_1 as spdx_3_0
44

5+
from spdx_tools.spdx3.object_set import SpdxObjectSet
56
from spdx_tools.spdx.parser.error import SPDXParsingError
67

78

8-
def parse_from_file(file_name: str, encoding: str = "utf-8") -> spdx_3_0.SHACLObjectSet:
9+
def parse_from_file(file_name: str, encoding: str = "utf-8") -> SpdxObjectSet:
910
"""Read a SPDX 3 JSON-LD file into a SHACLObjectSet (the in-memory representation
1011
provided by the spdx-python-model bindings)."""
11-
object_set: spdx_3_0.SHACLObjectSet = spdx_3_0.SHACLObjectSet()
12+
object_set = spdx_3_0.SHACLObjectSet()
1213
try:
1314
# The binding's deserializer reads from a binary stream.
1415
with open(file_name, "rb") as file:

src/spdx_tools/spdx3/parser/parse_anything.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# SPDX-FileCopyrightText: 2026-present SPDX contributors
22
# SPDX-License-Identifier: Apache-2.0
3-
from spdx_python_model import v3_0_1 as spdx_3_0
4-
53
from spdx_tools.spdx3.formats import FileFormat, file_name_to_format
4+
from spdx_tools.spdx3.object_set import SpdxObjectSet
65
from spdx_tools.spdx3.parser.json_ld import json_ld_parser
76
from spdx_tools.spdx.parser.error import SPDXParsingError
87

98

10-
def parse_file(file_name: str, encoding: str = "utf-8") -> spdx_3_0.SHACLObjectSet:
9+
def parse_file(file_name: str, encoding: str = "utf-8") -> SpdxObjectSet:
1110
"""Parse a SPDX 3 file into a SHACLObjectSet, dispatching on the file format.
1211
1312
SPDX 3.0 currently defines a single serialization (JSON-LD); the dispatch is

tests/spdx3/parser/test_read.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import os
44

55
import pytest
6-
from spdx_python_model import v3_0_1 as spdx_3_0
76

7+
from spdx_tools.spdx3.object_set import SpdxObjectSet
88
from spdx_tools.spdx3.parser import parse_file
99
from spdx_tools.spdx.parser.error import SPDXParsingError
1010

@@ -14,7 +14,7 @@
1414
def test_parse_file_returns_object_set():
1515
object_set = parse_file(EXAMPLE_FILE)
1616

17-
assert isinstance(object_set, spdx_3_0.SHACLObjectSet)
17+
assert isinstance(object_set, SpdxObjectSet)
1818

1919
elements = list(object_set.foreach())
2020
assert len(elements) == 60

0 commit comments

Comments
 (0)