Skip to content

Commit

Permalink
Added handling for results with operations (boolean results) and fixe…
Browse files Browse the repository at this point in the history
…d a few bugs regarding the reexport check
  • Loading branch information
Masara committed Aug 25, 2024
1 parent 3921da7 commit fc46724
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/safeds_stubgen/api_analyzer/_mypy_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ def mypy_expression_to_sds_type(expr: mp_nodes.Expression) -> sds_types.Abstract
elif isinstance(expr, mp_nodes.TupleExpr):
return sds_types.TupleType(types=[mypy_expression_to_sds_type(item) for item in expr.items])
elif isinstance(expr, mp_nodes.UnaryExpr):
if expr.op == "not":
return sds_types.NamedType(name="bool", qname="builtins.bool")
return mypy_expression_to_sds_type(expr.expr)
elif ((isinstance(expr, mp_nodes.OpExpr) and expr.op in {"or", "and"}) or
(isinstance(expr, mp_nodes.ComparisonExpr) and ("is not" in expr.operators or "is" in expr.operators))):
return sds_types.NamedType(name="bool", qname="builtins.bool")

logging.warning(
"Could not parse a parameter or return type for a function: Safe-DS does not support "
Expand Down
2 changes: 1 addition & 1 deletion src/safeds_stubgen/stubs_generator/_generate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def generate_stub_data(
shortest_path, alias = _get_shortest_public_reexport(
reexport_map=api.reexport_map,
name=module.name,
qname="",
qname=module.id,
is_module=True,
)
if shortest_path:
Expand Down
21 changes: 16 additions & 5 deletions src/safeds_stubgen/stubs_generator/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def _get_shortest_public_reexport(
is_module: bool,
) -> tuple[str, str]:
parent_name = ""
if not is_module and qname:
qname_parts = qname.split(".")
if len(qname_parts) > 2:
parent_name = qname_parts[-2]
qname = qname.replace("/", ".")
qname_parts = qname.split(".")
if not is_module and qname and len(qname_parts) > 2:
parent_name = qname_parts[-2]

def _module_name_check(text: str, is_wildcard: bool = False) -> bool:
if is_module:
Expand All @@ -66,7 +66,18 @@ def _module_name_check(text: str, is_wildcard: bool = False) -> bool:
or (parent_name != "" and text.endswith(f"{parent_name}.*"))
)

keys = [reexport_key for reexport_key in reexport_map if _module_name_check(reexport_key)]
found_keys = [reexport_key for reexport_key in reexport_map if _module_name_check(reexport_key)]

# Make sure we didn't get similar modules
keys = []
for key in found_keys:
if key.split(".")[0] == qname_parts[0] and key.split(".")[-1] == qname_parts[-1]:
if qname == key:
keys.append(key)
else:
pass

Check warning on line 78 in src/safeds_stubgen/stubs_generator/_helper.py

View check run for this annotation

Codecov / codecov/patch

src/safeds_stubgen/stubs_generator/_helper.py#L78

Added line #L78 was not covered by tests
else:
keys.append(key)

module_ids = set()
for key in keys:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _create_module_string(self, module: Module) -> tuple[str, str]:
package_info, _ = _get_shortest_public_reexport(
reexport_map=self.api.reexport_map,
name=module.name,
qname="",
qname=module.id,
is_module=True,
)

Expand Down
16 changes: 16 additions & 0 deletions tests/data/various_modules_package/function_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,19 @@ def type_alias_param(values: ArrayLike) -> ArrayLike:

def alias_subclass_result_type() -> ArrayLike | np.ndarray:
...


def different_result_operants(y):
if y:
return False
elif y - 1:
return y or y - 1
elif y - 2:
return y and y + 1
elif y - 3:
return y or y - 1 and y - 2
elif y - 4:
return y is not None
elif y - 5:
return y is None
return not y
27 changes: 25 additions & 2 deletions tests/safeds_stubgen/api_analyzer/__snapshots__/test__get_api.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -4973,8 +4973,8 @@
'name': 'not_true',
'type': dict({
'kind': 'NamedType',
'name': 'int',
'qname': 'builtins.int',
'name': 'bool',
'qname': 'builtins.bool',
}),
}),
])
Expand Down Expand Up @@ -6160,6 +6160,28 @@
'tests/data/various_modules_package/function_module/dictionary_results_no_key_no_value/result_1',
]),
}),
dict({
'docstring': dict({
'description': '',
'examples': list([
]),
'full_docstring': '',
}),
'id': 'tests/data/various_modules_package/function_module/different_result_operants',
'is_class_method': False,
'is_property': False,
'is_public': True,
'is_static': False,
'name': 'different_result_operants',
'parameters': list([
'tests/data/various_modules_package/function_module/different_result_operants/y',
]),
'reexported_by': list([
]),
'results': list([
'tests/data/various_modules_package/function_module/different_result_operants/result_1',
]),
}),
dict({
'docstring': dict({
'description': '',
Expand Down Expand Up @@ -7955,6 +7977,7 @@
'tests/data/various_modules_package/function_module/return_without_result',
'tests/data/various_modules_package/function_module/type_alias_param',
'tests/data/various_modules_package/function_module/alias_subclass_result_type',
'tests/data/various_modules_package/function_module/different_result_operants',
]),
'id': 'tests/data/various_modules_package/function_module',
'name': 'function_module',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fun specialParams(
@PythonName("none_none_bool_none_union") noneNoneBoolNoneUnion: Boolean?,
@PythonName("none_list_union_none_none") noneListUnionNoneNone: List<Nothing?>?,
none: Nothing?,
@PythonName("not_true") notTrue: Int = unknown
@PythonName("not_true") notTrue: Boolean = unknown
)

// TODO Result type information missing.
Expand Down Expand Up @@ -355,6 +355,13 @@ fun typeAliasParam(
@PythonName("alias_subclass_result_type")
fun aliasSubclassResultType() -> result1: union<ArrayLike, ndarray>

// TODO Some parameter have no type information.
@Pure
@PythonName("different_result_operants")
fun differentResultOperants(
y
) -> result1: Boolean

class FunctionModuleClassA()

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

0 comments on commit fc46724

Please sign in to comment.