Skip to content

Commit 57836de

Browse files
giaco5988int19h
authored andcommitted
improve code readability for sonarcloud checks
1 parent 644c658 commit 57836de

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/debugpy/common/json.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ def __format__(self, format_spec):
9393
# some substitutions - e.g. replacing () with some default value.
9494

9595

96+
def _converter(value, classinfo):
97+
"""Convert value (str) to number, otherwise return None if is not possible"""
98+
for one_info in classinfo:
99+
if issubclass(one_info, numbers.Number):
100+
try:
101+
return one_info(value)
102+
except ValueError:
103+
pass
104+
105+
96106
def of_type(*classinfo, **kwargs):
97107
"""Returns a validator for a JSON property that requires it to have a value of
98108
the specified type. If optional=True, () is also allowed.
@@ -108,12 +118,9 @@ def validate(value):
108118
if (optional and value == ()) or isinstance(value, classinfo):
109119
return value
110120
else:
111-
for one_info in classinfo:
112-
if issubclass(one_info, numbers.Number):
113-
try:
114-
return one_info(value)
115-
except ValueError:
116-
pass
121+
converted_value = _converter(value, classinfo)
122+
if converted_value:
123+
return converted_value
117124

118125
if not optional and value == ():
119126
raise ValueError("must be specified")

0 commit comments

Comments
 (0)