Skip to content

Commit

Permalink
Added information for class methods to api analyzer and stubs generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Masara committed Nov 14, 2023
1 parent 912b6ca commit 35f0dd1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/safeds_stubgen/api_analyzer/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class Function:
docstring: FunctionDocstring
is_public: bool
is_static: bool
is_class_method: bool = False
results: list[Result] = field(default_factory=list)
reexported_by: list[Module] = field(default_factory=list)
parameters: list[Parameter] = field(default_factory=list)
Expand All @@ -240,6 +241,7 @@ def to_dict(self) -> dict[str, Any]:
"docstring": self.docstring.to_dict(),
"is_public": self.is_public,
"is_static": self.is_static,
"is_class_method": self.is_class_method,
"results": [result.id for result in self.results],
"reexported_by": [module.id for module in self.reexported_by],
"parameters": [parameter.id for parameter in self.parameters],
Expand Down
1 change: 1 addition & 0 deletions src/safeds_stubgen/api_analyzer/_ast_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def enter_funcdef(self, node: FuncDef) -> None:
docstring=docstring,
is_public=is_public,
is_static=is_static,
is_class_method=node.is_class,
results=results,
reexported_by=reexported_by,
parameters=arguments,
Expand Down
24 changes: 18 additions & 6 deletions src/safeds_stubgen/stubs_generator/_generate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _create_module_files(self) -> None:
# Create global functions
for function in module.global_functions:
if function.is_public:
module_text += f"\n{self._create_function_string(function, is_class_method=False)}\n"
module_text += f"\n{self._create_function_string(function, is_method=False)}\n"

# Create classes, class attr. & class methods
for class_ in module.classes:
Expand Down Expand Up @@ -168,7 +168,7 @@ def _create_class_string(self, class_: Class, class_indentation: str = "") -> st
if not method.is_public:
continue
class_methods.append(
self._create_function_string(method, inner_indentations, is_class_method=True),
self._create_function_string(method, inner_indentations, is_method=True),
)
if class_methods:
methods = "\n\n".join(class_methods)
Expand All @@ -183,14 +183,25 @@ def _create_class_string(self, class_: Class, class_indentation: str = "") -> st

return f"{class_signature} {{{class_text}"

def _create_function_string(self, function: Function, indentations: str = "", is_class_method: bool = False) -> str:
def _create_function_string(self, function: Function, indentations: str = "", is_method: bool = False) -> str:
"""Create a function string for Safe-DS stubs."""
# Check if static or class method
is_static = function.is_static
static = "static " if is_static else ""
is_class_method = function.is_class_method

static = ""
if is_class_method or is_static:
static = "static "

if is_class_method:
self.current_todo_msgs.add("class_method")

# Parameters
is_instance_method = not is_static and is_class_method
func_params = self._create_parameter_string(function.parameters, indentations, is_instance_method)
func_params = self._create_parameter_string(
parameters=function.parameters,
indentations=indentations,
is_instance_method=not is_static and is_method
)

# Convert function name to camelCase
name = function.name
Expand Down Expand Up @@ -453,6 +464,7 @@ def create_todo_msg(self, indentations: str) -> str:
"REQ_NAME_ONLY": "Safe-DS does not support required but name only parameter assignments.",
"multiple_inheritance": "Safe-DS does not support multiple inheritance.",
"variadic": "Safe-DS does not support variadic parameters.",
"class_method": "Safe-DS does not support class methods",
}[msg]
for msg in self.current_todo_msgs
]
Expand Down
8 changes: 8 additions & 0 deletions tests/data/test_package/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ def no_return_1(self):
def no_return_2(self) -> None:
pass

@classmethod
def class_method(cls) -> None:
pass

@classmethod
def class_method_params(cls, param_1: int) -> bool:
pass

class NestedClass(_AcImportAlias):
def nested_class_function(self, param_1: int) -> set[bool | None]:
pass
Expand Down
2 changes: 1 addition & 1 deletion tests/data/test_stub_generation/function_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def __init__(self, init_param): ...
def instance_method(self, a: A) -> A: ...

@staticmethod
def static_class_method() -> None: ...
def static_method() -> None: ...

@classmethod
def class_method(cls): ...
Expand Down

0 comments on commit 35f0dd1

Please sign in to comment.