Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Generated names of callback results start with result, not with param #104

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/safeds_stubgen/stubs_generator/_generate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
)

if TYPE_CHECKING:
from collections.abc import Generator

from safeds_stubgen.docstring_parsing import AttributeDocstring, ClassDocstring, FunctionDocstring

Expand Down Expand Up @@ -701,21 +700,27 @@ def _create_type_string(self, type_data: dict | None) -> str:
elif kind == "FinalType":
return self._create_type_string(type_data["type"])
elif kind == "CallableType":
name_generator = _callable_type_name_generator()

params = [
f"{next(name_generator)}: {self._create_type_string(parameter_type)}"
for parameter_type in type_data["parameter_types"]
(
f"{_convert_name_to_convention('param_' + str(i + 1), self.naming_convention)}: "
f"{self._create_type_string(parameter_type)}"
)
for i, parameter_type in enumerate(type_data["parameter_types"])
]

return_type = type_data["return_type"]
if return_type["kind"] == "TupleType":
return_types = [
f"{next(name_generator)}: {self._create_type_string(type_)}" for type_ in return_type["types"]
(
f"{_convert_name_to_convention('result_' + str(i+1), self.naming_convention)}: "
f"{self._create_type_string(type_)}"
)
for i, type_ in enumerate(return_type["types"])
]
return_type_string = f"({', '.join(return_types)})"
else:
return_type_string = f"{next(name_generator)}: {self._create_type_string(return_type)}"
result_name = _convert_name_to_convention("result_1", self.naming_convention)
return_type_string = f"{result_name}: {self._create_type_string(return_type)}"

return f"({', '.join(params)}) -> {return_type_string}"
elif kind in {"SetType", "ListType"}:
Expand Down Expand Up @@ -1035,13 +1040,6 @@ def _create_docstring_description_part(description: str, indentations: str) -> s
return full_docstring + "\n"


def _callable_type_name_generator() -> Generator:
"""Generate a name for callable type parameters starting from 'a' until 'zz'."""
while True:
for x in range(1, 1000):
yield f"param{x}"


def _create_name_annotation(name: str) -> str:
return f'@PythonName("{name}")'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ fun anyResults() -> result1: Any
@Pure
@PythonName("callable_type")
fun callableType(
param: (param1: String) -> (param2: Int, param3: String)
) -> result1: (param1: Int, param2: Int) -> param3: Int
param: (param1: String) -> (result1: Int, result2: String)
) -> result1: (param1: Int, param2: Int) -> result1: Int

// TODO Result type information missing.
// TODO Some parameter have no type information.
Expand Down