Skip to content

Commit 6e99ddc

Browse files
recognize int and float options if there is a int/float
1 parent edb2322 commit 6e99ddc

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

acclimatise/model.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def capital(self):
371371

372372

373373
int_re = re.compile(
374-
r"\b((int(eger)?)|size|length|max|min|num(ber)?)\b", flags=re.IGNORECASE
374+
r"\b((int(eger)?)|size|length|max|min|(num(ber)?))\b", flags=re.IGNORECASE
375375
)
376376
str_re = re.compile(r"\bstr(ing)?\b", flags=re.IGNORECASE)
377377
float_re = re.compile(r"\b(float|decimal)\b", flags=re.IGNORECASE)
@@ -384,8 +384,11 @@ def capital(self):
384384
default_re = re.compile(
385385
r"default(?: value)?(?:[:=] ?| )([^ )\]]+)", flags=re.IGNORECASE
386386
)
387-
float_re = re.compile(r"[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)", flags=re.IGNORECASE)
388-
int_re = re.compile(r"[+-]?([0-9]+[^.0-9])", flags=re.IGNORECASE)
387+
float_num_re = re.compile(
388+
r"[+-]?(([0-9]*\.[0-9]+)|((?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)))",
389+
flags=re.IGNORECASE,
390+
)
391+
int_num_re = re.compile(r"([+-]?[0-9]+)", flags=re.IGNORECASE)
389392

390393

391394
def infer_type(string) -> typing.Optional[cli_types.CliType]:
@@ -416,7 +419,13 @@ def infer_type(string) -> typing.Optional[cli_types.CliType]:
416419
elif str_re.search(string):
417420
return cli_types.CliString()
418421
else:
419-
return None
422+
if float_num_re.search(string):
423+
return cli_types.CliFloat()
424+
elif int_num_re.search(string):
425+
print("int num")
426+
return cli_types.CliInteger()
427+
else:
428+
return None
420429

421430

422431
@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)