Skip to content

Commit

Permalink
fix: Fixed a bug where results in stubs would not be named (#131)
Browse files Browse the repository at this point in the history
Closes #100

### Summary of Changes
Fixed a bug where results in stubs would not be named if the docstrings
did not specify a result type. Now results named if the

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
Masara and megalinter-bot committed May 4, 2024
1 parent 48c5367 commit 4408c84
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 44 deletions.
109 changes: 65 additions & 44 deletions src/safeds_stubgen/api_analyzer/_ast_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,22 +466,33 @@ def _parse_results(
# Create Result objects and try to find a matching docstring name
all_results = []
name_generator = result_name_generator()
for type_ in return_results:
result_docstring = ResultDocstring()
for docstring in result_docstrings:
if hash(docstring.type) == hash(type_):
result_docstring = docstring
break

result_name = result_docstring.name if result_docstring.name else next(name_generator)

result = Result(
id=f"{function_id}/{result_name}",
type=type_,
name=f"{result_name}",
)

all_results.append(result)
if len(return_results) == 1 == len(result_docstrings):
result_name = result_docstrings[0].name or next(name_generator)
all_results = [
Result(
id=f"{function_id}/{result_name}",
type=return_results[0],
name=f"{result_name}",
),
]
else:
for type_ in return_results:
result_docstring = ResultDocstring()
for docstring in result_docstrings:
if hash(docstring.type) == hash(type_):
result_docstring = docstring
break

result_name = result_docstring.name or next(name_generator)

all_results.append(
Result(
id=f"{function_id}/{result_name}",
type=type_,
name=f"{result_name}",
),
)

return all_results

Expand Down Expand Up @@ -582,39 +593,49 @@ def _create_inferred_results(
# Create Result objects
name_generator = result_name_generator()
inferred_results = []
for result_list in result_array:
result_count = len(result_list)
if result_count == 1:
result_type = result_list[0]
else:
result_type = sds_types.UnionType(result_list)

# Search for matching docstrings for each result for the name
result_docstring = ResultDocstring()
if docstrings:
if isinstance(result_type, sds_types.UnionType):
possible_type: sds_types.AbstractType | None
if len(docstrings) > 1:
docstring_types = [docstring.type for docstring in docstrings if docstring.type is not None]
possible_type = sds_types.UnionType(types=docstring_types)
else:
possible_type = docstrings[0].type
if possible_type == result_type:
result_docstring = docstrings[0]
else:
for docstring in docstrings:
if hash(docstring.type) == hash(result_type):
result_docstring = docstring
break

result_name = result_docstring.name or next(name_generator)
inferred_results.append(
if 1 == len(result_array) == len(result_array[0]) == len(docstrings):
result_name = docstrings[0].name or next(name_generator)
inferred_results = [
Result(
id=f"{function_id}/{result_name}",
type=result_type,
type=result_array[0][0],
name=result_name,
),
)
]
else:
for result_list in result_array:
result_count = len(result_list)
if result_count == 1:
result_type = result_list[0]
else:
result_type = sds_types.UnionType(result_list)

# Search for matching docstrings for each result for the name
result_docstring = ResultDocstring()
if docstrings:
if isinstance(result_type, sds_types.UnionType):
possible_type: sds_types.AbstractType | None
if len(docstrings) > 1:
docstring_types = [docstring.type for docstring in docstrings if docstring.type is not None]
possible_type = sds_types.UnionType(types=docstring_types)
else:
possible_type = docstrings[0].type
if possible_type == result_type:
result_docstring = docstrings[0]
else:
for docstring in docstrings:
if hash(docstring.type) == hash(result_type):
result_docstring = docstring
break

result_name = result_docstring.name or next(name_generator)
inferred_results.append(
Result(
id=f"{function_id}/{result_name}",
type=result_type,
name=result_name,
),
)

return inferred_results

Expand Down
24 changes: 24 additions & 0 deletions tests/data/docstring_parser_package/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,27 @@ def numpy_sequence_types(a: SequenceTypeVar[list]) -> SequenceTypeVar2[int]:
this will be the return value
"""
pass


def numpy_named_result_without_type() -> str:
"""
numpy_named_result_without_type
Returns
-------
named_result :
this will be the return value
"""
return "result"


def numpy_named_result_without_type_inferred():
"""
numpy_named_result_without_type_inferred
Returns
-------
named_result :
this will be the return value
"""
return "result"
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ fun numpySequenceTypes(
a: SequenceTypeVar<List<Any>>
) -> namedResult: SequenceTypeVar2<Int>

/**
* numpy_named_result_without_type
*
* @result namedResult this will be the return value
*/
@Pure
@PythonName("numpy_named_result_without_type")
fun numpyNamedResultWithoutType() -> namedResult: String

/**
* numpy_named_result_without_type_inferred
*
* @result namedResult this will be the return value
*/
@Pure
@PythonName("numpy_named_result_without_type_inferred")
fun numpyNamedResultWithoutTypeInferred() -> namedResult: String

/**
* ClassWithDocumentation. Code::
*
Expand Down

0 comments on commit 4408c84

Please sign in to comment.