Skip to content

Commit

Permalink
✅ Cover type conversion of ProcessType
Browse files Browse the repository at this point in the history
  • Loading branch information
valentingol committed Dec 17, 2023
1 parent b456b95 commit 43257b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
17 changes: 11 additions & 6 deletions tests/unit/processing/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,9 @@ def test_process_typing() -> None:
flat_config = Config(flat_dict, [processing])
flat_config = processing.premerge(flat_config)
flat_config.dict["param1"] = 3
flat_config.dict["param2"] = {"a": None, "b": [{"c": 1}]}
flat_config.dict = flatten(flat_config.dict)
flat_config.dict["param2"] = [None, {"b": 1, "c": 2.1}]
flat_config = processing.postmerge(flat_config)
check.equal(
flat_config.dict, {"param1": 3, "param2.a": None, "param2.b": [{"c": 1}]}
)
check.equal(flat_config.dict, {"param1": 3, "param2": [None, {"b": 1, "c": 2.1}]})
check.equal(
flat_config.process_list[0].forced_types, # type: ignore
{
Expand All @@ -252,10 +249,18 @@ def test_process_typing() -> None:
{"param1": "int", "param2": "List[Optional[Dict[str, int|float]]]"},
)
flat_config = processing.endbuild(flat_config) # no error
flat_config.dict["param1"] = "3"
flat_config.dict["param2"] = [{"d": "2.3"}, None]
flat_config = processing.endbuild(flat_config) # no error
check.equal(flat_config.dict["param1"], 3)
check.equal(flat_config.dict["param2"], [{"d": 2.3}, None])
flat_config = processing.presave(flat_config)
check.equal(
flat_config.dict,
{"param1@type:int": 3, "param2.a": None, "param2.b": [{"c": 1}]},
{
"param1@type:int": 3,
"param2@type:List[Optional[Dict[str, int|float]]]": [{"d": 2.3}, None],
},
)
processing.forced_types = {} # Reset forced types
processing.type_desc = {} # Reset type description
Expand Down
23 changes: 19 additions & 4 deletions tests/unit/processing/test_type_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest_check as check

from cliconfig.processing._type_parser import (
_convert_type,
_isinstance,
_parse_dict,
_parse_list,
Expand All @@ -14,24 +15,30 @@
)


def test_type_parser_isinstance() -> None:
"""Test _parse_type and _isinstance."""
def test_type_routines() -> None:
"""Test _parse_type, _isinstance and _convert_type."""
type_ = _parse_type("None")
check.equal(type_, (type(None),))
check.is_true(_isinstance(None, type_))
check.is_false(_isinstance([None], type_))
check.equal(_convert_type("unchanged", type_), "unchanged")

type_ = _parse_type("List[float]")
check.equal(type_, (("list", (float,)),))

type_ = _parse_type("list|dict")
check.equal(type_, (list, dict))
type_ = _parse_type("dict|list")
check.equal(type_, (dict, list))
check.is_true(_isinstance([2.0, True], type_))
check.equal(_convert_type((0, 1), type_), [0, 1])

type_ = _parse_type("Dict[str, List[float]]")
check.equal(type_, (("dict", (str,), (("list", (float,)),)),))
check.is_true(_isinstance({"a": [1.0, 2.0]}, type_))
check.is_false(_isinstance({"a": [1.0, 2.0], "b": [1.0, 2]}, type_))
check.equal(
_convert_type({"a": [1.0, 2.0], "b": [1.0, 2]}, type_),
{"a": [1.0, 2.0], "b": [1.0, 2.0]},
)

type_ = _parse_type("Dict[str|bool, int|float]")
check.equal(type_, (("dict", (str, bool), (int, float)),))
Expand Down Expand Up @@ -65,7 +72,15 @@ def test_type_parser_isinstance() -> None:
check.is_true(_isinstance({1: {"a": 2.0}}, type_))
check.is_false(_isinstance({"a": [None, 1.0], "b": {False: 1, True: "1"}}, type_))

type_ = _parse_type("Dict[int, Optional[Dict[str, float]]]")
check.equal(
_convert_type({"0": {1: True, 2: "2.3"}, "1": {3: 3, 4: 4}}, type_),
{0: {"1": 1.0, "2": 2.3}, 1: {"3": 3.0, "4": 4.0}},
)
check.equal(_convert_type({"str": None}, type_), {"str": None})

# Wrong type description
check.equal(_convert_type({"str": None}, ("list")), {"str": None}) # type: ignore
with pytest.raises(ValueError, match="Unknown type: 'unknown'"):
_parse_type("unknown")
desc = "str, List[float]"
Expand Down

0 comments on commit 43257b6

Please sign in to comment.