Skip to content

Commit

Permalink
improve code readability for sonarcloud checks
Browse files Browse the repository at this point in the history
  • Loading branch information
giaco5988 authored and int19h committed May 2, 2022
1 parent 644c658 commit 57836de
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/debugpy/common/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ def __format__(self, format_spec):
# some substitutions - e.g. replacing () with some default value.


def _converter(value, classinfo):
"""Convert value (str) to number, otherwise return None if is not possible"""
for one_info in classinfo:
if issubclass(one_info, numbers.Number):
try:
return one_info(value)
except ValueError:
pass


def of_type(*classinfo, **kwargs):
"""Returns a validator for a JSON property that requires it to have a value of
the specified type. If optional=True, () is also allowed.
Expand All @@ -108,12 +118,9 @@ def validate(value):
if (optional and value == ()) or isinstance(value, classinfo):
return value
else:
for one_info in classinfo:
if issubclass(one_info, numbers.Number):
try:
return one_info(value)
except ValueError:
pass
converted_value = _converter(value, classinfo)
if converted_value:
return converted_value

if not optional and value == ():
raise ValueError("must be specified")
Expand Down

0 comments on commit 57836de

Please sign in to comment.