Skip to content

Commit

Permalink
Fixed bugs and tests, added qnames for some NamedTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
Masara committed Nov 27, 2023
1 parent 4aea41e commit 4bff2cb
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 35 deletions.
14 changes: 13 additions & 1 deletion src/safeds_stubgen/api_analyzer/_ast_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def _mypy_expression_to_sds_type(self, expr: mp_nodes.Expression) -> sds_types.N
if expr.name in {"False", "True"}:
return sds_types.NamedType(name="bool")
else:
return sds_types.NamedType(name=expr.name)
return sds_types.NamedType(name=expr.name, qname=expr.fullname)
elif isinstance(expr, mp_nodes.IntExpr):
return sds_types.NamedType(name="int")
elif isinstance(expr, mp_nodes.FloatExpr):
Expand Down Expand Up @@ -476,6 +476,18 @@ def _create_result(self, node: mp_nodes.FuncDef, function_id: str) -> list[Resul

return results

elif isinstance(ret_type, sds_types.TupleType):
return [
Result(
id=f"{function_id}/result_{i + 1}",
type=type_,
is_type_inferred=is_type_inferred,
name=f"result_{i + 1}",
docstring=self.docstring_parser.get_result_documentation(node),
)
for i, type_ in enumerate(ret_type.types)
]

return [Result(
id=f"{function_id}/result_1",
type=ret_type,
Expand Down
4 changes: 2 additions & 2 deletions src/safeds_stubgen/api_analyzer/_mypy_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def mypy_type_to_abstract_type(
mypy_type_to_abstract_type(arg)
for arg in mypy_type.args
])
# Todo Aliasing: Import auflösen
# Todo Aliasing: Import auflösen, wir können wir keinen fullname (qname) bekommen
return sds_types.NamedType(name=mypy_type.name)
elif isinstance(mypy_type, mp_types.TypeType):
# The first parameter of cls methods
Expand Down Expand Up @@ -119,7 +119,7 @@ def mypy_type_to_abstract_type(

return sds_types.DictType(key_type=key_type, value_type=value_type)
else:
return sds_types.NamedType(name=type_name)
return sds_types.NamedType(name=type_name, qname=mypy_type.type.fullname)
raise ValueError("Unexpected type.")


Expand Down
1 change: 1 addition & 0 deletions tests/data/test_main/main_test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ac_alias = AnotherClass


# Todo Frage: Ist bei den Stubs param_2 optional? Wird None als default value unterstützt?
# noinspection PyUnusedLocal
def global_func(param_1: str = "first param", param_2: ac_alias | None = None) -> ac_alias:
"""Docstring 1.
Expand Down
2 changes: 1 addition & 1 deletion tests/data/test_package/function_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def public_no_params_no_result(): ...
def params(
integer: int,
boolean: bool,
float_: 1.2,
float_: float,
none: None,
string: str,
obj: FunctionModuleClassA,
Expand Down
13 changes: 11 additions & 2 deletions tests/data/test_package/infer_types_module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
class InferMe:
...


class InferMyTypes:
infer_attr = 1
infer_int = 1
infer_float = 1.2
infer_bool = False
infer_str = "String"
infer_none = None
infer_obj = InferMe # Todo Frage: Ist "static attr inferObj: () -> a: InferMe" richtig?

def __init__(self, init_param=1):
self.init_infer = 3
Expand Down Expand Up @@ -33,6 +42,6 @@ def infer_function(infer_param=1, infer_param_2: int = "Something"):

for _ in (1, 2):
if infer_param_2:
return ModuleClass
return InferMe

return int
16 changes: 8 additions & 8 deletions tests/safeds_stubgen/__snapshots__/test_main.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'AnotherClass',
'qname': '',
'qname': 'tests.data.test_main.another_path.another_module.AnotherClass',
}),
}),
dict({
Expand Down Expand Up @@ -725,7 +725,7 @@
'type': '',
}),
'id': 'test_main/main_test_module/global_func/param_2',
'is_optional': True,
'is_optional': False,
'is_type_inferred': False,
'name': 'param_2',
'type': dict({
Expand All @@ -734,7 +734,7 @@
dict({
'kind': 'NamedType',
'name': 'AnotherClass',
'qname': '',
'qname': 'tests.data.test_main.another_path.another_module.AnotherClass',
}),
dict({
'kind': 'NamedType',
Expand Down Expand Up @@ -786,7 +786,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'AnotherClass',
'qname': '',
'qname': 'tests.data.test_main.another_path.another_module.AnotherClass',
}),
}),
dict({
Expand All @@ -803,17 +803,17 @@
dict({
'kind': 'NamedType',
'name': 'AnotherClass',
'qname': '',
'qname': 'tests.data.test_main.another_path.another_module.AnotherClass',
}),
dict({
'kind': 'NamedType',
'name': 'AnotherClass',
'qname': '',
'qname': 'tests.data.test_main.another_path.another_module.AnotherClass',
}),
dict({
'kind': 'NamedType',
'name': 'AnotherClass',
'qname': '',
'qname': 'tests.data.test_main.another_path.another_module.AnotherClass',
}),
]),
}),
Expand All @@ -829,7 +829,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'AnotherClass',
'qname': '',
'qname': 'tests.data.test_main.another_path.another_module.AnotherClass',
}),
}),
]),
Expand Down
46 changes: 31 additions & 15 deletions tests/safeds_stubgen/api_analyzer/__snapshots__/test__get_api.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
dict({
'kind': 'NamedType',
'name': 'AttributesClassA',
'qname': '',
'qname': 'tests.data.test_package.attribute_module.AttributesClassA',
}),
]),
}),
Expand Down Expand Up @@ -628,7 +628,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'AttributesClassA',
'qname': '',
'qname': 'tests.data.test_package.attribute_module.AttributesClassA',
}),
}),
dict({
Expand Down Expand Up @@ -1905,7 +1905,12 @@
# name: test_classes_InferMyTypes
dict({
'attributes': list([
'test_package/infer_types_module/InferMyTypes/infer_attr',
'test_package/infer_types_module/InferMyTypes/infer_int',
'test_package/infer_types_module/InferMyTypes/infer_float',
'test_package/infer_types_module/InferMyTypes/infer_bool',
'test_package/infer_types_module/InferMyTypes/infer_str',
'test_package/infer_types_module/InferMyTypes/infer_none',
'test_package/infer_types_module/InferMyTypes/infer_obj',
'test_package/infer_types_module/InferMyTypes/init_infer',
]),
'classes': list([
Expand Down Expand Up @@ -2124,7 +2129,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'A',
'qname': '',
'qname': 'tests.data.test_package.variance_module.A',
}),
'variance_type': 'CONTRAVARIANT',
}),
Expand Down Expand Up @@ -3425,7 +3430,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'FunctionModuleClassA',
'qname': '',
'qname': 'tests.data.test_package.function_module.FunctionModuleClassA',
}),
}),
dict({
Expand Down Expand Up @@ -4056,6 +4061,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'float',
'qname': '',
}),
}),
dict({
Expand All @@ -4069,6 +4075,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'int',
'qname': '',
}),
}),
])
Expand Down Expand Up @@ -4236,27 +4243,32 @@
'types': list([
dict({
'kind': 'NamedType',
'name': 'float',
'name': 'int',
'qname': '',
}),
dict({
'kind': 'NamedType',
'name': 'ModuleClass',
'qname': '',
'name': 'InferMe',
'qname': 'tests.data.test_package.infer_types_module.InferMe',
}),
dict({
'kind': 'NamedType',
'name': 'int',
'name': 'None',
'qname': 'builtins.None',
}),
dict({
'kind': 'NamedType',
'name': 'float',
'qname': '',
}),
dict({
'kind': 'NamedType',
'name': 'bool',
'qname': '',
'qname': 'builtins.bool',
}),
dict({
'kind': 'NamedType',
'name': 'None',
'name': 'str',
'qname': '',
}),
dict({
Expand All @@ -4267,12 +4279,12 @@
dict({
'kind': 'NamedType',
'name': 'InferMyTypes',
'qname': '',
'qname': 'tests.data.test_package.infer_types_module.InferMyTypes',
}),
dict({
'kind': 'NamedType',
'name': 'str',
'qname': '',
'name': 'int',
'qname': 'builtins.int',
}),
]),
}),
Expand Down Expand Up @@ -4330,7 +4342,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'FunctionModuleClassA',
'qname': '',
'qname': 'tests.data.test_package.function_module.FunctionModuleClassA',
}),
}),
])
Expand All @@ -4348,6 +4360,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'str',
'qname': '',
}),
}),
dict({
Expand All @@ -4361,6 +4374,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'int',
'qname': '',
}),
}),
dict({
Expand All @@ -4374,6 +4388,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'bool',
'qname': '',
}),
}),
dict({
Expand All @@ -4387,6 +4402,7 @@
'type': dict({
'kind': 'NamedType',
'name': 'FunctionModuleClassA',
'qname': 'tests.data.test_package.function_module.FunctionModuleClassA',
}),
}),
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from abc import abstractmethod

class AbstractModuleClass {
// TODO Safe-DS does not support tuple types.
@PythonName("abstract_property_method") attr abstractPropertyMethod: Tuple<Float, Int>
@PythonName("abstract_property_method") attr abstractPropertyMethod: union<Float, Int>

// TODO Result type information missing.
@Pure
Expand Down Expand Up @@ -311,7 +310,6 @@
@PythonName("one_result")
fun oneResult() -> result1: Int

// TODO Safe-DS does not support tuple types.
@Pure
@PythonName("multiple_results")
fun multipleResults() -> (result1: String, result2: Int, result3: Boolean, result4: FunctionModuleClassA)
Expand Down Expand Up @@ -394,11 +392,23 @@
@PythonModule("test_package.infer_types_module")
package testPackage.inferTypesModule

class InferMe()

class InferMyTypes(
@PythonName("init_param") initParam: Int = 1
) {
@PythonName("infer_attr")
static attr inferAttr: Int
@PythonName("infer_int")
static attr inferInt: Int
@PythonName("infer_float")
static attr inferFloat: Float
@PythonName("infer_bool")
static attr inferBool: Boolean
@PythonName("infer_str")
static attr inferStr: String
@PythonName("infer_none")
static attr inferNone: Nothing?
@PythonName("infer_obj")
static attr inferObj: () -> a: InferMe
// TODO Attribute has no type information.
@PythonName("init_infer")
attr initInfer
Expand All @@ -408,7 +418,7 @@
static fun inferFunction(
@PythonName("infer_param") inferParam: Int = 1,
@PythonName("infer_param_2") inferParam2: Int = Something
) -> (result1: union<Boolean, Float, InferMyTypes, Int, ModuleClass, Nothing?, String>, result2: union<Float, Int>, result3: Float)
) -> (result1: union<Boolean, Float, InferMe, InferMyTypes, Int, Nothing?, String>, result2: union<Float, Int>, result3: Float)
}

'''
Expand Down

0 comments on commit 4bff2cb

Please sign in to comment.