Esempio n. 1
0
def merged_body(params: List[str], bound_args: Dict[str,
                                                    Any]) -> Dict[str, Any]:
    body = {}
    for param in params:
        arg = typic.primitive(bound_args[param])
        if isinstance(arg, Mapping):
            body.update(arg)
        else:
            body[param] = arg
    return body
Esempio n. 2
0
 def bind(self, args: Tuple[..., Any],
          kwargs: Dict[str, Any]) -> Dict[str, Dict[str, Any]]:
     bound_args = self.signature.bind(*args, **kwargs).arguments
     return {
         param_type: {
             p: dict(flatten_args(typic.primitive(bound_args[p])))
             for p in params
         } if param_type not in ('Body', 'Form') else merged_body(
             params, bound_args)
         for param_type, params in self.params_map.items()
     }
Esempio n. 3
0

@typic.klass
class Klass:
    attr: str


class Other:
    def __init__(self, attr: str):
        self.attr = attr


if __name__ == "__main__":

    Klass(attr="foo")
    Klass("foo")
    Klass.transmute("foo")
    Klass.validate({"attr": "foo"})
    Klass("foo").primitive()
    Klass("foo").primitive(lazy=True)
    Klass("foo").tojson()
    Klass("foo").tojson(indent=0)
    Klass("foo").tojson(ensure_ascii=False)
    typic.primitive(Klass("foo"))
    k: Klass = typic.transmute(Klass, "foo")
    v = typic.validate(Klass, {"attr": "foo"})
    j: str = typic.tojson(Klass("foo"))
    o: Other = Klass("foo").translate(Other)
    fields = [*Klass("foo").iterate()]
    iterfields = [*Klass("foo")]
Esempio n. 4
0
def test_tojson_native(obj, expected):
    native = json.dumps(typic.primitive(obj)).replace("\n",
                                                      "").replace(" ", "")
    assert typic.tojson(obj).decode() == native == expected
Esempio n. 5
0
def test_primitive(obj, expected):
    primitive = typic.primitive(obj)
    assert primitive == expected
    assert isinstance(primitive, type(expected))