def test_nest_union():
    d = {"a": True}
    c = into(d, C)
    check_type(c)

    d = {"a": {"a": 0, "b": ["foo"]}}
    c = into(d, C)
    check_type(c)
Esempio n. 2
0
 def from_disk(cls: Type[T_SerdeDataclass], path: Path) -> T_SerdeDataclass:
     """Load cls from json file."""
     meta_path = path / cls.FILENAME
     try:
         data = dataclass_utils.into(json.loads(meta_path.read_text()), cls)
     except dataclass_utils.error.Error as e:
         raise ValueError("Invalid metadata content.") from e
     return data
def test_nest():
    d = {"a": 1, "b": {"a": 1, "b": ["foo"]}}
    b = into(d, B)
    check_type(b)
def test_basic():
    d = {"a": 1, "b": ["foo", "bar"]}
    a = into(d, A)
    check_type(a)
def test_verb_key():
    with pytest.raises(TypeError):
        into({"c": 1}, A)
def test1():
    d = {"a": 1}
    a = into(d, A)
    check_type(a)
def test_invalid_dict():
    d = {"a": 1, 1: 2}
    with pytest.raises(TypeError):
        into(d, A0)
def test_any():
    assert into(1, Any) == 1
def test_str():
    with pytest.raises(TypeError):
        into("foo", List[str])
def test_literal():
    into("a", Literal["a"])
    into(1, Literal[1, 2])
    into("b", Literal["a", "b", "c"])
    with pytest.raises(TypeError):
        into(1, Literal["a"])
    with pytest.raises(TypeError):
        into("c", Literal["a"])

    ty = Tuple[Literal["a", "b"], int]
    into(("a", 10), ty)
    with pytest.raises(TypeError):
        into((1, 2), ty)
    with pytest.raises(TypeError):
        into(("c", 10), ty)

    ty = Optional[Tuple[Literal["a", "b"], int]]
    into(None, ty)
    with pytest.raises(TypeError):
        into((1, 2), ty)
def test0():
    into(1, int)
    into({"d": 1}, dict)
    into(1, Literal[1])
def test_nest_tuple():
    with pytest.raises(TypeError):
        into({"a": (A(), B(1, A()))}, D)
    v = into({"a": ({}, {"a": 1, "b": {}})}, D)
    check_type(v)