Skip to content
This repository was archived by the owner on Nov 23, 2024. It is now read-only.

Commit 97154b0

Browse files
authored
feat: for optional parameters allow using the default value as constant value (#916)
* feat(gui): add omitted variant to value forms * feat(backend): process OmittedAnnotation * feat(parser): generate value annotations with variant "omitted" * chore(data): generate annotations * style: apply automatic fixes of linters Co-authored-by: lars-reimann <lars-reimann@users.noreply.github.com>
1 parent 1601f9d commit 97154b0

File tree

7 files changed

+95
-34
lines changed

7 files changed

+95
-34
lines changed

package-parser/package_parser/processing/annotations/_generate_value_annotations.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from package_parser.processing.annotations.model import (
44
AnnotationStore,
55
ConstantAnnotation,
6+
OmittedAnnotation,
67
OptionalAnnotation,
78
RequiredAnnotation,
89
ValueAnnotation,
@@ -45,6 +46,18 @@ def _generate_constant_annotation(
4546
:param annotations: AnnotationStore object
4647
"""
4748

49+
# Always set to original default value
50+
if sole_stringified_value == parameter.default_value:
51+
annotations.valueAnnotations.append(
52+
OmittedAnnotation(
53+
target=parameter.id,
54+
authors=[autogen_author],
55+
reviewers=[],
56+
comment=f"I omitted this parameter because it is always set to the original default value ({parameter.default_value}).",
57+
)
58+
)
59+
return
60+
4861
default_value_type, default_value = _get_type_and_value_for_stringified_value(
4962
sole_stringified_value
5063
)

package-parser/package_parser/processing/annotations/model/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
EnumAnnotation,
77
EnumPair,
88
Interval,
9+
OmittedAnnotation,
910
OptionalAnnotation,
1011
ParameterInfo,
1112
ParameterType,

package-parser/package_parser/processing/annotations/model/_annotations.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class EnumAnnotation(AbstractAnnotation):
5757
class ValueAnnotation(AbstractAnnotation, ABC):
5858
class Variant(Enum):
5959
CONSTANT = "constant"
60+
OMITTED = "omitted"
6061
OPTIONAL = "optional"
6162
REQUIRED = "required"
6263

@@ -87,6 +88,20 @@ def to_json(self) -> dict:
8788
}
8889

8990

91+
@dataclass
92+
class OmittedAnnotation(ValueAnnotation):
93+
variant = ValueAnnotation.Variant.OMITTED
94+
95+
def to_json(self) -> dict:
96+
return {
97+
"target": self.target,
98+
"authors": self.authors,
99+
"reviewers": self.reviewers,
100+
"comment": self.comment,
101+
"variant": self.variant.value,
102+
}
103+
104+
90105
@dataclass
91106
class OptionalAnnotation(ValueAnnotation):
92107
variant = ValueAnnotation.Variant.OPTIONAL

package-parser/tests/data/valueAnnotations/annotation_data.json

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,14 @@
2626
"defaultValueType": "string",
2727
"defaultValue": "test"
2828
},
29-
"test/test/UsedClass/used_method/constant_optional_parameter": {
30-
"target": "test/test/UsedClass/used_method/constant_optional_parameter",
31-
"authors": ["$autogen$"],
32-
"reviewers": [],
33-
"comment": "I replaced this parameter with a constant because it is always set to the same literal value (0.5).",
34-
"variant": "constant",
35-
"defaultValueType": "number",
36-
"defaultValue": 0.5
37-
},
3829
"test/test/some_global_function/optional_parameter_that_should_be_constant": {
3930
"target": "test/test/some_global_function/optional_parameter_that_should_be_constant",
4031
"authors": ["$autogen$"],
4132
"reviewers": [],
42-
"comment": "I replaced this parameter with a constant because it is always set to the same literal value ('test').",
33+
"comment": "I replaced this parameter with a constant because it is always set to the same literal value ('test2').",
4334
"variant": "constant",
4435
"defaultValueType": "string",
45-
"defaultValue": "test"
36+
"defaultValue": "test2"
4637
},
4738
"test/test/some_global_function/required_parameter_that_should_be_constant": {
4839
"target": "test/test/some_global_function/required_parameter_that_should_be_constant",
@@ -53,6 +44,20 @@
5344
"defaultValueType": "string",
5445
"defaultValue": "test"
5546
},
47+
"test/test/UsedClass/used_method/constant_optional_parameter": {
48+
"target": "test/test/UsedClass/used_method/constant_optional_parameter",
49+
"authors": ["$autogen$"],
50+
"reviewers": [],
51+
"comment": "I omitted this parameter because it is always set to the original default value (0.5).",
52+
"variant": "omitted"
53+
},
54+
"test/test/some_global_function/optional_parameter_that_should_be_omitted": {
55+
"target": "test/test/some_global_function/optional_parameter_that_should_be_omitted",
56+
"authors": ["$autogen$"],
57+
"reviewers": [],
58+
"comment": "I omitted this parameter because it is always set to the original default value (np.nan).",
59+
"variant": "omitted"
60+
},
5661
"test/test/some_global_function/required_parameter_that_should_be_optional": {
5762
"target": "test/test/some_global_function/required_parameter_that_should_be_optional",
5863
"authors": ["$autogen$"],

package-parser/tests/data/valueAnnotations/api_data.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@
226226
"qname": "test.some_global_function.optional_parameter_that_should_be_constant",
227227
"default_value": "'test'"
228228
},
229+
{
230+
"id": "test/test/some_global_function/optional_parameter_that_should_be_omitted",
231+
"name": "optional_parameter_that_should_be_omitted",
232+
"qname": "test.some_global_function.optional_parameter_that_should_be_omitted",
233+
"default_value": "np.nan"
234+
},
229235
{
230236
"id": "test/test/some_global_function/optional_parameter_that_should_be_required",
231237
"name": "optional_parameter_that_should_be_required",

package-parser/tests/data/valueAnnotations/usage_data.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"test/test/some_global_function/required_parameter_that_should_be_required_with_non_string": 20,
3131
"test/test/some_global_function/required_parameter_that_should_be_optional": 20,
3232
"test/test/some_global_function/required_parameter_that_should_be_optional_with_non_string": 20,
33-
"test/test/some_global_function/optional_parameter_that_should_be_constant": 0,
33+
"test/test/some_global_function/optional_parameter_that_should_be_constant": 20,
34+
"test/test/some_global_function/optional_parameter_that_should_be_omitted": 0,
3435
"test/test/some_global_function/optional_parameter_that_should_be_required": 8,
3536
"test/test/some_global_function/optional_parameter_that_should_be_required_with_non_string": 18,
3637
"test/test/some_global_function/optional_parameter_that_should_be_optional_with_same_default": 4,
@@ -106,7 +107,10 @@
106107
"non_string": 4,
107108
"'test2'": 13
108109
},
109-
"test/test/some_global_function/optional_parameter_that_should_be_constant": {},
110+
"test/test/some_global_function/optional_parameter_that_should_be_constant": {
111+
"'test2'": 20
112+
},
113+
"test/test/some_global_function/optional_parameter_that_should_be_omitted": {},
110114
"test/test/some_global_function/optional_parameter_that_should_be_required": {
111115
"'test2'": 8
112116
},

package-parser/tests/processing/annotations/model/test_annotations.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
EnumAnnotation,
88
EnumPair,
99
Interval,
10+
OmittedAnnotation,
1011
OptionalAnnotation,
1112
RemoveAnnotation,
1213
RequiredAnnotation,
@@ -26,6 +27,29 @@ def test_base_annotation_to_json():
2627
}
2728

2829

30+
def test_boundary_annotation_to_json():
31+
annotation = BoundaryAnnotation(
32+
target="test/test",
33+
authors=["$autogen$"],
34+
reviewers=[],
35+
comment="Autogenerated",
36+
interval=Interval(False, 0, 0, 0, 0),
37+
)
38+
assert annotation.to_json() == {
39+
"target": "test/test",
40+
"authors": ["$autogen$"],
41+
"reviewers": [],
42+
"comment": "Autogenerated",
43+
"interval": {
44+
"isDiscrete": False,
45+
"lowerIntervalLimit": 0,
46+
"lowerLimitType": 0,
47+
"upperIntervalLimit": 0,
48+
"upperLimitType": 0,
49+
},
50+
}
51+
52+
2953
def test_constant_annotation_to_json():
3054
annotation = ConstantAnnotation(
3155
target="test/test",
@@ -46,23 +70,27 @@ def test_constant_annotation_to_json():
4670
}
4771

4872

49-
def test_remove_annotation_to_json():
50-
annotation = RemoveAnnotation(
73+
def test_enum_annotation_to_json():
74+
annotation = EnumAnnotation(
5175
target="test/test",
5276
authors=["$autogen$"],
5377
reviewers=[],
5478
comment="Autogenerated",
79+
enumName="test",
80+
pairs=[EnumPair("test", "test")],
5581
)
5682
assert annotation.to_json() == {
5783
"target": "test/test",
5884
"authors": ["$autogen$"],
5985
"reviewers": [],
6086
"comment": "Autogenerated",
87+
"enumName": "test",
88+
"pairs": [{"instanceName": "test", "stringValue": "test"}],
6189
}
6290

6391

64-
def test_required_annotation_to_json():
65-
annotation = RequiredAnnotation(
92+
def test_omitted_annotation_to_json():
93+
annotation = OmittedAnnotation(
6694
target="test/test",
6795
authors=["$autogen$"],
6896
reviewers=[],
@@ -73,7 +101,7 @@ def test_required_annotation_to_json():
73101
"authors": ["$autogen$"],
74102
"reviewers": [],
75103
"comment": "Autogenerated",
76-
"variant": "required",
104+
"variant": "omitted",
77105
}
78106

79107

@@ -97,45 +125,34 @@ def test_optional_annotation_to_json():
97125
}
98126

99127

100-
def test_boundary_annotation_to_json():
101-
annotation = BoundaryAnnotation(
128+
def test_remove_annotation_to_json():
129+
annotation = RemoveAnnotation(
102130
target="test/test",
103131
authors=["$autogen$"],
104132
reviewers=[],
105133
comment="Autogenerated",
106-
interval=Interval(False, 0, 0, 0, 0),
107134
)
108135
assert annotation.to_json() == {
109136
"target": "test/test",
110137
"authors": ["$autogen$"],
111138
"reviewers": [],
112139
"comment": "Autogenerated",
113-
"interval": {
114-
"isDiscrete": False,
115-
"lowerIntervalLimit": 0,
116-
"lowerLimitType": 0,
117-
"upperIntervalLimit": 0,
118-
"upperLimitType": 0,
119-
},
120140
}
121141

122142

123-
def test_enum_annotation_to_json():
124-
annotation = EnumAnnotation(
143+
def test_required_annotation_to_json():
144+
annotation = RequiredAnnotation(
125145
target="test/test",
126146
authors=["$autogen$"],
127147
reviewers=[],
128148
comment="Autogenerated",
129-
enumName="test",
130-
pairs=[EnumPair("test", "test")],
131149
)
132150
assert annotation.to_json() == {
133151
"target": "test/test",
134152
"authors": ["$autogen$"],
135153
"reviewers": [],
136154
"comment": "Autogenerated",
137-
"enumName": "test",
138-
"pairs": [{"instanceName": "test", "stringValue": "test"}],
155+
"variant": "required",
139156
}
140157

141158

0 commit comments

Comments
 (0)