Skip to content

Commit

Permalink
#20 Use floats for numeric default args
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Nov 26, 2024
1 parent 924d8d0 commit 0b6aa25
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 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) -> Number:
def str_to_num(expr: str) -> float:
"""
Convert a literal string expression to a number e.g. "(-1)" to -1.
Expand All @@ -183,24 +183,14 @@ def str_to_num(expr: str) -> Number:
Returns
-------
Number
float
The converted number, or None if the conversion fails
"""
expr = expr.strip()
if expr.isnumeric():
return int(expr)

try:
result = float(expr)
return result
except ValueError:
pass

try:
result = ast.literal_eval(expr)
result = ast.literal_eval(expr.strip())
if isinstance(result, Number):
return result
except (ValueError, SyntaxError):
return float(result)
except (SyntaxError, TypeError, ValueError):
pass

return None
Expand Down

0 comments on commit 0b6aa25

Please sign in to comment.