mirror of
https://github.com/microsoft/debugpy.git
synced 2025-12-23 08:48:12 +00:00
more flexible logic that try converting str to number with all classinfo and fall back on error message when not successfull
This commit is contained in:
parent
50632736bf
commit
7597e466c4
1 changed files with 8 additions and 11 deletions
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue