Skip to content

Commit

Permalink
Removed ResultDocstrings class
Browse files Browse the repository at this point in the history
  • Loading branch information
Masara committed Apr 11, 2024
1 parent 7fec406 commit 134532f
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 127 deletions.
4 changes: 2 additions & 2 deletions src/safeds_stubgen/api_analyzer/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ClassDocstring,
FunctionDocstring,
ParameterDocstring,
ResultDocstrings,
ResultDocstring,
)

from ._types import AbstractType, TypeVarType
Expand Down Expand Up @@ -242,7 +242,7 @@ class Function:
is_static: bool
is_class_method: bool
is_property: bool
result_docstrings: ResultDocstrings
result_docstrings: list[ResultDocstring]
type_var_types: list[TypeVarType] = field(default_factory=list)
results: list[Result] = field(default_factory=list)
reexported_by: list[Module] = field(default_factory=list)
Expand Down
29 changes: 13 additions & 16 deletions src/safeds_stubgen/api_analyzer/_ast_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mypy.types as mp_types

import safeds_stubgen.api_analyzer._types as sds_types
from safeds_stubgen.docstring_parsing import ResultDocstring, ResultDocstrings
from safeds_stubgen.docstring_parsing import ResultDocstring

from ._api import (
API,
Expand Down Expand Up @@ -409,7 +409,7 @@ def _parse_results(
self,
node: mp_nodes.FuncDef,
function_id: str,
result_docstrings: ResultDocstrings,
result_docstrings: list[ResultDocstring],
) -> list[Result]:
# __init__ functions aren't supposed to have returns, so we can ignore them
if node.name == "__init__":
Expand Down Expand Up @@ -461,11 +461,10 @@ def _parse_results(
name_generator = result_name_generator()
for type_ in return_results:
result_docstring = ResultDocstring()
if result_docstrings.docstrings:
for docstring in result_docstrings.docstrings:
if hash(docstring.type) == hash(type_):
result_docstring = docstring
break
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)

Expand Down Expand Up @@ -518,7 +517,7 @@ def _infer_type_from_return_stmts(func_node: mp_nodes.FuncDef) -> sds_types.Tupl
@staticmethod
def _create_inferred_results(
results: sds_types.TupleType,
docstrings: ResultDocstrings,
docstrings: list[ResultDocstring],
function_id: str,
) -> list[Result]:
"""Create Result objects with inferred results.
Expand Down Expand Up @@ -581,20 +580,18 @@ def _create_inferred_results(

# Search for matching docstrings for each result for the name
result_docstring = ResultDocstring()
if docstrings.docstrings:
if docstrings:
if isinstance(result_type, sds_types.UnionType):
possible_type: sds_types.AbstractType | None
if len(docstrings.docstrings) > 1:
docstring_types = [
docstring.type for docstring in docstrings.docstrings if docstring.type is not 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.docstrings[0].type
possible_type = docstrings[0].type
if possible_type == result_type:
result_docstring = docstrings.docstrings[0]
result_docstring = docstrings[0]
else:
for docstring in docstrings.docstrings:
for docstring in docstrings:
if hash(docstring.type) == hash(result_type):
result_docstring = docstring
break
Expand Down
2 changes: 0 additions & 2 deletions src/safeds_stubgen/docstring_parsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
FunctionDocstring,
ParameterDocstring,
ResultDocstring,
ResultDocstrings,
)
from ._docstring_parser import DocstringParser
from ._docstring_style import DocstringStyle
Expand All @@ -25,5 +24,4 @@
"ParameterDocstring",
"PlaintextDocstringParser",
"ResultDocstring",
"ResultDocstrings",
]
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ClassDocstring,
FunctionDocstring,
ParameterDocstring,
ResultDocstrings,
ResultDocstring,
)


Expand Down Expand Up @@ -42,5 +42,5 @@ def get_attribute_documentation(
pass # pragma: no cover

@abstractmethod
def get_result_documentation(self, function_qname: str) -> ResultDocstrings:
def get_result_documentation(self, function_qname: str) -> list[ResultDocstring]:
pass # pragma: no cover
6 changes: 0 additions & 6 deletions src/safeds_stubgen/docstring_parsing/_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from collections.abc import Sequence
from typing import Any

from safeds_stubgen.api_analyzer import AbstractType
Expand Down Expand Up @@ -48,11 +47,6 @@ def to_dict(self) -> dict[str, Any]:
return dataclasses.asdict(self)


@dataclass(frozen=True)
class ResultDocstrings:
docstrings: Sequence[ResultDocstring]


@dataclass(frozen=True)
class ResultDocstring:
type: AbstractType | None = None
Expand Down
9 changes: 4 additions & 5 deletions src/safeds_stubgen/docstring_parsing/_docstring_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
FunctionDocstring,
ParameterDocstring,
ResultDocstring,
ResultDocstrings,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -174,12 +173,12 @@ def get_attribute_documentation(
description=last_attribute.description.strip("\n"),
)

def get_result_documentation(self, function_qname: str) -> ResultDocstrings:
def get_result_documentation(self, function_qname: str) -> list[ResultDocstring]:
# Find matching parameter docstrings
griffe_docstring = self.__get_cached_docstring(function_qname)

if griffe_docstring is None:
return ResultDocstrings(docstrings=[])
return []

all_returns = None
for docstring_section in griffe_docstring.parsed:
Expand All @@ -188,7 +187,7 @@ def get_result_documentation(self, function_qname: str) -> ResultDocstrings:
break

if not all_returns:
return ResultDocstrings(docstrings=[])
return []

# Multiple results are handled differently for numpy, since there we can define multiple named results.
if self.parser == Parser.numpy:
Expand Down Expand Up @@ -220,7 +219,7 @@ def get_result_documentation(self, function_qname: str) -> ResultDocstrings:
),
]

return ResultDocstrings(docstrings=results)
return results

@staticmethod
def _get_matching_docstrings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ClassDocstring,
FunctionDocstring,
ParameterDocstring,
ResultDocstrings,
ResultDocstring,
)
from ._helpers import get_full_docstring

Expand Down Expand Up @@ -53,5 +53,5 @@ def get_attribute_documentation(
def get_result_documentation(
self,
function_qname: str, # noqa: ARG002
) -> ResultDocstrings:
return ResultDocstrings(docstrings=[])
) -> list[ResultDocstring]:
return []
2 changes: 1 addition & 1 deletion src/safeds_stubgen/stubs_generator/_generate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ def _create_sds_docstring(
if isinstance(node, Function):
name_generator = result_name_generator()

for result_docstring in node.result_docstrings.docstrings:
for result_docstring in node.result_docstrings:
result_desc = result_docstring.description
if result_desc:
result_desc = f"\n{indentations} * ".join(result_desc.split("\n"))
Expand Down
59 changes: 25 additions & 34 deletions tests/safeds_stubgen/docstring_parsing/test_googledoc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)

# noinspection PyProtectedMember
from safeds_stubgen.docstring_parsing._docstring import AttributeDocstring, ResultDocstrings
from safeds_stubgen.docstring_parsing._docstring import AttributeDocstring

from tests.safeds_stubgen._helpers import get_specific_mypy_node

Expand Down Expand Up @@ -718,45 +718,36 @@ def test_get_attribute_documentation(
[
(
"function_with_return_value_and_type",
ResultDocstrings(
docstrings=[
ResultDocstring(
type=NamedType(name="bool", qname="builtins.bool"),
description="this will be the return value.",
),
],
),
[
ResultDocstring(
type=NamedType(name="bool", qname="builtins.bool"),
description="this will be the return value.",
),
],
),
(
"function_with_return_value_no_type",
ResultDocstrings(
docstrings=[
ResultDocstring(
type=NamedType(name="None", qname="builtins.None"),
description="None",
),
],
),
),
(
"function_without_return_value",
ResultDocstrings(docstrings=[]),
[
ResultDocstring(
type=NamedType(name="None", qname="builtins.None"),
description="None",
),
],
),
("function_without_return_value", []),
(
"function_with_multiple_results",
ResultDocstrings(
docstrings=[
ResultDocstring(
type=TupleType(
types=[
NamedType(name="int", qname="builtins.int"),
NamedType(name="bool", qname="builtins.bool"),
],
),
description="first result",
[
ResultDocstring(
type=TupleType(
types=[
NamedType(name="int", qname="builtins.int"),
NamedType(name="bool", qname="builtins.bool"),
],
),
],
),
description="first result",
),
],
),
],
ids=[
Expand All @@ -769,7 +760,7 @@ def test_get_attribute_documentation(
def test_get_result_documentation(
googlestyledoc_parser: DocstringParser,
function_name: str,
expected_result_documentation: ResultDocstrings,
expected_result_documentation: list[ResultDocstring],
) -> None:
node = get_specific_mypy_node(mypy_file, function_name)
assert isinstance(node, nodes.FuncDef)
Expand Down
46 changes: 21 additions & 25 deletions tests/safeds_stubgen/docstring_parsing/test_numpydoc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)

# noinspection PyProtectedMember
from safeds_stubgen.docstring_parsing._docstring import AttributeDocstring, ResultDocstrings
from safeds_stubgen.docstring_parsing._docstring import AttributeDocstring

from tests.safeds_stubgen._helpers import get_specific_mypy_node

Expand Down Expand Up @@ -923,35 +923,31 @@ def test_get_attribute_documentation(
[
(
"function_with_result_value_and_type",
ResultDocstrings(
docstrings=[
ResultDocstring(
type=NamedType(name="bool", qname="builtins.bool"),
description="this will be the return value",
),
],
),
[
ResultDocstring(
type=NamedType(name="bool", qname="builtins.bool"),
description="this will be the return value",
),
],
),
(
"function_without_result_value",
ResultDocstrings(docstrings=[]),
[],
),
(
"function_with_multiple_results",
ResultDocstrings(
docstrings=[
ResultDocstring(
type=NamedType(name="int", qname="builtins.int"),
description="first result",
name="first_result",
),
ResultDocstring(
type=NamedType(name="bool", qname="builtins.bool"),
description="second result",
name="second_result",
),
],
),
[
ResultDocstring(
type=NamedType(name="int", qname="builtins.int"),
description="first result",
name="first_result",
),
ResultDocstring(
type=NamedType(name="bool", qname="builtins.bool"),
description="second result",
name="second_result",
),
],
),
],
ids=[
Expand All @@ -963,7 +959,7 @@ def test_get_attribute_documentation(
def test_get_result_documentation(
numpydoc_parser: DocstringParser,
function_name: str,
expected_result_documentation: ResultDocstrings,
expected_result_documentation: list[ResultDocstring],
) -> None:
node = get_specific_mypy_node(mypy_file, function_name)
assert isinstance(node, nodes.FuncDef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)

# noinspection PyProtectedMember
from safeds_stubgen.docstring_parsing._docstring import AttributeDocstring, ResultDocstrings
from safeds_stubgen.docstring_parsing._docstring import AttributeDocstring, ResultDocstring

from tests.safeds_stubgen._helpers import get_specific_mypy_node

Expand Down Expand Up @@ -182,11 +182,11 @@ def test_get_attribute_documentation(
[
(
"function_with_documentation",
ResultDocstrings(docstrings=[]),
[],
),
(
"function_without_documentation",
ResultDocstrings(docstrings=[]),
[],
),
],
ids=[
Expand All @@ -197,7 +197,7 @@ def test_get_attribute_documentation(
def test_get_result_documentation(
plaintext_docstring_parser: PlaintextDocstringParser,
function_name: str,
expected_result_documentation: ResultDocstrings,
expected_result_documentation: list[ResultDocstring],
) -> None:
node = get_specific_mypy_node(mypy_file, function_name)
assert isinstance(node, nodes.FuncDef)
Expand Down
Loading

0 comments on commit 134532f

Please sign in to comment.