Skip to content

Commit

Permalink
api_analyzer: Fixed a bug where lists with multiple types would not b…
Browse files Browse the repository at this point in the history
…e correctly parsed
  • Loading branch information
Masara committed Nov 13, 2023
1 parent 507e3f0 commit 912b6ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/safeds_stubgen/api_analyzer/_ast_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,18 @@ def parse_parameter_data(self, node: FuncDef, function_id: str) -> list[Paramete
mypy_type = argument.variable.type
if mypy_type is None: # pragma: no cover
raise ValueError("Argument has no type.")
arg_type = mypy_type_to_abstract_type(mypy_type)

type_annotation = argument.type_annotation
if (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)
else:
arg_type = mypy_type_to_abstract_type(mypy_type)

arg_kind = get_argument_kind(argument)

default_value = None
Expand Down
6 changes: 6 additions & 0 deletions src/safeds_stubgen/api_analyzer/_mypy_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def mypy_type_to_abstract_type(mypy_type: Instance | ProperType | MypyType) -> A
elif isinstance(mypy_type, mp_types.NoneType):
return sds_types.NamedType(name="None")
elif isinstance(mypy_type, mp_types.UnboundType):
if mypy_type.name == "list":
types = [
mypy_type_to_abstract_type(arg)
for arg in mypy_type.args
]
return sds_types.ListType(types=types)
# Todo Aliasing: Import auflösen
return sds_types.NamedType(name=mypy_type.name)
elif isinstance(mypy_type, mp_types.TypeType):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
@PythonName("illegal_params")
fun illegalParams(
lst: List<Int, String>,
@PythonName("lst_2") lst2: List<Int, String>,
@PythonName("lst_2") lst2: List<Int, String, Int>,
tpl: Tuple<Int, String, Boolean, Int>,
_: Int = String
) -> result_1: Any
Expand Down

0 comments on commit 912b6ca

Please sign in to comment.