Skip to content

Commit

Permalink
fix: Generated names of callback results start with result, not with …
Browse files Browse the repository at this point in the history
…param (#104)

Closes #85

### Summary of Changes

Fixed the bug where generated names of callback results started with
`param` instead of `result`.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
Masara and megalinter-bot authored Apr 7, 2024
1 parent 9b2f802 commit 6e696e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
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

0 comments on commit 6e696e9

Please sign in to comment.