Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
feat: Automatisierte Herleitung von Optional Annotations (#473)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
6 people authored May 6, 2022
1 parent b0f7f79 commit fd5816a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from package_parser.models.annotation_models import (
AnnotationStore,
ConstantAnnotation,
OptionalAnnotation,
ParameterInfo,
ParameterType,
RequiredAnnotation,
Expand Down Expand Up @@ -42,6 +43,7 @@ def generate_annotations(
__get_unused_annotations,
__get_constant_annotations,
__get_required_annotations,
__get_optional_annotations,
]

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


def __get_optional_annotations(
usages: UsageStore, api: API, annotations: AnnotationStore
) -> None:
"""
Collects all parameters that are currently required but should be optional to be assign a value
:param usages: Usage store
:param api: Description of the API
:param annotations: AnnotationStore, that holds all annotations
"""
parameters = api.parameters()
all_parameter = [(it, parameters[it]) for it in parameters]

for qname, _ in all_parameter:
parameter_info = __get_parameter_info(qname, usages)

if parameter_info.type == ParameterType.Optional:
formatted_name = __qname_to_target_name(api, qname)
annotations.optionals.append(
OptionalAnnotation(
target=formatted_name,
defaultValue=parameter_info.value,
defaultType=parameter_info.value_type,
)
)


def __get_parameter_info(qname: str, usages: UsageStore) -> ParameterInfo:
"""
Returns a ParameterInfo object, that contains the type of the parameter, the value that is associated with it, and the values type
Returns a ParameterInfo object, that contains the type of the parameter, the value that is associated with it,
and the values type.
:param qname: name of the parameter
:param usages: UsageStore
:return ParameterInfo
Expand All @@ -298,6 +327,8 @@ def __get_parameter_info(qname: str, usages: UsageStore) -> ParameterInfo:
value = max(values, key=lambda item: item[1])[0]
if value[0] == "'":
value = value[1:-1]

# If its neither required nor constant, return optional
return ParameterInfo(
ParameterType.Optional, value, __get_default_type_from_value(value)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from package_parser.commands.find_usages import UsageStore
from package_parser.commands.generate_annotations.generate_annotations import (
__get_constant_annotations,
__get_optional_annotations,
__get_required_annotations,
__get_unused_annotations,
__qname_to_target_name,
Expand Down Expand Up @@ -65,7 +66,24 @@
},
}

OPTIONALS_EXPECTED: dict[str, dict[str, str]] = {}
OPTIONALS_EXPECTED: dict[str, dict[str, str]] = {
"test/test/commonly_used_global_required_and_optional_function/required_that_should_be_optional": {
"target": "test/test/commonly_used_global_required_and_optional_function/required_that_should_be_optional",
"defaultType": "string",
"defaultValue": "miau",
},
"test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_optional": {
"target": "test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_optional",
"defaultType": "string",
"defaultValue": "captain_morgan",
},
"test/test/commonly_used_global_required_and_optional_function/commonly_used_almost_required": {
"target": "test/test/commonly_used_global_required_and_optional_function/commonly_used_almost_required",
"defaultType": "string",
"defaultValue": "marvel",
},
}

BOUNDARIES_EXPECTED: dict[str, dict[str, str]] = {}
ENUMS_EXPECTED: dict[str, dict[str, str]] = {}

Expand Down Expand Up @@ -151,6 +169,16 @@ def test_get_required():
} == REQUIREDS_EXPECTED


def test_get_optional():
usages, api, usages_file, api_file, usages_json_path, api_json_path = setup()
annotations = AnnotationStore()
_preprocess_usages(usages, api)
__get_optional_annotations(usages, api, annotations)
assert {
annotation.target: annotation.to_json() for annotation in annotations.optionals
} == OPTIONALS_EXPECTED


def test_generate():
usages, api, usages_file, api_file, usages_json_path, api_json_path = setup()
out_file_path = os.path.join(
Expand Down

0 comments on commit fd5816a

Please sign in to comment.