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

Commit e83df6e

Browse files
author
GideonKoenig
committed
Code should be working - Still need to write a test
1 parent 1284bff commit e83df6e

File tree

4 files changed

+373
-5
lines changed

4 files changed

+373
-5
lines changed

package-parser/package-parser.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<sourceFolder url="file://$MODULE_DIR$/refined_types" type="java-resource" />
99
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
1010
</content>
11-
<orderEntry type="jdk" jdkName="Poetry (api-editor)" jdkType="Python SDK" />
11+
<orderEntry type="jdk" jdkName="Poetry (package-parser)" jdkType="Python SDK" />
1212
<orderEntry type="sourceFolder" forTests="false" />
1313
</component>
14-
</module>
14+
</module>

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from __future__ import annotations
2+
13
import json
24
from io import TextIOWrapper
35
from pathlib import Path
46
from typing import Any
7+
from enum import Enum, auto
58

69
from package_parser.commands.find_usages import (
710
ClassUsage,
@@ -123,9 +126,7 @@ def __add_implicit_usages_of_default_value(usages: UsageStore, api: API) -> None
123126
usages.add_value_usage(parameter_qname, default_value, location)
124127

125128

126-
def __find_constant_parameters(
127-
usages: UsageStore, api: API
128-
) -> dict[str, dict[str, str]]:
129+
def __find_constant_parameters(usages: UsageStore, api: API) -> dict[str, dict[str, str]]:
129130
"""
130131
Returns all parameters that are only ever assigned a single value.
131132
@@ -188,3 +189,43 @@ def __get_default_type_from_value(default_value: str) -> tuple[str, str]:
188189
default_value = default_value
189190

190191
return default_type, default_value
192+
193+
194+
def __get_required_annotations(usages: UsageStore, api: API) -> dict[str, dict[str, dict[str, str]]]:
195+
result = {}
196+
197+
parameters = (api.parameters())
198+
optional_parameter = [(it, parameters[it]) for it in parameters if parameters[it].default_value is not None]
199+
200+
for qname, parameter in optional_parameter:
201+
values = usages.value_usages[qname].items()
202+
values = [(it[0], len(it[1])) for it in values]
203+
if __get_parameter_type(values)[0] is ParameterType.Required:
204+
target_name = __qname_to_target_name(api, qname)
205+
result[target_name] = {"target": target_name}
206+
207+
return {"requireds": result}
208+
209+
210+
def __get_parameter_type(values: list[tuple[str, int]]) -> (ParameterType, str):
211+
if len(values) == 0:
212+
return ParameterType.Unused, None
213+
elif len(values) == 1:
214+
return ParameterType.Constant, values[0][0]
215+
216+
n = len(values)
217+
m = sum([count for value, count in values])
218+
219+
most_used_value, seconds_most_used_value = sorted(values, key=lambda tup: tup[1])[:2]
220+
221+
if most_used_value[1] - seconds_most_used_value[1] <= n/m:
222+
return ParameterType.Required, None
223+
else:
224+
return ParameterType.Optional, most_used_value[0]
225+
226+
227+
class ParameterType(Enum):
228+
Constant = 0
229+
Optional = 1
230+
Required = 2
231+
Unused = 3
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"distribution": "test",
3+
"package": "test",
4+
"version": "0.0.1",
5+
"modules": [
6+
{
7+
"name": "test",
8+
"imports": [],
9+
"from_imports": [],
10+
"classes": [],
11+
"functions": [
12+
"test.unused_global_function",
13+
"test.commonly_used_global_function"
14+
]
15+
}
16+
],
17+
"classes": [],
18+
"functions": [
19+
{
20+
"name": "unused_global_function",
21+
"unique_name": "unused_global_function",
22+
"qname": "test.unused_global_function",
23+
"unique_qname": "test.unused_global_function",
24+
"decorators": [],
25+
"parameters": [
26+
{
27+
"name": "unused_required_parameter",
28+
"default_value": null,
29+
"is_public": true,
30+
"assigned_by": "POSITION_OR_NAME",
31+
"docstring": {
32+
"type": "str",
33+
"description": ""
34+
}
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+
}
45+
}
46+
],
47+
"results": [],
48+
"is_public": true,
49+
"description": "",
50+
"docstring": "",
51+
"source_code": ""
52+
},
53+
{
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",
58+
"decorators": [],
59+
"parameters": [
60+
{
61+
"name": "useless_required_parameter",
62+
"default_value": null,
63+
"is_public": true,
64+
"assigned_by": "POSITION_OR_NAME",
65+
"docstring": {
66+
"type": "str",
67+
"description": ""
68+
}
69+
},
70+
{
71+
"name": "useful_required_parameter",
72+
"default_value": null,
73+
"is_public": true,
74+
"assigned_by": "POSITION_OR_NAME",
75+
"docstring": {
76+
"type": "str",
77+
"description": ""
78+
}
79+
},
80+
{
81+
"name": "unused_optional_parameter",
82+
"default_value": "'bla'",
83+
"is_public": true,
84+
"assigned_by": "POSITION_OR_NAME",
85+
"docstring": {
86+
"type": "str",
87+
"description": ""
88+
}
89+
},
90+
{
91+
"name": "useless_optional_parameter",
92+
"default_value": "'bla'",
93+
"is_public": true,
94+
"assigned_by": "POSITION_OR_NAME",
95+
"docstring": {
96+
"type": "str",
97+
"description": ""
98+
}
99+
},
100+
{
101+
"name": "useful_optional_parameter",
102+
"default_value": "'bla'",
103+
"is_public": true,
104+
"assigned_by": "POSITION_OR_NAME",
105+
"docstring": {
106+
"type": "str",
107+
"description": ""
108+
}
109+
}
110+
],
111+
"results": [],
112+
"is_public": true,
113+
"description": "",
114+
"docstring": "",
115+
"source_code": ""
116+
}
117+
]
118+
}

0 commit comments

Comments
 (0)