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 6b0e74e commit 46e75fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
26 changes: 16 additions & 10 deletions src/safeds_stubgen/api_analyzer/_ast_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,18 +554,19 @@ def _parse_parameter_data(self, node: mp_nodes.FuncDef, function_id: str) -> lis
raise ValueError("Argument has no type.")

type_annotation = argument.type_annotation
if (isinstance(type_annotation, mp_types.UnboundType) and
type_annotation.name == "list" and
len(type_annotation.args) >= 2):
arg_type = None
if isinstance(mypy_type, mp_types.AnyType) and mp_types.TypeOfAny.unannotated:
# We try to infer the type through the default value later, if possible
pass
elif (isinstance(type_annotation, mp_types.UnboundType) and
type_annotation.name == "list" and
len(type_annotation.args) >= 2):
# A special case where the argument is a list with multiple types. We have to handle this case like this
# b/c something like list[int, str] is not allowed according to PEP and therefore not handled the normal
# way in Mypy.
arg_type = mypy_type_to_abstract_type(type_annotation)
elif type_annotation is not None:
arg_type = mypy_type_to_abstract_type(mypy_type)
else:
# We try to infer the type through the default value later, if possible
arg_type = None

# Get default value information
default_value = None
Expand All @@ -574,17 +575,22 @@ def _parse_parameter_data(self, node: mp_nodes.FuncDef, function_id: str) -> lis
if initializer is not None:
if not hasattr(initializer, "value"):
if isinstance(initializer, mp_nodes.CallExpr):
# Special case when the default is a call expression
value = None
# Special case when the default is a call expression, we set the value to tuple, so that it will
# be ignored later on when creating the default_value
value = tuple
elif hasattr(initializer, "name"):
if initializer.name == "None":
value = None
elif initializer.name == "True":
value = True
elif initializer.name == "False":
value = False
else: # pragma: no cover
raise ValueError("No value found for parameter")
else:
# Todo Frage: We don't support other classes as type hints for parameters
# (Wenn z.B. ein Parameter eine Klasse als default value hat)
# Thus we set the value to tuple, so that it will be ignored later on when creating the
# default_value
value = tuple
else: # pragma: no cover
raise ValueError("No value found for parameter")
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@
none: Nothing?
)

// TODO Safe-DS does not support required but name only parameter assignments.
@PythonName("param_position")
fun paramPosition(
self,
a,
b: Boolean,
c: Int = 1,
d: A,
d,
e: Int = 1
)

Expand Down Expand Up @@ -247,15 +248,16 @@
class InferMyTypes(
@PythonName("init_param") initParam: Int = 1
) {
static attr infer_attr: Int
@PythonName("infer_attr")
static attr inferAttr: Int
@PythonName("init_infer")
attr initInfer: Any
attr initInfer

@PythonName("infer_function")
static fun inferFunction(
@PythonName("infer_param") inferParam: Int = 1,
@PythonName("infer_param_2") inferParam2: Int = Something
) -> (result1: Boolean, result2: Int, result3: InferMyTypes, result4: Nothing?, result5: Float, result6: String, result7: SomeClass)
) -> (result1: InferMyTypes, result2: Nothing?, result3: SomeClass, result4: Boolean, result5: Float, result6: Int, result7: String)
}

'''
Expand Down

0 comments on commit 46e75fe

Please sign in to comment.