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

Commit 784d447

Browse files
author
GideonKoenig
committed
Add tests and prepare for merge with issue #441
1 parent e83df6e commit 784d447

File tree

5 files changed

+501
-235
lines changed

5 files changed

+501
-235
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,16 @@ def __get_default_type_from_value(default_value: str) -> tuple[str, str]:
192192

193193

194194
def __get_required_annotations(usages: UsageStore, api: API) -> dict[str, dict[str, dict[str, str]]]:
195+
"""
196+
Returns all required annotations
197+
198+
:param usages: Usage store
199+
:param api: Description of the API
200+
"""
195201
result = {}
196202

197203
parameters = (api.parameters())
204+
# Takes all parameters with default value
198205
optional_parameter = [(it, parameters[it]) for it in parameters if parameters[it].default_value is not None]
199206

200207
for qname, parameter in optional_parameter:
@@ -208,6 +215,11 @@ def __get_required_annotations(usages: UsageStore, api: API) -> dict[str, dict[s
208215

209216

210217
def __get_parameter_type(values: list[tuple[str, int]]) -> (ParameterType, str):
218+
"""
219+
Returns a tuple of the parameter Typ and parameter value
220+
221+
:param values: list of tuples where the first value represents the parameter name and the second represents the count of occurrences
222+
"""
211223
if len(values) == 0:
212224
return ParameterType.Unused, None
213225
elif len(values) == 1:
@@ -216,9 +228,9 @@ def __get_parameter_type(values: list[tuple[str, int]]) -> (ParameterType, str):
216228
n = len(values)
217229
m = sum([count for value, count in values])
218230

219-
most_used_value, seconds_most_used_value = sorted(values, key=lambda tup: tup[1])[:2]
231+
seconds_most_used_value, most_used_value = sorted(values, key=lambda tup: tup[1])[-2:]
220232

221-
if most_used_value[1] - seconds_most_used_value[1] <= n/m:
233+
if most_used_value[1] - seconds_most_used_value[1] <= m/n:
222234
return ParameterType.Required, None
223235
else:
224236
return ParameterType.Optional, most_used_value[0]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import json
2+
import os
3+
import pytest
4+
5+
from package_parser.commands.find_usages import UsageStore
6+
from package_parser.commands.generate_annotations.generate_annotations import (
7+
generate_annotations, __get_unused_annotations, __get_constant_annotations, ____qname_to_target_name,
8+
_preprocess_usages)
9+
from package_parser.commands.get_api import API
10+
11+
REQUIRED_EXPECTED = {"constant": {
12+
'test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_required':
13+
{'target': 'test/test/commonly_used_global_required_and_optional_function/optional_that_should_be_required'},
14+
'test/test/commonly_used_global_required_and_optional_function/required_that_should_be_required':
15+
{'target': 'test/test/commonly_used_global_required_and_optional_function/required_that_should_be_required'},
16+
'test/test/commonly_used_global_required_and_optional_function/commonly_used_barely_required':
17+
{'target': 'test/test/commonly_used_global_required_and_optional_function/commonly_used_barely_required'}}
18+
}
19+
20+
21+
# Reihenfolge ist wichtig, siehe Reihenfolge von annotation_functions in generate_annotations.py
22+
FULL_EXPECTED = {**UNUSED_EXPECTED, **CONSTANT_EXPECTED, **REQUIRED_EXPECTED}
23+
24+
def test_required_annotation():
25+
pass

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

Lines changed: 0 additions & 209 deletions
This file was deleted.

package-parser/tests/data/requried/api_data.json renamed to package-parser/tests/data/requried_and_optional/api_data.json

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"classes": [],
1111
"functions": [
1212
"test.unused_global_function",
13-
"test.commonly_used_global_function"
13+
"test.commonly_used_global_required_and_optional_function"
1414
]
1515
}
1616
],
@@ -24,24 +24,14 @@
2424
"decorators": [],
2525
"parameters": [
2626
{
27-
"name": "unused_required_parameter",
27+
"name": "unused_parameter",
2828
"default_value": null,
2929
"is_public": true,
3030
"assigned_by": "POSITION_OR_NAME",
3131
"docstring": {
3232
"type": "str",
3333
"description": ""
3434
}
35-
},
36-
{
37-
"name": "unused_optional_parameter",
38-
"default_value": "'bla'",
39-
"is_public": true,
40-
"assigned_by": "POSITION_OR_NAME",
41-
"docstring": {
42-
"type": "str",
43-
"description": ""
44-
}
4535
}
4636
],
4737
"results": [],
@@ -51,14 +41,24 @@
5141
"source_code": ""
5242
},
5343
{
54-
"name": "commonly_used_global_function",
55-
"unique_name": "commonly_used_global_function",
56-
"qname": "test.commonly_used_global_function",
57-
"unique_qname": "test.commonly_used_global_function",
44+
"name": "commonly_used_global_required_and_optional_function",
45+
"unique_name": "commonly_used_global_required_and_optional_function",
46+
"qname": "test.commonly_used_global_required_and_optional_function",
47+
"unique_qname": "test.commonly_used_global_required_and_optional_function",
5848
"decorators": [],
5949
"parameters": [
6050
{
61-
"name": "useless_required_parameter",
51+
"name": "optional_that_should_be_required",
52+
"default_value": "'test'",
53+
"is_public": true,
54+
"assigned_by": "POSITION_OR_NAME",
55+
"docstring": {
56+
"type": "str",
57+
"description": ""
58+
}
59+
},
60+
{
61+
"name": "required_that_should_be_required",
6262
"default_value": null,
6363
"is_public": true,
6464
"assigned_by": "POSITION_OR_NAME",
@@ -68,7 +68,7 @@
6868
}
6969
},
7070
{
71-
"name": "useful_required_parameter",
71+
"name": "required_that_should_be_optional",
7272
"default_value": null,
7373
"is_public": true,
7474
"assigned_by": "POSITION_OR_NAME",
@@ -78,8 +78,18 @@
7878
}
7979
},
8080
{
81-
"name": "unused_optional_parameter",
82-
"default_value": "'bla'",
81+
"name": "optional_that_should_be_optional",
82+
"default_value": "'captain_morgan'",
83+
"is_public": true,
84+
"assigned_by": "POSITION_OR_NAME",
85+
"docstring": {
86+
"type": "str",
87+
"description": ""
88+
}
89+
},
90+
{
91+
"name": "commonly_used_almost_required",
92+
"default_value": "'marvel'",
8393
"is_public": true,
8494
"assigned_by": "POSITION_OR_NAME",
8595
"docstring": {
@@ -88,8 +98,8 @@
8898
}
8999
},
90100
{
91-
"name": "useless_optional_parameter",
92-
"default_value": "'bla'",
101+
"name": "commonly_used_barely_required",
102+
"default_value": "'otto'",
93103
"is_public": true,
94104
"assigned_by": "POSITION_OR_NAME",
95105
"docstring": {
@@ -98,8 +108,8 @@
98108
}
99109
},
100110
{
101-
"name": "useful_optional_parameter",
102-
"default_value": "'bla'",
111+
"name": "constant_parameter",
112+
"default_value": null,
103113
"is_public": true,
104114
"assigned_by": "POSITION_OR_NAME",
105115
"docstring": {

0 commit comments

Comments
 (0)