def pydantic_model(request): typed_dict_obj = request.param class Config: extra = Extra.forbid return create_model_from_typeddict( typed_dict_obj, __config__=Config, )
'val': 12, 'name': 'John', } class Config(BaseConfig): title = 'Record' extra = Extra.ignore max_anystr_length = 1234 class Settings(BaseSettings): ... class CustomPath(PurePath): def __init__(self, *args: str): self.path = os.path.join(*args) def __fspath__(self) -> str: return f'a/custom/{self.path}' def dont_check_path_existence() -> None: Settings(_env_file='a/path', _secrets_dir='a/path') Settings(_env_file=CustomPath('a/path'), _secrets_dir=CustomPath('a/path')) create_model_from_typeddict(SomeDict)(**obj) DynamicModel = create_model('DynamicModel')
import pytest from loguru import logger from starlette.testclient import TestClient from app.apis.secondary_views.gallery.specs import GallerySpecName from app.apis.topic_views.confounder import ConfounderType from app.apis.topic_views.pqtl import pqtl_models from app.apis.topic_views.xqtl import XqtlMrMethod, XqtlQtlType from app.main import app from app.models import EpigraphdbGraphs, TopicViewEndpoints from app.models.cypher_diagram_models import CypherDiagramData from app.models.network_graph_models import VisData from app.utils import unittest_headers client = TestClient(app) NetworkGraphDataModel = pydantic.create_model_from_typeddict( VisData) # type: ignore CypherDiagramDataModel = pydantic.create_model_from_typeddict( CypherDiagramData) # type: ignore # General functionalities get_response_params_general = [ # status ("/ping", None), ("/status/ping", None), ("/status/env/table", None), # about ("/about/schema", None), # metadata *[("/metadata/meta_node/list", { "db": db }) for db in [item.value for item in EpigraphdbGraphs]],
def test_process_schema(): SchemaModel = create_model_from_typeddict(MetaSchemaData) schema_data = process_schema() assert SchemaModel(**schema_data)
from typing_extensions import TypedDict from pydantic import ValidationError, create_model_from_typeddict class User(TypedDict): name: str id: int class Config: extra = 'forbid' UserM = create_model_from_typeddict(User, __config__=Config) print(repr(UserM(name=123, id='3'))) try: UserM(name=123, id='3', other='no') except ValidationError as e: print(e)
def test_resources_extra_dict(): Model = create_model_from_typeddict(models.RawResourcesExtra) assert Model(**resources_extra_dict)
def test_resources_dict_raw(): Model = create_model_from_typeddict(models.RawResources) assert Model(**resources_dict_raw)
def test_meta_rels_dict_raw(): Model = create_model_from_typeddict(models.RawMetaRel) for key, value in meta_rels_dict_raw.items(): assert Model(**value)
class SchemaInfoInfo(TypedDict): nodes: Dict[str, Any] rels: Dict[str, Any] class SchemaInfoData(TypedDict): graph: SchemaInfoGraphData info: SchemaInfoInfo class SchemaMetricsMetaNode(TypedDict): node_name: str count: int class SchemaMetricsMetaRel(TypedDict): relationshipType: str count: int class SchemaMetricsData(TypedDict): meta_node: List[SchemaMetricsMetaNode] meta_rel: List[SchemaMetricsMetaRel] AboutSchemaResponse = create_model_from_typeddict(SchemaInfoData) AboutSchemaMetrics = create_model_from_typeddict(SchemaMetricsData)