Skip to content

Commit

Permalink
fix: Added handling for boolean default values for parameters (#25)
Browse files Browse the repository at this point in the history
### Summary of Changes

Added handling for boolean default values for parameters and adjusted
corresponding test snapshots.
  • Loading branch information
Masara committed Oct 16, 2023
1 parent 5e0b3b1 commit 1ff250d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
27 changes: 15 additions & 12 deletions src/safeds_stubgen/api_analyzer/_ast_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,9 @@ def enter_assignmentstmt(self, node: AssignmentStmt) -> None:
for assignment in self.parse_attributes(lvalue, node.unanalyzed_type, is_static=True):
assignments.append(assignment)
elif isinstance(parent, Function) and parent.name == "__init__":
try:
grand_parent = self.__declaration_stack[-2]
except IndexError:
# If the function has no parent (and is therefore not a class method) ignore the attributes
grand_parent = None

if grand_parent is not None and isinstance(grand_parent, Class) and not isinstance(lvalue, NameExpr):
grand_parent = self.__declaration_stack[-2]
# If the grandparent is not a class we ignore the attributes
if isinstance(grand_parent, Class) and not isinstance(lvalue, NameExpr):
# Ignore non instance attributes in __init__ classes
for assignment in self.parse_attributes(lvalue, node.unanalyzed_type, is_static=False):
assignments.append(assignment)
Expand Down Expand Up @@ -323,7 +319,7 @@ def leave_assignmentstmt(self, _: AssignmentStmt) -> None:
self.api.add_enum_instance(assignment)
parent.add_enum_instance(assignment)

else:
else: # pragma: no cover
raise TypeError("Unexpected value type for assignments")

# ############################## Utilities ############################## #
Expand Down Expand Up @@ -478,7 +474,7 @@ def create_attribute(
else: # pragma: no cover
raise AttributeError("Could not get argument information for attribute.")

else:
else: # pragma: no cover
raise TypeError("Attribute has an unexpected type.")

type_ = None
Expand Down Expand Up @@ -525,9 +521,16 @@ def parse_parameter_data(self, node: FuncDef, function_id: str) -> list[Paramete
if isinstance(initializer, CallExpr):
# Special case when the default is a call expression
value = None
elif hasattr(initializer, "name") and initializer.name == "None":
value = None
else:
elif hasattr(initializer, "name"):
if initializer.name == "None":
value = None
elif initializer.name == "True":
value = True
elif initializer.name == "False":
value = False
else: # pragma: no cover
raise ValueError("No value found for parameter")
else: # pragma: no cover
raise ValueError("No value found for parameter")
else:
value = initializer.value
Expand Down
6 changes: 3 additions & 3 deletions tests/data/test_package/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ def __init__(self, init_param_1):
self._init_attr_private: float = "I'm a string"
no_class_attr: bool

def _some_function(self, param_1: bool, param_2: ac_alias) -> ac_alias:
def _some_function(self, param_1: ac_alias, param_2: bool = False) -> ac_alias:
"""Function Docstring.
param_1: bool.
param_2: bool.
"""

@staticmethod
def static_function(param_1: bool, param_2: int | bool = 123456) -> tuple[bool, int]:
def static_function(param_1: bool = True, param_2: int | bool = 123456) -> tuple[bool, int]:
"""Function Docstring."""

def test_position(self, param1, /, param2: bool, param3=1, *, param4=AcDoubleAlias(), param5: int = 1) -> Any:
Expand Down
16 changes: 8 additions & 8 deletions tests/safeds_stubgen/__snapshots__/test_main.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1632,12 +1632,12 @@
'description': '''
Function Docstring.

param_1: bool.
param_2: bool.
''',
'full_docstring': '''
Function Docstring.

param_1: bool.
param_2: bool.
''',
}),
'id': 'test_package/test_module/SomeClass/_some_function',
Expand Down Expand Up @@ -2495,23 +2495,23 @@
'name': 'param_1',
'type': dict({
'kind': 'NamedType',
'name': 'bool',
'name': 'AnotherClass',
}),
}),
dict({
'assigned_by': 'POSITION_OR_NAME',
'default_value': None,
'default_value': False,
'docstring': dict({
'default_value': '',
'description': '',
'type': '',
}),
'id': 'test_package/test_module/SomeClass/_some_function/param_2',
'is_optional': False,
'is_optional': True,
'name': 'param_2',
'type': dict({
'kind': 'NamedType',
'name': 'AnotherClass',
'name': 'bool',
}),
}),
dict({
Expand Down Expand Up @@ -2580,14 +2580,14 @@
}),
dict({
'assigned_by': 'POSITION_OR_NAME',
'default_value': None,
'default_value': True,
'docstring': dict({
'default_value': '',
'description': '',
'type': '',
}),
'id': 'test_package/test_module/SomeClass/static_function/param_1',
'is_optional': False,
'is_optional': True,
'name': 'param_1',
'type': dict({
'kind': 'NamedType',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,12 +848,12 @@
'description': '''
Function Docstring.

param_1: bool.
param_2: bool.
''',
'full_docstring': '''
Function Docstring.

param_1: bool.
param_2: bool.
''',
}),
'id': 'test_package/test_module/SomeClass/_some_function',
Expand Down Expand Up @@ -2097,14 +2097,14 @@
list([
dict({
'assigned_by': 'POSITION_OR_NAME',
'default_value': None,
'default_value': True,
'docstring': dict({
'default_value': '',
'description': '',
'type': '',
}),
'id': 'test_package/test_module/SomeClass/static_function/param_1',
'is_optional': False,
'is_optional': True,
'name': 'param_1',
'type': dict({
'kind': 'NamedType',
Expand Down

0 comments on commit 1ff250d

Please sign in to comment.