From 7597e466c4fa137f489808841d6afaf51dacdb10 Mon Sep 17 00:00:00 2001 From: giacomo Date: Thu, 28 Apr 2022 13:50:32 +0100 Subject: [PATCH] more flexible logic that try converting str to number with all classinfo and fall back on error message when not successfull --- src/debugpy/common/json.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/debugpy/common/json.py b/src/debugpy/common/json.py index 3502b09f..7e1b317b 100644 --- a/src/debugpy/common/json.py +++ b/src/debugpy/common/json.py @@ -7,9 +7,8 @@ import builtins import json -import operator -import string import numbers +import operator JsonDecoder = json.JSONDecoder @@ -108,16 +107,14 @@ def of_type(*classinfo, **kwargs): def validate(value): if (optional and value == ()) or isinstance(value, classinfo): return value - elif ( - isinstance(value, str) - and all(x in string.digits + "." for x in value) - and any(issubclass(x, numbers.Number) for x in classinfo) - ): - try: - return int(value) - except ValueError: - return float(value) else: + for one_info in classinfo: + if issubclass(one_info, numbers.Number): + try: + return one_info(value) + except ValueError: + pass + if not optional and value == (): raise ValueError("must be specified") raise TypeError("must be " + " or ".join(t.__name__ for t in classinfo))