Skip to content

Commit

Permalink
prune forced types and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vict0rsch committed Oct 31, 2021
1 parent ecfcf57 commit 6bbda8d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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]: '___'
Expand Down
14 changes: 4 additions & 10 deletions minydra/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Parser:
"bool",
"int",
"float",
"dict",
"list",
# "dict",
# "list",
"str",
"set",
# "set",
}

type_separator = "___"
Expand Down Expand Up @@ -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):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(["="])
Expand Down

0 comments on commit 6bbda8d

Please sign in to comment.