Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit fd5816a

Browse files
MasaraGideonKoenigGideonKoeniglars-reimann
authored
feat: Automatisierte Herleitung von Optional Annotations (#473)
* Code should be working - Still need to write a test * feat: Added function for getting optional annotations and added tests * feat: Adjusted tests and function which calculates if a parameter is optional or required * Add tests and prepare for merge with issue #441 * Still testing * get required test funktioniert jetzt * Test done and working * Satisfy Linter * style: apply automatic fixes of linters * Bug fix * Implemented the new AnnotationStore object Improved auxiliary functions Added ParameterInfo Class to communicate the return values of functions better Migrated said ParameterInfo and ParameterType classes to annotation_model.py Test is now working - all is good * Update package-parser.iml * Renamed function to better communicate it's function * Delete package-parser.iml * Satisfy Linter -> Fixed typing * style: apply automatic fixes of linters * Remove unnecessary import * Further seperate the replaceable math part of determining if a parameter should be optional or required for more clarity * Function namechange * feat: Adjusted tests and functions for optional annotation & removed unused test files * feat: Fixed optional annotation function and adjusted them according to the new data class structure and adjusted the optional annotation test. * style: apply automatic fixes of linters * style: apply automatic fixes of linters * refactor: changed variable naming and comments Co-authored-by: GideonKoenig <[email protected]> Co-authored-by: GideonKoenig <[email protected]> Co-authored-by: GideonKoenig <[email protected]> Co-authored-by: Masara <[email protected]> Co-authored-by: Lars Reimann <[email protected]>
1 parent b0f7f79 commit fd5816a

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

package-parser/package_parser/commands/generate_annotations/generate_annotations.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from package_parser.models.annotation_models import (
99
AnnotationStore,
1010
ConstantAnnotation,
11+
OptionalAnnotation,
1112
ParameterInfo,
1213
ParameterType,
1314
RequiredAnnotation,
@@ -42,6 +43,7 @@ def generate_annotations(
4243
__get_unused_annotations,
4344
__get_constant_annotations,
4445
__get_required_annotations,
46+
__get_optional_annotations,
4547
]
4648

4749
__generate_annotation_dict(api, usages, annotations, annotation_functions)
@@ -273,9 +275,36 @@ def __add_implicit_usages_of_default_value(usages: UsageStore, api: API) -> None
273275
usages.add_value_usage(parameter_qname, default_value, location)
274276

275277

278+
def __get_optional_annotations(
279+
usages: UsageStore, api: API, annotations: AnnotationStore
280+
) -> None:
281+
"""
282+
Collects all parameters that are currently required but should be optional to be assign a value
283+
:param usages: Usage store
284+
:param api: Description of the API
285+
:param annotations: AnnotationStore, that holds all annotations
286+
"""
287+
parameters = api.parameters()
288+
all_parameter = [(it, parameters[it]) for it in parameters]
289+
290+
for qname, _ in all_parameter:
291+
parameter_info = __get_parameter_info(qname, usages)
292+
293+
if parameter_info.type == ParameterType.Optional:
294+
formatted_name = __qname_to_target_name(api, qname)
295+
annotations.optionals.append(
296+
OptionalAnnotation(
297+
target=formatted_name,
298+
defaultValue=parameter_info.value,
299+
defaultType=parameter_info.value_type,
300+
)
301+
)
302+
303+
276304
def __get_parameter_info(qname: str, usages: UsageStore) -> ParameterInfo:
277305
"""
278-
Returns a ParameterInfo object, that contains the type of the parameter, the value that is associated with it, and the values type
306+
Returns a ParameterInfo object, that contains the type of the parameter, the value that is associated with it,
307+
and the values type.
279308
:param qname: name of the parameter
280309
:param usages: UsageStore
281310
:return ParameterInfo
@@ -298,6 +327,8 @@ def __get_parameter_info(qname: str, usages: UsageStore) -> ParameterInfo:
298327
value = max(values, key=lambda item: item[1])[0]
299328
if value[0] == "'":
300329
value = value[1:-1]
330+
331+
# If its neither required nor constant, return optional
301332
return ParameterInfo(
302333
ParameterType.Optional, value, __get_default_type_from_value(value)
303334
)

package-parser/tests/commands/generate_annotations/test_generate_annotations.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from package_parser.commands.find_usages import UsageStore
77
from package_parser.commands.generate_annotations.generate_annotations import (
88
__get_constant_annotations,
9+
__get_optional_annotations,
910
__get_required_annotations,
1011
__get_unused_annotations,
1112
__qname_to_target_name,
@@ -65,7 +66,24 @@
6566
},
6667
}
6768

68-
OPTIONALS_EXPECTED: dict[str, dict[str, str]] = {}
69+
OPTIONALS_EXPECTED: dict[str, dict[str, str]] = {
70+
"test/test/commonly_used_global_required_and_optional_function/required_that_should_be_optional": {
71+
"target": "test/test/commonly_used_global_required_and_optional_function/required_that_should_be_optional",
72+
"defaultType": "string",
73+
"defaultValue": "miau",
74+
},
75+
"test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_optional": {
76+
"target": "test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_optional",
77+
"defaultType": "string",
78+
"defaultValue": "captain_morgan",
79+
},
80+
"test/test/commonly_used_global_required_and_optional_function/commonly_used_almost_required": {
81+
"target": "test/test/commonly_used_global_required_and_optional_function/commonly_used_almost_required",
82+
"defaultType": "string",
83+
"defaultValue": "marvel",
84+
},
85+
}
86+
6987
BOUNDARIES_EXPECTED: dict[str, dict[str, str]] = {}
7088
ENUMS_EXPECTED: dict[str, dict[str, str]] = {}
7189

@@ -151,6 +169,16 @@ def test_get_required():
151169
} == REQUIREDS_EXPECTED
152170

153171

172+
def test_get_optional():
173+
usages, api, usages_file, api_file, usages_json_path, api_json_path = setup()
174+
annotations = AnnotationStore()
175+
_preprocess_usages(usages, api)
176+
__get_optional_annotations(usages, api, annotations)
177+
assert {
178+
annotation.target: annotation.to_json() for annotation in annotations.optionals
179+
} == OPTIONALS_EXPECTED
180+
181+
154182
def test_generate():
155183
usages, api, usages_file, api_file, usages_json_path, api_json_path = setup()
156184
out_file_path = os.path.join(

0 commit comments

Comments
 (0)