Skip to content

Commit

Permalink
Issue #34: Test and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Masara committed Nov 18, 2023
1 parent b6a7051 commit 6b0e74e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
21 changes: 14 additions & 7 deletions src/safeds_stubgen/api_analyzer/_ast_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,23 +318,27 @@ def leave_assignmentstmt(self, _: mp_nodes.AssignmentStmt) -> None:

@staticmethod
def _get_types_from_return_stmts(return_stmts: list[mp_nodes.ReturnStmt]) -> list[sds_types.AbstractType]:
types = []
types = set()
for return_stmt in return_stmts:
expr = return_stmt.expr
if isinstance(expr, mp_nodes.NameExpr):
if expr.name in {"False", "True"}:
types.append(sds_types.NamedType(name="bool"))
types.add(sds_types.NamedType(name="bool"))
else:
types.append(sds_types.NamedType(name=expr.name))
types.add(sds_types.NamedType(name=expr.name))
elif isinstance(expr, mp_nodes.IntExpr):
types.append(sds_types.NamedType(name="int"))
types.add(sds_types.NamedType(name="int"))
elif isinstance(expr, mp_nodes.FloatExpr):
types.append(sds_types.NamedType(name="float"))
types.add(sds_types.NamedType(name="float"))
elif isinstance(expr, mp_nodes.StrExpr):
types.append(sds_types.NamedType(name="str"))
types.add(sds_types.NamedType(name="str"))
else: # pragma: no cover
raise TypeError("Unexpected expression type for return type.")
return types

# We have to sort the list for the snapshot tests
return_types = list(types)
return_types.sort(key=lambda x: x.name)
return return_types

def _create_result(self, node: mp_nodes.FuncDef, function_id: str) -> list[Result]:
# __init__ functions aren't supposed to have returns, so we can ignore them
Expand Down Expand Up @@ -481,6 +485,9 @@ def _create_attribute(
# MemberExpr are constructor (__init__) attributes
if isinstance(attribute, mp_nodes.MemberExpr):
attribute_type = node.type
if isinstance(attribute_type, mp_types.AnyType) and mp_types.TypeOfAny.unannotated:
attribute_type = None

# Sometimes the is_inferred value is True even thoght has_explicit_value is False, thus we HAVE to check
# has_explicit_value, too.
is_type_inferred = node.is_inferred and node.has_explicit_value
Expand Down
29 changes: 13 additions & 16 deletions tests/safeds_stubgen/__snapshots__/test_main.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,9 @@
'id': 'test_package/test_module/InferMyTypes/init_infer',
'is_public': True,
'is_static': False,
'is_type_inferred': True,
'is_type_inferred': False,
'name': 'init_infer',
'type': dict({
'kind': 'NamedType',
'name': 'int',
}),
'type': None,
}),
dict({
'docstring': dict({
Expand Down Expand Up @@ -3243,7 +3240,7 @@
'name': 'result_1',
'type': dict({
'kind': 'NamedType',
'name': 'bool',
'name': 'InferMyTypes',
}),
}),
dict({
Expand All @@ -3256,41 +3253,41 @@
'name': 'result_2',
'type': dict({
'kind': 'NamedType',
'name': 'int',
'name': 'None',
}),
}),
dict({
'docstring': dict({
'description': '',
'type': '',
}),
'id': 'test_package/test_module/InferMyTypes/infer_function/result_4',
'id': 'test_package/test_module/InferMyTypes/infer_function/result_3',
'is_type_inferred': True,
'name': 'result_3',
'type': dict({
'kind': 'NamedType',
'name': 'InferMyTypes',
'name': 'SomeClass',
}),
}),
dict({
'docstring': dict({
'description': '',
'type': '',
}),
'id': 'test_package/test_module/InferMyTypes/infer_function/result_5',
'id': 'test_package/test_module/InferMyTypes/infer_function/result_4',
'is_type_inferred': True,
'name': 'result_4',
'type': dict({
'kind': 'NamedType',
'name': 'None',
'name': 'bool',
}),
}),
dict({
'docstring': dict({
'description': '',
'type': '',
}),
'id': 'test_package/test_module/InferMyTypes/infer_function/result_6',
'id': 'test_package/test_module/InferMyTypes/infer_function/result_5',
'is_type_inferred': True,
'name': 'result_5',
'type': dict({
Expand All @@ -3303,25 +3300,25 @@
'description': '',
'type': '',
}),
'id': 'test_package/test_module/InferMyTypes/infer_function/result_7',
'id': 'test_package/test_module/InferMyTypes/infer_function/result_6',
'is_type_inferred': True,
'name': 'result_6',
'type': dict({
'kind': 'NamedType',
'name': 'str',
'name': 'int',
}),
}),
dict({
'docstring': dict({
'description': '',
'type': '',
}),
'id': 'test_package/test_module/InferMyTypes/infer_function/result_8',
'id': 'test_package/test_module/InferMyTypes/infer_function/result_7',
'is_type_inferred': True,
'name': 'result_7',
'type': dict({
'kind': 'NamedType',
'name': 'SomeClass',
'name': 'str',
}),
}),
dict({
Expand Down

0 comments on commit 6b0e74e

Please sign in to comment.