def build_resp_model(cls): ns = { 'code': Field(cls.CODE, const=True, example=cls.CODE), 'message': Field(cls.MESSAGE, const=True, example=cls.MESSAGE), '__annotations__': { 'code': int, 'message': str, } } data_model = cls.get_data_model() if data_model is not None: if not cls.data_required: data_model = Optional[data_model] # noinspection PyTypeChecker ns['__annotations__']['data'] = data_model name = cls._component_name or cls.__name__ _JsonRpcErrorModel = ModelMetaclass.__new__(ModelMetaclass, '_JsonRpcErrorModel', (BaseModel,), ns) _JsonRpcErrorModel = component_name(name, cls.__module__)(_JsonRpcErrorModel) @component_name(f'_ErrorResponse[{name}]', cls.__module__) class _ErrorResponseModel(BaseModel): jsonrpc: StrictStr = Field('2.0', const=True, example='2.0') id: Union[StrictStr, int] = Field(None, example=0) error: _JsonRpcErrorModel class Config: extra = 'forbid' return _ErrorResponseModel
def __new__(cls, name, bases, namespace, **kwargs): firestore = FirestoreConfig for base in reversed(bases): if ( _DOCUMENT_CLASS_DEFINED and issubclass(base, Document) and base != Document ): firestore = inherit_config(base.__firestore__, firestore) firestore = inherit_config(namespace.get("Firestore"), firestore) annotations = namespace.get("__annotations__", {}) ids = {k: v for k, v in annotations.items() if issubclass(v, FirestoreID)} if ( _DOCUMENT_CLASS_DEFINED and len(ids) != 1 and not hasattr(firestore, "id_attr") ): raise TypeError( f'"{name}" must have exactly one attribute of type FirestoreID.' ) elif len(ids) == 1: id_attr = list(ids.keys())[0] setattr(firestore, "id_attr", id_attr) namespace["__firestore__"] = firestore return ModelMetaclass.__new__(cls, name, bases, namespace, **kwargs)
def make_request_model(name, module, body_params: List[ModelField]): whole_params_list = [ p for p in body_params if isinstance(p.field_info, Params) ] if len(whole_params_list): if len(whole_params_list) > 1: raise RuntimeError(f"Only one 'Params' allowed: " f"params={whole_params_list}") body_params_list = [ p for p in body_params if not isinstance(p.field_info, Params) ] if body_params_list: raise RuntimeError( f"No other params allowed when 'Params' used: " f"params={whole_params_list}, other={body_params_list}") if whole_params_list: assert whole_params_list[0].alias == 'params' params_field = whole_params_list[0] else: _JsonRpcRequestParams = ModelMetaclass.__new__( ModelMetaclass, '_JsonRpcRequestParams', (BaseModel, ), {}) for f in body_params: _JsonRpcRequestParams.__fields__[f.name] = f _JsonRpcRequestParams = component_name(f'_Params[{name}]', module)(_JsonRpcRequestParams) params_field = ModelField( name='params', type_=_JsonRpcRequestParams, class_validators={}, default=None, required=True, model_config=BaseConfig, field_info=Field(...), ) class _Request(BaseModel): jsonrpc: StrictStr = Field('2.0', const=True, example='2.0') id: Union[StrictStr, int] = Field(None, example=0) method: StrictStr = Field(name, const=True, example=name) class Config: extra = 'forbid' _Request.__fields__[params_field.name] = params_field _Request = component_name(f'_Request[{name}]', module)(_Request) return _Request
def __new__(mcs, name, bases, namespace): slots = set(namespace.pop("__slots__", tuple())) for base in bases: if hasattr(base, "__slots__"): slots.update(base.__slots__) if "__dict__" in slots: slots.remove("__dict__") namespace["__slots__"] = tuple(slots) return ModelMetaclass.__new__(mcs, name, bases, namespace)
def build_data_model(cls): if cls.DataModel is not None: return rename_if_scope_child_component(cls, cls.DataModel, 'Data') error_model = cls.get_error_model() if error_model is None: return None errors_annotation = List[error_model] if not cls.errors_required: errors_annotation = Optional[errors_annotation] ns = { '__annotations__': { 'errors': errors_annotation, } } _ErrorData = ModelMetaclass.__new__(ModelMetaclass, '_ErrorData', (BaseModel,), ns) _ErrorData = component_name(f'_ErrorData[{error_model.__name__}]', error_model.__module__)(_ErrorData) return _ErrorData