def get_model_mapper(model, stoppage=None, full=True): """Get a dictionary of name: class for all the objects in model.""" model = get_model(model) flat_models = get_flat_models_from_model(model) # this is the list of all the referenced objects model_name_map = get_model_name_map(flat_models) # flip the dictionary so I can access each class by name model_name_map = {v: k for k, v in model_name_map.items()} if full: if not stoppage: stoppage = set( ['NoExtraBaseModel', 'ModelMetaclass', 'BaseModel', 'object']) # Pydantic does not necessarily add all the baseclasses to the OpenAPI # documentation. We check all of them and them to the list if they are not # already added models = list(model_name_map.values()) for model in models: for cls in type.mro(model): if cls.__name__ in stoppage: break if cls.__name__ not in model_name_map: model_name_map[cls.__name__] = cls return model_name_map
def test_get_model(): class A(BaseModel): a: str assert get_model(A) == A @dataclass class B: a: str assert get_model(B) == B.__pydantic_model__ class C: pass with pytest.raises(TypeError): get_model(C)