コード例 #1
0
ファイル: _load_impl.py プロジェクト: yyanglike/asynciotest
def _check_and_get_cls_and_meta_hints(
        json_obj: object, cls: type, fork_inst: type,
        inferred_cls: bool) -> Tuple[type, Optional[dict]]:
    # Check if json_obj is of a valid type and return the cls.
    if type(json_obj) not in VALID_TYPES:
        invalid_type = get_class_name(type(json_obj), fully_qualified=True)
        valid_types = [
            get_class_name(typ, fully_qualified=True) for typ in VALID_TYPES
        ]
        msg = ('Invalid type: "{}", only arguments of the following types are '
               'allowed: {}'.format(invalid_type, ", ".join(valid_types)))
        raise DeserializationError(msg, json_obj, cls)

    cls_from_meta, meta = get_cls_and_meta(json_obj, fork_inst)
    meta_hints = meta.get('classes', {}) if meta else {}
    return determine_precedence(cls, cls_from_meta, type(json_obj),
                                inferred_cls), meta_hints
コード例 #2
0
def _get_value_from_obj(obj, cls, sig, sig_key, meta_hints, **kwargs):
    # Obtain the value for the attribute with the given signature from the
    # given obj. Try to obtain the class of this attribute from the meta info
    # or from type hints.
    cls_key = '/{}'.format(sig_key)
    cls_str_from_meta = meta_hints.get(cls_key, None)
    new_hints = meta_hints
    cls_from_meta = None
    if cls_str_from_meta:
        cls_from_meta = get_cls_from_str(
            cls_str_from_meta, obj, kwargs['fork_inst'])
        # Rebuild the class hints: cls_key becomes the new root.
        new_hints = {
            _remove_prefix(cls_key, key): meta_hints[key]
            for key in meta_hints
        }
    cls_ = determine_precedence(cls=cls, cls_from_meta=cls_from_meta,
                                cls_from_type=None, inferred_cls=True)
    value = load(obj[sig_key], cls_, meta_hints=new_hints, **kwargs)
    return value