Skip to content

Commit

Permalink
fix: Translation of callable (#102)
Browse files Browse the repository at this point in the history
Closes #88

### Summary of Changes

Fixed the translation of callables the way it was descripted in issue
#88.
  • Loading branch information
Masara authored Apr 10, 2024
1 parent 8e7aa5d commit c581e6a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/safeds_stubgen/stubs_generator/_generate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,8 @@ def _create_type_string(self, type_data: dict | None) -> str:
for i, type_ in enumerate(return_type["types"])
]
return_type_string = f"({', '.join(return_types)})"
elif return_type["kind"] == "NamedType" and return_type["name"] == "None":
return f"({', '.join(params)}) -> ()"
else:
result_name = _convert_name_to_convention("result_1", self.naming_convention)
return_type_string = f"{result_name}: {self._create_type_string(return_type)}"
Expand Down
5 changes: 3 additions & 2 deletions tests/data/various_modules_package/function_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def params(
optional: Optional[int],
tuple_: tuple[int, str, bool],
literal: Literal["Some String"],
any_: Any
any_: Any,
callable_none: Callable[[int, float], None] | None,
): ...


Expand All @@ -75,7 +76,7 @@ def params_with_default_value(
optional: Optional[int] = None,
tuple_: tuple[int, str, bool] = (1, "2", True),
literal: Literal["Some String"] = "Some String",
any_: Any = False
any_: Any = False,
): ...


Expand Down
43 changes: 43 additions & 0 deletions tests/safeds_stubgen/api_analyzer/__snapshots__/test__get_api.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3729,6 +3729,48 @@
'qname': 'builtins.bool',
}),
}),
dict({
'assigned_by': 'POSITION_OR_NAME',
'default_value': None,
'docstring': dict({
'default_value': '',
'description': '',
'type': None,
}),
'id': 'tests/data/various_modules_package/function_module/params/callable_none',
'is_optional': False,
'name': 'callable_none',
'type': dict({
'kind': 'UnionType',
'types': list([
dict({
'kind': 'CallableType',
'parameter_types': list([
dict({
'kind': 'NamedType',
'name': 'int',
'qname': 'builtins.int',
}),
dict({
'kind': 'NamedType',
'name': 'float',
'qname': 'builtins.float',
}),
]),
'return_type': dict({
'kind': 'NamedType',
'name': 'None',
'qname': 'builtins.None',
}),
}),
dict({
'kind': 'NamedType',
'name': 'None',
'qname': 'builtins.None',
}),
]),
}),
}),
dict({
'assigned_by': 'POSITION_OR_NAME',
'default_value': None,
Expand Down Expand Up @@ -6021,6 +6063,7 @@
'tests/data/various_modules_package/function_module/params/tuple_',
'tests/data/various_modules_package/function_module/params/literal',
'tests/data/various_modules_package/function_module/params/any_',
'tests/data/various_modules_package/function_module/params/callable_none',
]),
'reexported_by': list([
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ fun params(
optional: Int?,
@PythonName("tuple_") tuple: Tuple<Int, String, Boolean>,
`literal`: literal<"Some String">,
@PythonName("any_") any: Any
@PythonName("any_") any: Any,
@PythonName("callable_none") callableNone: (param1: Int, param2: Float) -> ()?
)

// TODO Result type information missing.
Expand Down

0 comments on commit c581e6a

Please sign in to comment.