From 6bbda8d9a5649e7932c7ea58f64b28fa0fbdf939 Mon Sep 17 00:00:00 2001 From: vict0rsch Date: Sun, 31 Oct 2021 03:20:36 -0400 Subject: [PATCH] prune forced types and add tests --- README.md | 4 ++-- minydra/parser.py | 14 ++++---------- tests/test_parser.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0fb9bb7..9aafc67 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Easily parse arbitrary arguments from the command line without dependencies: ![example code](assets/code.png) ![example code](assets/run.png) -![](https://img.shields.io/badge/coverage-93%25-success) +![](https://img.shields.io/badge/coverage-97%25-success) ![](https://img.shields.io/badge/version-0.1.3-informational) ![](https://img.shields.io/badge/python-3.7%2B%20-orange) @@ -157,7 +157,7 @@ Known types are defined in `Parser.known_types` and the separator (`___`) in `Pa In [1]: from minydra import Parser In [2]: Parser.known_types -Out[2]: {'bool', 'dict', 'float', 'int', 'list', 'set', 'str'} +Out[2]: {'bool', 'float', 'int', 'str'} In [3]: Parser.type_separator Out[3]: '___' diff --git a/minydra/parser.py b/minydra/parser.py index c758eb7..1db1421 100644 --- a/minydra/parser.py +++ b/minydra/parser.py @@ -14,10 +14,10 @@ class Parser: "bool", "int", "float", - "dict", - "list", + # "dict", + # "list", "str", - "set", + # "set", } type_separator = "___" @@ -134,17 +134,11 @@ def _force_type(value, type_str): if type_str == "bool": return bool(value) if type_str == "int": - return int(value) + return int(float(value)) if type_str == "float": return float(value) - if type_str == "dict": - return dict(value) - if type_str == "list": - return list(value) if type_str == "str": return str(value) - if type_str == "set": - return set(value) @staticmethod def _parse_arg(arg, type_str=None): diff --git a/tests/test_parser.py b/tests/test_parser.py index 7959130..a9357ca 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -58,6 +58,20 @@ def test_dotted(): assert run(["a.b.x=2"]) == capture(MinyDict({"a": {"b": {"x": 2}}})) +def test_force_types(): + assert ( + run( + [ + "a___str=01", + "b___bool=d", + "c___int=1.3", + "d___float=2", + ] + ) + == capture(MinyDict({"a": "01", "b": True, "c": 1, "d": 2.0})) + ) + + def test_fail_equal(): assert "MinydraWrongArgumentException" in fail(["a="]) assert "MinydraWrongArgumentException" in fail(["="])