Skip to content

Commit

Permalink
Merge pull request #69 from Chaste/numeric-default-args
Browse files Browse the repository at this point in the history
Cast int or float in numeric default args
  • Loading branch information
kwabenantim authored Nov 26, 2024
2 parents 693351f + 60f0e3b commit 9417584
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
6 changes: 4 additions & 2 deletions cppwg/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def read_source_file(
return source


def str_to_num(expr: str) -> float:
def str_to_num(expr: str, integer: bool = False) -> Number:
"""
Convert a literal string expression to a number e.g. "(-1)" to -1.
Expand All @@ -183,12 +183,14 @@ def str_to_num(expr: str) -> float:
Returns
-------
float
Number
The converted number, or None if the conversion fails
"""
try:
result = ast.literal_eval(expr.strip())
if isinstance(result, Number):
if integer:
return int(result)
return float(result)
except (SyntaxError, TypeError, ValueError):
pass
Expand Down
8 changes: 5 additions & 3 deletions cppwg/writers/constructor_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ def generate_wrapper(self) -> str:
):
# Try to convert "(-1)" to "-1" etc.
default_value = str(arg.default_value)
num = utils.str_to_num(default_value)
if num is not None:
default_value = str(num)
value = utils.str_to_num(
default_value, integer="int" in str(arg.decl_type)
)
if value is not None:
default_value = str(value)

# Check for template params in default value
if self.template_params:
Expand Down
16 changes: 9 additions & 7 deletions cppwg/writers/free_function_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ def generate_wrapper(self) -> str:
# e.g. with default values: ', py::arg("foo") = 1, py::arg("bar") = 2'
default_args = ""
if not self.free_function_info.hierarchy_attribute("exclude_default_args"):
for argument in self.free_function_info.decls[0].arguments:
default_args += f', py::arg("{argument.name}")'
if argument.default_value is not None:
for arg in self.free_function_info.decls[0].arguments:
default_args += f', py::arg("{arg.name}")'
if arg.default_value is not None:
# Try to convert "(-1)" to "-1" etc.
default_value = str(argument.default_value)
num = utils.str_to_num(default_value)
if num is not None:
default_value = str(num)
default_value = str(arg.default_value)
value = utils.str_to_num(
default_value, integer="int" in str(arg.decl_type)
)
if value is not None:
default_value = str(value)
default_args += f" = {default_value}"

# Add the free function wrapper code to the wrapper string
Expand Down
8 changes: 5 additions & 3 deletions cppwg/writers/method_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ def generate_wrapper(self) -> str:
):
# Try to convert "(-1)" to "-1" etc.
default_value = str(arg.default_value)
num = utils.str_to_num(default_value)
if num is not None:
default_value = str(num)
value = utils.str_to_num(
default_value, integer="int" in str(arg.decl_type)
)
if value is not None:
default_value = str(value)

# Check for template params in default value
if self.template_params:
Expand Down

0 comments on commit 9417584

Please sign in to comment.