From 0b6aa25d148de9b7ad1d3adc2cbb0a40654ca3bd Mon Sep 17 00:00:00 2001 From: Kwabena Amponsah Date: Tue, 26 Nov 2024 08:37:13 +0000 Subject: [PATCH] #20 Use floats for numeric default args --- cppwg/utils/utils.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/cppwg/utils/utils.py b/cppwg/utils/utils.py index d3d3aab..bdd38d7 100644 --- a/cppwg/utils/utils.py +++ b/cppwg/utils/utils.py @@ -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. @@ -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