Skip to content

Commit 5142d63

Browse files
recognize int and float options if there is a int/float
1 parent c4fdde9 commit 5142d63

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

acclimatise/model.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def capital(self):
372372

373373

374374
int_re = re.compile(
375-
r"\b((int(eger)?)|size|length|max|min|num(ber)?)\b", flags=re.IGNORECASE
375+
r"\b((int(eger)?)|size|length|max|min|(num(ber)?))\b", flags=re.IGNORECASE
376376
)
377377
str_re = re.compile(r"\bstr(ing)?\b", flags=re.IGNORECASE)
378378
float_re = re.compile(r"\b(float|decimal)\b", flags=re.IGNORECASE)
@@ -385,8 +385,12 @@ def capital(self):
385385
default_re = re.compile(
386386
r"default(?: value)?(?:[:=] ?| )([^ )\]]+)", flags=re.IGNORECASE
387387
)
388-
float_re = re.compile(r"[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)", flags=re.IGNORECASE)
389-
int_re = re.compile(r"[+-]?([0-9]+[^.0-9])", flags=re.IGNORECASE)
388+
#
389+
float_num_re = re.compile(
390+
r"[+-]?(([0-9]*\.[0-9]+)|((?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)))",
391+
flags=re.IGNORECASE
392+
)
393+
int_num_re = re.compile(r"([+-]?[0-9]+)", flags=re.IGNORECASE)
390394

391395

392396
def infer_type(string) -> typing.Optional[cli_types.CliType]:
@@ -417,7 +421,13 @@ def infer_type(string) -> typing.Optional[cli_types.CliType]:
417421
elif str_re.search(string):
418422
return cli_types.CliString()
419423
else:
420-
return cli_types.CliString()
424+
if float_num_re.search(string):
425+
return cli_types.CliFloat()
426+
elif int_num_re.search(string):
427+
print("int num")
428+
return cli_types.CliInteger()
429+
else:
430+
return cli_types.CliString()
421431

422432

423433
@yaml_object(yaml)

test/test_type_inference.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
("folder", CliDir, None),
3232
("directory", CliDir, None),
3333
("output directory", CliDir, True),
34+
("blah 23 blub", CliInteger, False),
35+
("nonsense 23.42", CliFloat, True),
36+
(".42 gibberish", CliFloat, True),
37+
("1E-5", CliFloat, True),
3438
],
3539
)
3640
def test_type_inference(string, typ, isoutput):

0 commit comments

Comments
 (0)