Skip to content

Commit

Permalink
fix: Overloaded functions where ignored by the analyzer. (#151)
Browse files Browse the repository at this point in the history
Closes #144

### Summary of Changes

Fixed a bug where overloaded functions where ignored by the analyzer.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
Masara and megalinter-bot authored Jun 18, 2024
1 parent ebcadbb commit 3f87c26
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/safeds_stubgen/api_analyzer/_ast_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Callable
from typing import Any

from mypy.nodes import AssignmentStmt, ClassDef, Decorator, FuncDef, MypyFile
from mypy.nodes import AssignmentStmt, ClassDef, Decorator, FuncDef, MypyFile, OverloadedFuncDef

from ._mypy_helpers import get_classdef_definitions, get_funcdef_definitions, get_mypyfile_definitions

Expand Down Expand Up @@ -34,6 +34,8 @@ def __walk(self, node: MypyFile | ClassDef | Decorator | FuncDef | AssignmentStm
# function node too
if isinstance(node, Decorator):
node = node.func
elif isinstance(node, OverloadedFuncDef):
node = node.impl

if node in visited_nodes: # pragma: no cover
raise AssertionError("Node visited twice")
Expand All @@ -54,7 +56,8 @@ def __walk(self, node: MypyFile | ClassDef | Decorator | FuncDef | AssignmentStm
child_nodes = [
_def
for _def in definitions
if _def.__class__.__name__ in {"AssignmentStmt", "FuncDef", "ClassDef", "Decorator"}
if _def.__class__.__name__
in {"AssignmentStmt", "FuncDef", "ClassDef", "Decorator", "OverloadedFuncDef"}
]
elif isinstance(node, FuncDef) and node.name == "__init__":
definitions = get_funcdef_definitions(node)
Expand Down
28 changes: 27 additions & 1 deletion tests/data/various_modules_package/class_module.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Self
from typing import Self, overload

from tests.data.main_package.another_path.another_module import yetAnotherClass

Expand Down Expand Up @@ -59,3 +59,29 @@ def self_result2(self) -> Self:

def infer_self_result2(self):
return self


class ClassWithOverloadedFunction:
@overload
def overloaded_function(
self,
parameter_1: int,
*,
parameter_2: float = ...,
) -> bool: ...

@overload
def overloaded_function(
self,
parameter_1: str,
*,
parameter_2: bool,
) -> bool | None: ...

def overloaded_function(
self,
parameter_1: int,
*,
parameter_2: bool = True,
) -> bool | None:
return None
Original file line number Diff line number Diff line change
Expand Up @@ -7072,6 +7072,7 @@
'tests/data/various_modules_package/class_module/InheritFromException2',
'tests/data/various_modules_package/class_module/SelfTypes1',
'tests/data/various_modules_package/class_module/SelfTypes2',
'tests/data/various_modules_package/class_module/ClassWithOverloadedFunction',
]),
'docstring': '',
'enums': list([
Expand All @@ -7085,6 +7086,10 @@
'alias': None,
'qualified_name': 'typing.Self',
}),
dict({
'alias': None,
'qualified_name': 'typing.overload',
}),
dict({
'alias': None,
'qualified_name': 'tests.data.main_package.another_path.another_module.yetAnotherClass',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,12 @@ class SelfTypes2() sub SelfTypes1 {
@PythonName("infer_self_result2")
fun inferSelfResult2() -> result1: SelfTypes2
}

class ClassWithOverloadedFunction() {
@Pure
@PythonName("overloaded_function")
fun overloadedFunction(
@PythonName("parameter_1") parameter1: Int,
@PythonName("parameter_2") parameter2: Boolean = true
) -> result1: Boolean?
}

0 comments on commit 3f87c26

Please sign in to comment.