8000 [issue-503] fix: correct type for LicenseInfoInSnippet in Snippet · spdx/tools-python@66a3355 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66a3355

Browse files
committed
[issue-503] fix: correct type for LicenseInfoInSnippet in Snippet
Signed-off-by: Meret Behrens <meret.behrens@tngtech.com>
1 parent 4a0675f commit 66a3355

File tree

14 files changed

+44
-42
lines changed

14 files changed

+44
-42
lines changed

src/spdx/jsonschema/snippet_converter.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ def _get_property_value(self, snippet: Snippet, snippet_property: SnippetPropert
4848
elif snippet_property == SnippetProperty.LICENSE_CONCLUDED:
4949
return apply_if_present(str, snippet.license_concluded)
5050
elif snippet_property == SnippetProperty.LICENSE_INFO_IN_SNIPPETS:
51-
if isinstance(snippet.license_info_in_snippet, list):
52-
return [str(license_expression) for license_expression in snippet.license_info_in_snippet] or None
53-
return apply_if_present(str, snippet.license_info_in_snippet)
51+
return [str(license_expression) for license_expression in snippet.license_info_in_snippet] or None
5452
elif snippet_property == SnippetProperty.NAME:
5553
return snippet.name
5654
elif snippet_property == SnippetProperty.RANGES:

src/spdx/model/snippet.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Snippet:
2525
byte_range: Tuple[int, int]
2626
line_range: Optional[Tuple[int, int]] = None
2727
license_concluded: Optional[Union[LicenseExpression, SpdxNoAssertion, SpdxNone]] = None
28-
license_info_in_snippet: Optional[Union[List[LicenseExpression], SpdxNoAssertion, SpdxNone]] = None
28+
license_info_in_snippet: List[Union[LicenseExpression, SpdxNoAssertion, SpdxNone]] = None
2929
license_comment: Optional[str] = None
3030
copyright_text: Optional[Union[str, SpdxNoAssertion, SpdxNone]] = None
3131
comment: Optional[str] = None
@@ -35,8 +35,9 @@ class Snippet:
3535
def __init__(self, spdx_id: str, file_spdx_id: str, byte_range: Tuple[int, int],
3636
line_range: Optional[Tuple[int, int]] = None,
3737
license_concluded: Optional[Union[LicenseExpression, SpdxNoAssertion, SpdxNone]] = None,
38-
license_info_in_snippet: Optional[Union[List[LicenseExpression], SpdxNoAssertion, SpdxNone]] = None,
38+
license_info_in_snippet: List[Union[LicenseExpression, SpdxNoAssertion, SpdxNone]] = None,
3939
license_comment: Optional[str] = None, copyright_text: Optional[str] = None,
4040
comment: Optional[str] = None, name: Optional[str] = None, attribution_texts: List[str] = None):
4141
attribution_texts = [] if attribution_texts is None else attribution_texts
42+
license_info_in_snippet = [] if license_info_in_snippet is None else license_info_in_snippet
4243
check_types_and_set_values(self, locals())

src/spdx/parser/jsonlikedict/snippet_parser.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def parse_snippet(self, snippet_dict: Dict) -> Snippet:
4242
spdx_id: Optional[str] = snippet_dict.get("SPDXID")
4343
file_spdx_id: Optional[str] = snippet_dict.get("snippetFromFile")
4444
name: Optional[str] = snippet_dict.get("name")
45-
45+
4646
ranges: Dict = parse_field_or_log_error(logger, snippet_dict.get("ranges", []), self.parse_ranges, default={})
4747
byte_range: Optional[Tuple[Union[int, str], Union[int, str]]] = ranges.get(RangeType.BYTE)
4848
line_range: Optional[Tuple[Union[int, str], Union[int, str]]] = ranges.get(RangeType.LINE)
@@ -53,15 +53,12 @@ def parse_snippet(self, snippet_dict: Dict) -> Snippet:
5353
comment: Optional[str] = snippet_dict.get("comment")
5454
copyright_text: Optional[str] = snippet_dict.get("copyrightText")
5555
license_comment: Optional[str] = snippet_dict.get("licenseComments")
56-
license_concluded: Optional[Union[
57-
LicenseExpression, SpdxNoAssertion, SpdxNone]] = parse_field_or_log_error(logger, snippet_dict.get(
58-
"licenseConcluded"), lambda x: parse_field_or_no_assertion_or_none(x,
59-
self.license_expression_parser.parse_license_expression))
60-
61-
license_info: Optional[Union[List[
62-
LicenseExpression], SpdxNoAssertion, SpdxNone]] = parse_field_or_log_error(logger, snippet_dict.get(
63-
"licenseInfoInSnippets"), lambda x: parse_field_or_no_assertion_or_none(x,
64-
self.license_expression_parser.parse_license_expressions))
56+
license_concluded: Optional[Union[LicenseExpression, SpdxNoAssertion, SpdxNone]] = parse_field_or_log_error(
57+
logger, snippet_dict.get("licenseConcluded"), self.license_expression_parser.parse_license_expression)
58+
59+
license_info: List[Union[LicenseExpression], SpdxNoAssertion, SpdxNone] = parse_field_or_log_error(
60+
logger, snippet_dict.get("licenseInfoInSnippets"), self.license_expression_parser.parse_license_expression,
61+
field_is_list=True)
6562
if logger.has_messages():
6663
raise SPDXParsingError([f"Error while parsing snippet: {logger.get_messages()}"])
6764

@@ -121,7 +118,8 @@ def validate_pointer_and_get_type(pointer: Dict) -> RangeType:
121118
return RangeType.BYTE if "offset" in pointer else RangeType.LINE
122119

123120
@staticmethod
124-
def convert_range_from_str(_range: Tuple[Union[int, str], Union[int, str]]) -> Tuple[Union[int, str], Union[int, str]]:
121+
def convert_range_from_str(_range: Tuple[Union[int, str], Union[int, str]]) -> Tuple[
122+
Union[int, str], Union[int, str]]:
125123
# XML does not support integers, so we have to convert from string (if possible)
126124
if not _range:
127125
return _range

src/spdx/parser/tagvalue/parser.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,7 @@ def p_snippet_attribution_text(self, p):
407407

408408
@grammar_rule("snippet_license_info : SNIPPET_LICENSE_INFO license_or_no_assertion_or_none")
409409
def p_snippet_license_info(self, p):
410-
if not self.check_that_current_element_matches_class_for_value(Snippet, p.lineno(1)):
411-
return
412-
if p[2] == SpdxNone() or p[2] == SpdxNoAssertion():
413-
self.current_element["license_info_in_snippet"] = p[2]
414-
else:
410+
if self.check_that_current_element_matches_class_for_value(Snippet, p.lineno(1)):
415411
self.current_element.setdefault("license_info_in_snippet", []).append(p[2])
416412

417413
@grammar_rule("snippet_byte_range : SNIPPET_BYTE_RANGE LINE\n snippet_line_range : SNIPPET_LINE_RANGE LINE")

src/spdx/writer/rdf/license_expression_writer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def add_license_expression_or_none_or_no_assertion(value: Union[
26-
List[LicenseExpression], LicenseExpression, SpdxNoAssertion, SpdxNone], graph: Graph, parent: Node, predicate: Node,
26+
List[Union[LicenseExpression, SpdxNoAssertion, SpdxNone]], LicenseExpression, SpdxNoAssertion, SpdxNone], graph: Graph, parent: Node, predicate: Node,
2727
doc_namespace: str):
2828
if isinstance(value, SpdxNoAssertion):
2929
graph.add((parent, predicate, SPDX_NAMESPACE.noassertion))
@@ -33,7 +33,7 @@ def add_license_expression_or_none_or_no_assertion(value: Union[
3333
return
3434
if isinstance(value, list):
3535
for license_expression in value:
36-
add_license_expression_to_graph(license_expression, graph, parent, predicate, doc_namespace)
36+
add_license_expression_or_none_or_no_assertion(license_expression, graph, parent, predicate, doc_namespace)
3737
if isinstance(value, LicenseExpression):
3838
add_license_expression_to_graph(value, graph, parent, predicate, doc_namespace)
3939

tests/spdx/fixtures.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
ExternalPackageRefCategory
2424
from spdx.model.relationship import Relationship, RelationshipType
2525
from spdx.model.snippet import Snippet
26+
from spdx.model.spdx_no_assertion import SpdxNoAssertion
27+
from spdx.model.spdx_none import SpdxNone
2628
from spdx.model.version import Version
2729

2830
"""Utility methods to create data model instances. All properties have valid defaults, so they don't need to be
@@ -116,7 +118,7 @@ def snippet_fixture(spdx_id="SPDXRef-Snippet", file_spdx_id="SPDXRef-File", byte
116118
copyright_text="licenseCopyrightText", comment="snippetComment", name="snippetName",
117119
attribution_texts=None) -> Snippet:
118120
license_info_in_snippet = [get_spdx_licensing().parse("MIT"), get_spdx_licensing().parse(
119-
"GPL-2.0")] if license_info_in_snippet is None else license_info_in_snippet
121+
"GPL-2.0"), SpdxNone()] if license_info_in_snippet is None else license_info_in_snippet
120122
attribution_texts = ["snippetAttributionText"] if attribution_texts is None else attribution_texts
121123
return Snippet(spdx_id=spdx_id, file_spdx_id=file_spdx_id, byte_range=byte_range, line_range=line_range,
122124
license_concluded=license_concluded, license_info_in_snippet=license_info_in_snippet,

tests/spdx/jsonschema/test_snippet_converter.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,24 @@ def test_null_values(converter: SnippetConverter):
114114

115115

116116
def test_spdx_no_assertion(converter: SnippetConverter):
117-
snippet = snippet_fixture(license_concluded=SpdxNoAssertion(), license_info_in_snippet=SpdxNoAssertion())
117+
snippet = snippet_fixture(license_concluded=SpdxNoAssertion(), license_info_in_snippet=[SpdxNoAssertion()])
118118

119119
document = Document(creation_info_fixture(), snippets=[snippet])
120120
converted_dict = converter.convert(snippet, document)
121121

122122
assert converted_dict[converter.json_property_name(SnippetProperty.LICENSE_CONCLUDED)] == SPDX_NO_ASSERTION_STRING
123-
assert converted_dict[
124-
converter.json_property_name(SnippetProperty.LICENSE_INFO_IN_SNIPPETS)] == SPDX_NO_ASSERTION_STRING
123+
assert converted_dict[converter.json_property_name(SnippetProperty.LICENSE_INFO_IN_SNIPPETS)] == [
124+
SPDX_NO_ASSERTION_STRING]
125125

126126

127127
def test_spdx_none(converter: SnippetConverter):
128-
snippet = snippet_fixture(license_concluded=SpdxNone(), license_info_in_snippet=SpdxNone())
128+
snippet = snippet_fixture(license_concluded=SpdxNone(), license_info_in_snippet=[SpdxNone()])
129129

130130
document = Document(creation_info_fixture(), snippets=[snippet])
131131
converted_dict = converter.convert(snippet, document)
132132

133133
assert converted_dict[converter.json_property_name(SnippetProperty.LICENSE_CONCLUDED)] == SPDX_NONE_STRING
134-
assert converted_dict[
135-
converter.json_property_name(SnippetProperty.LICENSE_INFO_IN_SNIPPETS)] == SPDX_NONE_STRING
134+
assert converted_dict[converter.json_property_name(SnippetProperty.LICENSE_INFO_IN_SNIPPETS)] == [SPDX_NONE_STRING]
136135

137136

138137
def test_snippet_annotations(converter: SnippetConverter):

tests/spdx/model/test_snippet.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77

88
def test_correct_initialization():
9-
snippet = Snippet("id", "file_id", (200, 400), (20, 40), SpdxNone(), SpdxNoAssertion(), "comment on license",
9+
snippet = Snippet("id", "file_id", (200, 400), (20, 40), SpdxNone(), [SpdxNoAssertion()], "comment on license",
1010
"copyright", "comment", "name", ["attribution"])
1111
assert snippet.spdx_id == "id"
1212
assert snippet.file_spdx_id == "file_id"
1313
assert snippet.byte_range == (200, 400)
1414
assert snippet.line_range == (20, 40)
1515
assert snippet.license_concluded == SpdxNone()
16-
assert snippet.license_info_in_snippet == SpdxNoAssertion()
16+
assert snippet.license_info_in_snippet == [SpdxNoAssertion()]
1717
assert snippet.license_comment == "comment on license"
1818
assert snippet.copyright_text == "copyright"
1919
assert snippet.comment == "comment"
@@ -28,7 +28,7 @@ def test_correct_initialization_with_default_values():
2828
assert snippet.byte_range == (200, 400)
2929
assert snippet.line_range is None
3030
assert snippet.license_concluded is None
31-
assert snippet.license_info_in_snippet is None
31+
assert snippet.license_info_in_snippet == []
3232
assert snippet.license_comment is None
3333
assert snippet.copyright_text is None
3434
assert snippet.comment is None
@@ -63,7 +63,7 @@ def test_wrong_type_in_license_concluded():
6363

6464
def test_wrong_type_in_license_info_in_snippet():
6565
with pytest.raises(TypeError):
66-
Snippet("id", "file_id", (200, 400), license_info_in_snippet=[SpdxNoAssertion()])
66+
Snippet("id", "file_id", (200, 400), license_info_in_snippet=SpdxNoAssertion())
6767

6868

6969
def test_wrong_type_in_license_comment():

tests/spdx/parser/jsonlikedict/test_snippet_parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import pytest
1414

1515
from license_expression import Licensing
16+
17+
from spdx.model.spdx_no_assertion import SpdxNoAssertion
1618
from spdx.parser.error import SPDXParsingError
1719
from spdx.parser.jsonlikedict.snippet_parser import SnippetParser
1820

@@ -26,7 +28,7 @@ def test_parse_snippet():
2628
"copyrightText": "Copyright 2008-2010 John Smith",
2729
"licenseComments": "The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.",
2830
"licenseConcluded": "GPL-2.0-only",
29-
"licenseInfoInSnippets": ["GPL-2.0-only"],
31+
"licenseInfoInSnippets": ["GPL-2.0-only", "NOASSERTION"],
3032
"name": "from linux kernel",
3133
"ranges": [{
3234
"endPointer": {
@@ -60,7 +62,7 @@ def test_parse_snippet():
6062
assert snippet.byte_range == (310, 420)
6163
assert snippet.line_range == (5, 23)
6264
assert snippet.file_spdx_id == "SPDXRef-DoapSource"
63-
assert snippet.license_info_in_snippet == [Licensing().parse("GPL-2.0-only")]
65+
assert snippet.license_info_in_snippet == [Licensing().parse("GPL-2.0-only"), SpdxNoAssertion()]
6466
assert snippet.license_concluded == Licensing().parse("GPL-2.0-only")
6567
assert snippet.attribution_texts == ["Some example attibution text."]
6668

tests/spdx/parser/rdf/data/file_to_test_rdf_parser.rdf.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
<spdx:snippetFromFile rdf:resource="https://some.namespace#SPDXRef-File"/>
229229
<spdx:licenseInfoInSnippet rdf:resource="http://spdx.org/licenses/MIT"/>
230230
<spdx:licenseInfoInSnippet rdf:resource="http://spdx.org/licenses/GPL-2.0-only"/>
231+
<spdx:licenseInfoInSnippet rdf:resource="http://spdx.org/rdf/terms#noassertion"/>
231232
<spdx:attributionText>snippetAttributionText</spdx:attributionText>
232233
<spdx:name>snippetName</spdx:name>
233234
<spdx:licenseComments>snippetLicenseComment</spdx:licenseComments>

0 commit comments

Comments
 (0)
0